1 |
3 |
ahitrov@rambler.ru |
package Contenido::File; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
use Contenido::Globals; |
7 |
|
|
use URI; |
8 |
|
|
use Data::Dumper; |
9 |
|
|
use IO::File; |
10 |
|
|
use IO::Scalar; |
11 |
|
|
use Contenido::File::Scheme::HTTP; |
12 |
|
|
use Contenido::File::Scheme::FILE; |
13 |
|
|
use Contenido::DateTime; |
14 |
|
|
|
15 |
|
|
our $IgnoreErrors = 1; |
16 |
|
|
|
17 |
|
|
sub fetch { |
18 |
|
|
my $filename = shift || return; |
19 |
|
|
my $fh; |
20 |
|
|
|
21 |
|
|
foreach my $dir (@{$state->{"files_dir"}}) { |
22 |
|
|
my $path = $dir . '/' . $filename; |
23 |
|
|
|
24 |
|
|
no strict "refs"; |
25 |
|
|
$fh = &{"Contenido::File::Scheme::".uc(scheme($path))."::fetch"}($path); |
26 |
|
|
return $fh if $fh; |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
sub store { |
31 |
|
|
my $filename = shift || return; |
32 |
|
|
my $fh = get_fh(shift) || return; |
33 |
|
|
|
34 |
|
|
my $dt = Contenido::DateTime->new()->set_locale('en')->set_time_zone("UTC"); |
35 |
|
|
|
36 |
|
|
my @successful; |
37 |
|
|
|
38 |
|
|
#������� ��������� � ����� / |
39 |
|
|
$filename =~ s#/+#/#g; |
40 |
|
|
#������� ��������� / |
41 |
|
|
$filename =~ s#^/##; |
42 |
|
|
|
43 |
|
|
foreach my $dir (@{$state->{"files_dir"}}) { |
44 |
|
|
seek $fh, 0, 0; |
45 |
|
|
my $path = $dir . '/' . $filename; |
46 |
|
|
|
47 |
|
|
no strict "refs"; |
48 |
|
|
my $return = &{"Contenido::File::Scheme::".uc(scheme($path))."::store"}($path, $fh, $dt); |
49 |
|
|
push @successful, $path if $return; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
if ( |
53 |
|
|
!@successful or |
54 |
|
|
( |
55 |
|
|
(scalar @successful != scalar @{$state->{"files_dir"}}) and |
56 |
|
|
!$IgnoreErrors |
57 |
|
|
) |
58 |
|
|
) { |
59 |
|
|
foreach my $path (@successful) { |
60 |
|
|
no strict "refs"; |
61 |
|
|
&{"Contenido::File::Scheme::".uc(scheme($path))."::remove"}($path); |
62 |
|
|
} |
63 |
|
|
return; |
64 |
|
|
} |
65 |
|
|
1; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
sub size { |
69 |
|
|
my $filename = shift; |
70 |
|
|
my %args = @_; |
71 |
|
|
|
72 |
|
|
my $size; |
73 |
|
|
|
74 |
|
|
#������� ��������� � ����� / |
75 |
|
|
$filename =~ s#/+#/#g; |
76 |
|
|
#������� ��������� / |
77 |
|
|
$filename =~ s#^/##; |
78 |
|
|
|
79 |
|
|
foreach my $dir (@{$state->{"files_dir"}}) { |
80 |
|
|
no strict "refs"; |
81 |
|
|
my $response = &{"Contenido::File::Scheme::".uc(scheme($dir))."::size"}($dir."/".$filename, %args); |
82 |
|
|
return unless defined $response; |
83 |
|
|
return if defined $size and $size != $response; |
84 |
|
|
|
85 |
|
|
$size = $response; |
86 |
|
|
#TODO |
87 |
|
|
last; |
88 |
|
|
} |
89 |
|
|
return $size; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
sub remove { |
93 |
|
|
my $filename = shift || return; |
94 |
|
|
|
95 |
|
|
#������� ��������� � ����� / |
96 |
|
|
$filename =~ s#/+#/#g; |
97 |
|
|
#������� ��������� / |
98 |
|
|
$filename =~ s#^/##; |
99 |
|
|
|
100 |
|
|
foreach my $dir (@{$state->{"files_dir"}}) { |
101 |
|
|
no strict "refs"; |
102 |
|
|
&{"Contenido::File::Scheme::".uc(scheme($dir))."::remove"}($dir."/".$filename); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
1; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
sub get_fh { |
109 |
|
|
my $input = shift; |
110 |
|
|
my $fh; |
111 |
|
|
|
112 |
|
|
if (not ref $input) { |
113 |
|
|
no strict "refs"; |
114 |
|
|
$fh = &{"Contenido::File::Scheme::".uc(scheme($input))."::get_fh"}($input); |
115 |
|
|
} elsif ((ref $input eq "GLOB") or (ref $input eq 'Apache::Upload') or (ref $input eq 'IO::File')) { |
116 |
|
|
$fh = $input; |
117 |
|
|
} elsif (ref $input eq "SCALAR") { |
118 |
|
|
$fh = IO::Scalar->new($input); |
119 |
|
|
} else { |
120 |
|
|
$log->warning("Path, scalar ref or fh needed"); |
121 |
|
|
return; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
return $fh |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
sub scheme { |
128 |
|
|
my $uri = shift; |
129 |
|
|
my $scheme; |
130 |
|
|
|
131 |
|
|
$scheme = URI->new($uri)->scheme() || "file"; |
132 |
|
|
|
133 |
|
|
return $scheme; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
# sub files_dir { |
137 |
|
|
# return unless $_[0]; |
138 |
|
|
# my $dir; |
139 |
|
|
|
140 |
|
|
# for (scheme($_[0])) { |
141 |
|
|
# /http/ && do { |
142 |
|
|
# $dir = URI->new($_[0])->canonical(); |
143 |
|
|
# $dir =~ s|/*$|/|; |
144 |
|
|
# last; |
145 |
|
|
# }; |
146 |
|
|
# /file/ && do { |
147 |
|
|
# $dir = URI->new($_[0])->path(); |
148 |
|
|
# $dir =~ s|/*$|/|; |
149 |
|
|
# last; |
150 |
|
|
# }; |
151 |
|
|
# } |
152 |
|
|
|
153 |
|
|
# return $dir; |
154 |
|
|
# } |
155 |
|
|
|
156 |
|
|
1; |