Line # Revision Author
1 3 ahitrov@rambler.ru package Contenido::File::Scheme::FILE;
2
3 use strict;
4 use warnings;
5
6 use Contenido::Globals;
7 use Data::Dumper;
8
9 sub store {
10 my $path = shift || return;
11 my $fh = shift || return;
12
13 local $/ = undef;
14
15 my $content = <$fh>;
16
17 make_dir_recursive($path);
18
19 unless (open FILE, ">$path") {
20 warn $!;
21 return;
22 }
23
24 print FILE $content;
25 close FILE;
26
27 return 1;
28 }
29
30 sub remove {
31 my $path = shift;
32
33 unlink $path or warn $! and return;
34 return 1;
35 }
36
37 sub size {
38 my $path = shift;
39
40 return unless -r $path and -f $path;
41 return (stat($path))[7];
42 }
43
44 sub listing {
45 my $path = shift;
46 my $files = {};
47
48 return unless Contenido::File::scheme($path) eq "file";
49 return unless -e $path and -d $path;
50
51 opendir my $dir, $path || return; #warn "can't opendir $path: $!";
52
53 foreach my $file (grep { -f "$path/$_" } readdir $dir) {
54 $files->{$file} = Contenido::File::Scheme::FILE::size($path."/".$file);
55 }
56
57 closedir $dir;
58
59 return $files;
60 }
61
62 sub make_dir_recursive {
63 my $path = shift;
64
65 my @parts = grep {$_} split "/", $path;
66
67 for (0 .. $#parts - 1) {
68 my $dir = "/".join("/", @parts[0 .. $_]);
69 next if -d $dir;
70 mkdir $dir or die "Can't create directory $dir: $!";
71 }
72 }
73
74 sub get_fh {
75 my $path = shift;
76
77 my $fh = IO::File->new($path) or do { warn "can't open file [$path] for reading"; return; };
78 return $fh;
79 }
80
81 1;