Added findFileByName() method

This commit is contained in:
vrag
2019-05-14 17:15:56 +03:00
parent 5df8afddc4
commit e2b970339d
5 changed files with 86 additions and 12 deletions

Binary file not shown.

View File

@@ -183,6 +183,57 @@ sub uploadFile {
return $mediafire_file; return $mediafire_file;
} }
sub findFileByName {
my ($self, %opt) = @_;
my $filename = $opt{-filename} // croak "You must specify '-filename' param";
my @mediafire_files;
my $url = 'https://www.mediafire.com/api/1.4/folder/search.php';
my %param = (
'r' => 'qzpa',
'search_text' => $filename,
'version' => '2.5',
'search_all' => 'yes',
'folder_key' => '',
'filter' => 'all',
'session_token' => $self->{session_token},
'response_format' => 'json',
);
my $full_url = $url . '?' . join('&', map {"$_=" . uri_escape($param{$_})} keys %param);
my $res = $self->{ua}->get($full_url);
my $code = $res->code;
if ($code ne '200') {
croak "Can't search file. Wrong response code on url '$full_url'. Code: $code";
}
my $json_res = eval {
decode_json($res->decoded_content);
};
if ($@) {
croak "Can't decode response to json. Response: '" . $res->decoded_content . "'";
}
for my $f (@{$json_res->{response}->{results}}) {
# If not file
if ($f->{type} ne 'file') {
next;
}
# If in Trash folder
if ($f->{parent_name} eq 'Trash') {
next;
}
push @mediafire_files,
Mediafire::Api::File->new(
-size => $f->{size},
-key => $f->{quickkey},
);
}
return \@mediafire_files;
}
=pod Вынести в отдельный класс с проверкой наличия такой директории =pod Вынести в отдельный класс с проверкой наличия такой директории
sub createDir { sub createDir {
my ($self, %opt) = @_; my ($self, %opt) = @_;
@@ -260,7 +311,7 @@ B<Mediafire::Api> - Upload and Download files from mediafire.com file sharing
=head1 DEPENDENCE =head1 DEPENDENCE
L<LWP::UserAgent>, L<JSON::XS>, L<URI::Escape>, L<Encode>, L<HTTP::Request>, L<Carp>, L<File::Basename> L<LWP::UserAgent>, L<JSON::XS>, L<URI::Escape>, L<Encode>, L<HTTP::Request>, L<Carp>, L<File::Basename>, L<MIME::Detect>
=head1 AUTHORS =head1 AUTHORS

Binary file not shown.

View File

@@ -12,6 +12,7 @@ use LWP::ConnCache;
use File::Basename; use File::Basename;
use HTTP::Request; use HTTP::Request;
use JSON::XS; use JSON::XS;
use MIME::Detect;
use Crypt::Digest::SHA256 qw/sha256_hex/; use Crypt::Digest::SHA256 qw/sha256_hex/;
use Time::HiRes qw/gettimeofday/; use Time::HiRes qw/gettimeofday/;
use IO::Socket::SSL; use IO::Socket::SSL;
@@ -26,14 +27,16 @@ our $VERSION = '0.01';
my $DEFAULT_BUFF_SIZE = 1048576; my $DEFAULT_BUFF_SIZE = 1048576;
############################ PRIVATE METHODS ############################################ ############################ PRIVATE METHODS ############################################
my $getSha256Sum = sub { my ($getSha256Sum, $checkUploadFile, $getFileFromCache, $checkResumeUpload, $getMimeType, $uploadF);
$getSha256Sum = sub {
my ($fname) = @_; my ($fname) = @_;
my $sha = Crypt::Digest::SHA256->new(); my $sha = Crypt::Digest::SHA256->new();
$sha->addfile($fname); $sha->addfile($fname);
return $sha->hexdigest; return $sha->hexdigest;
}; };
my $checkUploadFile = sub { $checkUploadFile = sub {
my ($self) = @_; my ($self) = @_;
my $url = 'https://www.mediafire.com/api/1.5/upload/check.php'; my $url = 'https://www.mediafire.com/api/1.5/upload/check.php';
@@ -87,7 +90,7 @@ my $checkUploadFile = sub {
return $response; return $response;
}; };
my $getFileFromCache = sub { $getFileFromCache = sub {
my ($self) = @_; my ($self) = @_;
my $url = 'https://www.mediafire.com/api/1.5/upload/instant.php'; my $url = 'https://www.mediafire.com/api/1.5/upload/instant.php';
@@ -130,7 +133,7 @@ my $getFileFromCache = sub {
return 1; return 1;
}; };
my $checkResumeUpload = sub { $checkResumeUpload = sub {
my ($self) = @_; my ($self) = @_;
my $ua = $self->{ua}; my $ua = $self->{ua};
@@ -163,7 +166,7 @@ my $checkResumeUpload = sub {
}; };
# Upload file # Upload file
my $uploadF = sub { $uploadF = sub {
my ($self) = @_; my ($self) = @_;
my $upload_file = $self->{upload_file}; my $upload_file = $self->{upload_file};
@@ -176,7 +179,6 @@ my $uploadF = sub {
my $param_str = join('&', map {"$_=" . uri_escape($param{$_})} keys %param); my $param_str = join('&', map {"$_=" . uri_escape($param{$_})} keys %param);
my $url = $self->{upload_url} . '?' . $param_str; my $url = $self->{upload_url} . '?' . $param_str;
my $file_type = 'application/zip';
my $unit_id = 0; my $unit_id = 0;
my $filebuf; my $filebuf;
open my $FH, "<$upload_file" or croak "Can't open $upload_file $!"; open my $FH, "<$upload_file" or croak "Can't open $upload_file $!";
@@ -194,7 +196,7 @@ my $uploadF = sub {
"Origin" => "https://www.mediafire.com", "Origin" => "https://www.mediafire.com",
"X-Filesize" => $self->{file}->size, "X-Filesize" => $self->{file}->size,
"X-Filename" => $self->{file}->name, "X-Filename" => $self->{file}->name,
"X-Filetype" => $file_type, "X-Filetype" => $getMimeType->($self->{file}->name),
"X-Filehash" => $self->{file}->hash, "X-Filehash" => $self->{file}->hash,
"X-Unit-Hash" => $unit_hash, "X-Unit-Hash" => $unit_hash,
"X-Unit-Size" => $bytes, "X-Unit-Size" => $bytes,
@@ -222,10 +224,8 @@ my $uploadF = sub {
if ($json_res->{response}->{resumable_upload}->{all_units_ready} eq 'yes') { if ($json_res->{response}->{resumable_upload}->{all_units_ready} eq 'yes') {
last; last;
} }
p $json_res;
++$unit_id; ++$unit_id;
} }
close $FH; close $FH;
@@ -234,11 +234,20 @@ my $uploadF = sub {
croak "Not all parts of file '$upload_file' uploaded. Wrong answer from server"; croak "Not all parts of file '$upload_file' uploaded. Wrong answer from server";
} }
p $json_res;
return 1; return 1;
}; };
$getMimeType = sub {
my ($fname) = @_;
my $default_mime = 'application/zip';
my $mime = MIME::Detect->new();
my @types = $mime->mime_types_from_name($fname);
if (@types) {
return $types[0]->mime_type;
}
return $default_mime;
};
######################################################################################## ########################################################################################

View File

@@ -19,6 +19,7 @@ use utf8;
use strict; use strict;
use warnings; use warnings;
use File::Spec; use File::Spec;
use File::Basename;
my $CURR_DIR; my $CURR_DIR;
BEGIN { BEGIN {
@@ -53,6 +54,7 @@ SKIP: {
} }
testUploadFile($mediafire, $UPLOAD_FILE); testUploadFile($mediafire, $UPLOAD_FILE);
testFindFileByName($mediafire, basename($UPLOAD_FILE));
}; };
@@ -80,4 +82,16 @@ sub testUploadFile {
ok($doupload_key, "Test upload file. DouploadKey: $doupload_key"); ok($doupload_key, "Test upload file. DouploadKey: $doupload_key");
} }
sub testFindFileByName {
my ($mediafire, $filename) = @_;
my $res = $mediafire->findFileByName(
-filename => $filename,
);
ok (@$res, "Test findFileByName ok");
my $doupload_key = $res->[0]->key;
ok($doupload_key, "Find file doupload_key: $doupload_key");
}