| 1 | package AnyData::Storage::FileSys; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use File::Find; | ||
| 6 | use File::Basename; | ||
| 7 | use vars qw( @ISA @files $wanted_part $wanted_re ); | ||
| 8 | use AnyData::Storage::File; | ||
| 9 | @ISA = qw( AnyData::Storage::File ); | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | sub open_table {} | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my $self = shift || {}; | ||
| 17 | $self->{col_names} = ['fullpath','path','name','ext','size','content' ]; | ||
| 18 | bless $self, $class; | ||
| 19 | my $exts = $self->{exts}; | ||
| 20 | if ($exts) { | ||
| 21 | $self->{wanted_part} = 'ext'; | ||
| 22 | $self->{wanted_re} = qr/\.$exts$/; | ||
| 23 | } | ||
| 24 | $self->{records} = $self->get_data; | ||
| 25 | $self->{index} = 0; | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | sub is_deleted {} | ||
| 29 | |||
| 30 | sub get_data { | ||
| 31 | my $self = shift; | ||
| 32 | my $dirs = shift || $self->{dirs}; | ||
| 33 | my @col_names = @{ $self->{col_names} }; | ||
| 34 | my $table = []; | ||
| 35 | my @files = $self->get_filename_parts; | ||
| 36 | for my $file_info(@files) { | ||
| 37 | my $file = $file_info->[0]; | ||
| 38 | # my $cols = get_mp3_tag($file) || next; | ||
| 39 | #my $filesize = -s $file; | ||
| 40 | #my @row = (@$file_info,$filesize); | ||
| 41 | my @row = ( $file_info->[0], | ||
| 42 | $file_info->[2], | ||
| 43 | $file_info->[1], | ||
| 44 | $file_info->[3], | ||
| 45 | ); | ||
| 46 | push @$table, \@row; | ||
| 47 | # 'fullpath,path,name,ext,size,content'; | ||
| 48 | # 'fullpath,file_name,path,ext,size,' | ||
| 49 | # 'name,artist,album,year,comment,genre'; | ||
| 50 | } | ||
| 51 | #use Data::Dumper; print "!",Dumper $table; exit; | ||
| 52 | return $table; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub seek_first_record { | ||
| 56 | my $self = shift; | ||
| 57 | $self->{index} = 0; | ||
| 58 | } | ||
| 59 | sub file2str { | ||
| 60 | my $self = shift; | ||
| 61 | my $curindex = $self->{index}; | ||
| 62 | return undef if $curindex >= scalar @{$self->{records}}; | ||
| 63 | $self->{index}++; | ||
| 64 | my $rec = $self->{records}->[$curindex]; | ||
| 65 | my $file = $rec->[0]; | ||
| 66 | push @$rec, -s $file; | ||
| 67 | local $/; | ||
| 68 | undef $/; | ||
| 69 | my $fh = $self->open_local_file( $file, $self->{open_mode}); | ||
| 70 | my $str = <$fh>; | ||
| 71 | undef $fh; | ||
| 72 | push @$rec, $str; | ||
| 73 | return $rec; | ||
| 74 | } | ||
| 75 | sub col_names { shift->{col_names} } | ||
| 76 | sub get_filename_parts { | ||
| 77 | my $self = shift; | ||
| 78 | my %flags; | ||
| 79 | %flags = @_ if scalar @_; | ||
| 80 | #use Data::Dumper; print "!",Dumper \%flags; exit; | ||
| 81 | $wanted_part = $flags{part} || $self->{wanted_part} || ''; | ||
| 82 | $wanted_re = $flags{re} || $self->{wanted_re} || ''; | ||
| 83 | my $dirs = $flags{dirs} || $self->{dirs} || []; | ||
| 84 | my $wanted_sub = $flags{sub} || \&wanted; | ||
| 85 | @files = (); | ||
| 86 | find { no_chdir => 1, | ||
| 87 | wanted => $wanted_sub, | ||
| 88 | }, | ||
| 89 | @$dirs; | ||
| 90 | ; | ||
| 91 | my @results = @files; | ||
| 92 | @files = (); | ||
| 93 | return @results; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub wanted { | ||
| 97 | my @info = fileparse($_,'\.[^\.]*$'); | ||
| 98 | my($name,$path,$ext) = map{$_ || ''} @info; | ||
| 99 | if (!$name && $ext) { $name = $ext; $ext = ''; } | ||
| 100 | unshift @info,$File::Find::name; | ||
| 101 | my $cols; | ||
| 102 | @{$cols}{('fullpath','filename','path','ext')} = @info; | ||
| 103 | if ($wanted_part && $wanted_re) { | ||
| 104 | return unless $cols->{$wanted_part} =~ $wanted_re; | ||
| 105 | } | ||
| 106 | push @files, \@info; | ||
| 107 | } | ||
| 108 | 1; |