| 1 | # P2P::pDonkey::Meta_v04.pm | ||
| 2 | # | ||
| 3 | # Copyright (c) 2003-2004 Alexey klimkin <klimkin at cpan.org>. | ||
| 4 | # All rights reserved. | ||
| 5 | # This program is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | # | ||
| 8 | package P2P::pDonkey::Meta_v04; | ||
| 9 | |||
| 10 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | |||
| 16 | our $VERSION = '0.05'; | ||
| 17 | |||
| 18 | our @ISA = qw(Exporter); | ||
| 19 | |||
| 20 | our %EXPORT_TAGS = | ||
| 21 | ( 'all' => [ qw( | ||
| 22 | unpackFileInfo_v04 packFileInfo_v04 makeFileInfo_v04 | ||
| 23 | unpackFileInfoList_v04 packFileInfoList_v04 | ||
| 24 | ) ] | ||
| 25 | ); | ||
| 26 | |||
| 27 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 28 | |||
| 29 | our @EXPORT = qw( | ||
| 30 | |||
| 31 | ); | ||
| 32 | |||
| 33 | 1 | ▶ | use File::Glob ':glob'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 1 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 0 | ▶ | use P2P::pDonkey::Meta ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 36 | |||
| 37 | my $debug = 0; | ||
| 38 | |||
| 39 | sub unpackFileInfo_v04 { | ||
| 40 | my (%res, $metas, %tags, @gaps); | ||
| 41 | defined($res{Date} = &unpackD) or return; | ||
| 42 | defined($res{Hash} = &unpackHash) or return; | ||
| 43 | $res{Parts} = &unpackHashList or return; | ||
| 44 | $metas = &unpackMetaList or return; | ||
| 45 | |||
| 46 | tie %tags, "Tie::IxHash"; | ||
| 47 | foreach my $meta (@$metas) { | ||
| 48 | if ($meta->{Type} == TT_GAPSTART || $meta->{Type} == TT_GAPEND) { | ||
| 49 | push @gaps, $meta->{Value}; | ||
| 50 | } else { | ||
| 51 | $tags{$meta->{Name}} = $meta; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | $res{Gaps} = [sort {$a <=> $b} @gaps]; | ||
| 55 | $res{Meta} = \%tags; | ||
| 56 | return \%res; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub packFileInfo_v04 { | ||
| 60 | my ($d) = @_; | ||
| 61 | my ($res, $metas); | ||
| 62 | $res = packD($d->{Date}) . packHash($d->{Hash}) . packHashList($d->{Parts}); | ||
| 63 | $metas = MetaListU2MetaList($d->{Meta}); | ||
| 64 | if ($d->{Gaps} and @{$d->{Gaps}}) { | ||
| 65 | my $gaps = $d->{Gaps}; | ||
| 66 | my $ngaps = @$gaps / 2; | ||
| 67 | for (my ($i, $n) = (0, 0); $i < $ngaps; $i += 2, $n++) { | ||
| 68 | push @$metas, makeMeta(TT_GAPSTART, $gaps->[$i], $n); | ||
| 69 | } | ||
| 70 | for (my ($i, $n) = (0, 0); $i < $ngaps; $i += 2, $n++) { | ||
| 71 | push @$metas, makeMeta(TT_GAPEND, $gaps->[$i+1], $i); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | $res .= packMetaList($metas); | ||
| 75 | return $res; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub unpackFileInfoList_v04 { | ||
| 79 | my ($nres, @res, $info); | ||
| 80 | defined($nres = &unpackD) or return; | ||
| 81 | @res = (); | ||
| 82 | while ($nres--) { | ||
| 83 | $info = &unpackFileInfo_v04 or return; | ||
| 84 | push @res, $info; | ||
| 85 | } | ||
| 86 | return \@res; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub packFileInfoList_v04 { | ||
| 90 | my ($l) = @_; | ||
| 91 | my ($res, $info); | ||
| 92 | $res = packD(scalar @$l);; | ||
| 93 | foreach $info (@$l) { | ||
| 94 | $res .= packFileInfo_v04($info); | ||
| 95 | } | ||
| 96 | return $res; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub makeFileInfo_v04 { | ||
| 100 | my ($path) = @_; | ||
| 101 | my ($base, $ext); | ||
| 102 | my ($context, %meta, $hash, $type); | ||
| 103 | |||
| 104 | $path = bsd_glob($path, GLOB_TILDE); | ||
| 105 | print $path, "\n"; | ||
| 106 | |||
| 107 | (-e $path && -r _) or return; | ||
| 108 | |||
| 109 | print "Making info for $path\n" if $debug; | ||
| 110 | |||
| 111 | # my $vinfo = Video::Info->new(-file => $path); | ||
| 112 | # if ($vinfo->type()) { | ||
| 113 | # print $vinfo->filename, "\n"; | ||
| 114 | # print $vinfo->filesize(), "\n"; | ||
| 115 | # print $vinfo->type(), "\n"; | ||
| 116 | # print $vinfo->duration(), "\n"; | ||
| 117 | # print $vinfo->minutes(), "\n"; | ||
| 118 | # print $vinfo->MMSS(), "\n"; | ||
| 119 | # print $vinfo->geometry(), "\n"; | ||
| 120 | # print $vinfo->title(), "\n"; | ||
| 121 | # print $vinfo->author(), "\n"; | ||
| 122 | # print $vinfo->copyright(), "\n"; | ||
| 123 | # print $vinfo->description(), "\n"; | ||
| 124 | # print $vinfo->rating(), "\n"; | ||
| 125 | # print $vinfo->packets(), "\n"; | ||
| 126 | # } | ||
| 127 | |||
| 128 | ($base, undef, $ext) = fileparse($path, '\..*'); | ||
| 129 | $ext = unpack('xa*', $ext) if $ext; # skip first '.' | ||
| 130 | if ($ext) { | ||
| 131 | my %ft = qw(mp3 Audio avi Video gif Image iso Pro doc Doc); | ||
| 132 | $type = $ft{lc $ext}; | ||
| 133 | } | ||
| 134 | |||
| 135 | my ($size, $date); | ||
| 136 | $size = (stat _)[7]; | ||
| 137 | $date = (stat _)[9]; | ||
| 138 | |||
| 139 | tie %meta, "Tie::IxHash"; | ||
| 140 | $meta{Name} = makeMeta(TT_NAME, "$base.$ext"); | ||
| 141 | $meta{Size} = makeMeta(TT_SIZE, $size); | ||
| 142 | $meta{Type} = makeMeta(TT_TYPE, $type) if $type; | ||
| 143 | $meta{Format} = makeMeta(TT_FORMAT, $ext) if $ext; | ||
| 144 | |||
| 145 | open(HANDLE, $path) or return; | ||
| 146 | binmode(HANDLE); | ||
| 147 | |||
| 148 | $context = new Digest::MD4; | ||
| 149 | |||
| 150 | my @parts = (); | ||
| 151 | if ($size > SZ_FILEPART) { | ||
| 152 | seek(HANDLE, 0, 0); | ||
| 153 | my ($nparts, $part); | ||
| 154 | $nparts = ceil($size / SZ_FILEPART); | ||
| 155 | for (my $i = 0; $i < $nparts; $i++) { | ||
| 156 | read(HANDLE, $part, SZ_FILEPART); | ||
| 157 | push @parts, md4_hex($part); | ||
| 158 | $context->add($part); | ||
| 159 | } | ||
| 160 | } else { | ||
| 161 | $context->addfile(\*HANDLE); | ||
| 162 | } | ||
| 163 | $hash = $context->hexdigest; | ||
| 164 | |||
| 165 | close HANDLE; | ||
| 166 | |||
| 167 | return {Date => $date, Hash => $hash, Parts => \@parts, Meta => \%meta, Path => $path}; | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; | ||