summaryrefslogtreecommitdiff
path: root/lib/import.pm
blob: 7edefdccd0a867ab6d8b97afc795a2910395d6fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
sub situate_imports
	{
	my ($pid, $uname) = @_;
	my ($filename, $size, $date);
	my $newfilename;
	my $tempsubdir;
	my $totalsize = 0;
	my @stats;

	open T, ">$temp_path/.importnow";
	close T;
	system($SYSTEM_CHMOD, "0777", "$temp_path/.importnow");
	print "Waiting to import...<br>" if ($DEBUG);
	while (-e "$temp_path/.importnow")
		{ sleep(1); }
	print "Setting permissions...<br>" if ($DEBUG);
	while (-e "$temp_path/.importing")
		{ sleep(1); }
	print "Ready to import!<br>" if ($DEBUG);

	foreach my $k (keys %$input)
		{
		next if ($k !~ /^imp/);
		next if (! -e $temp_path."/".$$input{$k});
		$filename = $$input{$k};

		if (! -e $data_path."/".$pid)
			{
			print "creating $data_path/$pid<br>\n" if $DEBUG;
            system("$MKDIR_PATH", $data_path."/".$pid);
            system("$MKDIR_PATH", $data_path."/".$pid."/.thumb");
            system("$CHMOD_PATH", "755", $data_path."/".$pid);
            system("$CHMOD_PATH", "755", $data_path."/".$pid."/.thumb");
			}

		print $filename."<br>" if ($DEBUG);

		@stats = stat($temp_path."/".$filename);
		$size = $stats[7];
		$date = $stats[9];
		$newfilename = $filename;
		$newfilename =~ s/^(.*)\///;
		$tempsubdir = $1;

		if (-e $data_path."/".$pid."/".$newfilename)
			{
			my $tfile = "another-$newfilename";
			my $i = 2;
			while (-e $data_path."/".$pid."/".$tfile)
				{
				$tfile = $newfilename;
				$tfile =~ s/(\....)$/-$i$1/;
				$i++;
				}
			$newfilename = $tfile;
			}

		system($MV_PATH, $temp_path."/".$filename, $data_path."/".$pid."/".$newfilename);
		add_file($pid, $uname, $newfilename, $size, $date);
		$totalsize += $size;
		}
	update_thread_size($pid);
	flush_imports($tempsubdir);
	}

sub flush_imports
        {
        while ($tsd)
                {
                foreach my $d (recurse_imports($temp_path."/".$tsd."/"))
                        {
                        if (! -d $temp_path."/".$tsd."/".$d)
                                { print "Files still in $tsd, will not flush!<br>" if $DEBUG; return; }
                        }
                system($RM_PATH, "-rf", $temp_path."/".$tsd);
                return if ($tsd !~ /\//);
                $tsd =~ s/^(.*)\///;
                $tsd = $1;
                }
        }


sub list_imports
	{
	my $r = 0;
	my $i = 0;
	my $size = 0;
	my $inc = 0;
	my $title = "";
	my $files;

	print <<FILES;
<center>
<table border=0 cellpadding=0 cellspacing=0 class="main">
  <tr>
    <td class="head">&nbsp;</td>
    <td class="head" align="left" nowrap>&nbsp;<b>Name</b><img src="/blank.gif" height="1" width="200"></td>
	<td class="head"></td>
    <td class="head" align="left" nowrap><b>Date</b></td>
    <td class="head" align="right">&nbsp;<b>Size</b>&nbsp;</td>
  </tr>
FILES

	@files = recurse_imports($temp_path."/");

	foreach $file (sort @files)
		{
		if (-d $temp_path."/".$file)
			{
			if ($file =~ /$title\//)
				{
				$file =~ s/^.*\///;
				$title .= " - $file";
				}
			else
				{ $title = $file; }
			next;
			}
		my @stats = stat($temp_path."/".$file);
		$inc += display_import({filename => $file, date => $stats[9], size => $stats[7]}, $r, $i);
		$size += $stats[7];
		$r = $r ? 0 : 1;
		$i++;
		}

	print qq!<tr><td colspan=5 align="right" style="border-top: 1px dotted #ccc;">total size: !.(sprintf "%0.1fmb",$size/1000000).qq!</td></tr></table></center>\n\n!;

	return ($title, $inc, $size);
	}

sub recurse_imports
	{
	my $d = shift;
	return unless (-d $d);
	my @files;

	opendir (DIR, $d) or die "couldn't list: $d, $!";
	@files = grep (!/^\./, sort readdir (DIR));
	closedir DIR;

	foreach my $f (@files)
		{
		push @files,
			map { $f . "/" . $_ }
			recurse_imports($d.$f);
		}

	return @files;
	}

sub display_import
	{
	my ($f, $r, $i) = @_;
	my $color;
	my $inc = 0;

	if (abs(time - $$f{date}) < 5) # incomplete! use abs in case these are files from THE FUTURE!
		{ $color = "incomplete"; $inc = 1; }
	elsif ((time - $$f{date}) < 86400) # modified today
		{ $color = "new"; }
	elsif ((time - $$f{date}) < 604800) # modifed this week
		{ $color = "recent"; }
	elsif ((time - $$f{date}) < 1209600) # modifed 2 weeks ago
		{ $color = "old"; }
	else
		{ $color = "quiet"; }

	print qq[<tr class="row$r">];
	if ($color eq "incomplete")
		{
		print qq[<td align="right">&nbsp;</td>];
		print '<td></td>';
		print qq[<td align="left" nowrap>&nbsp;<i>$$f{filename}</i></td>];
		print qq[<td align="left" nowrap><i>]. (verbosedate($$f{date})), qq[</i></td>];
		print qq[<td align="right"><i>&nbsp;$$f{size}</i>&nbsp;</td>];
		}
	else
		{
		if ($f->{filename} =~ /^temp_/)
			{ print qq[<td align="right">&nbsp;</td>]; }
		else
			{ print qq[<td align="right"><input type="checkbox" name="import$i" value="$$f{filename}"></td>]; }	
		if ($f->{filename} =~ /jpg|gif|png$/)
			{
		#print "<td><img src='/bucky/data/incoming/$$f{filename}' width=150></td>";
			print '<td></td>';
			}
		else
			{
			print '<td></td>';
			}
		print qq[<td align="left" nowrap>&nbsp;];
		print qq[<a href="/bucky/data/incoming/$$f{filename}" target="_blank">$$f{filename}</a></td>];
		print qq[<td align="left" nowrap><span class="$color">]. (verbosedate($$f{date})), qq[</span></td>];
		print qq[<td align="right">&nbsp;$$f{size}&nbsp;</td>];
		}

	print qq[</tr>\n];

	return $inc;
	}

1;