Added Mediafire::Api::File class

This commit is contained in:
vrag
2019-05-14 09:55:27 +03:00
parent f7c0be73f0
commit 5df8afddc4
6 changed files with 235 additions and 16 deletions

Binary file not shown.

View File

@@ -179,9 +179,104 @@ sub uploadFile {
-ua => $self->{ua},
-session_token => $self->{session_token},
);
$upload_file->uploadFile(%opt);
return $upload_file;
my $mediafire_file = $upload_file->uploadFile(%opt);
return $mediafire_file;
}
=pod Вынести в отдельный класс с проверкой наличия такой директории
sub createDir {
my ($self, %opt) = @_;
my $dirname = $opt{-dirname} // croak "You must specify '-dirname' param";
my $url = 'https://www.mediafire.com/api/1.4/folder/create.php';
my %param = (
'r' => 'sloe',
'foldername' => $dirname,
'parent_key' => '',
'session_token' => $self->{session_token},
'response_format' => 'json',
);
$url . = '?' . join('&', map {"$_=" . uri_escape($param{$_})} keys %param);
my $res = $self->{ua}->get($url);
my $code = $res->code;
if ($code ne '200') {
croak "Can't create dir '$dirname'. Code: $code";
}
my $json_res = decode_json($res->decoded_content);
p $json_res;
}
=cut
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
B<Mediafire::Api> - Upload and Download files from mediafire.com file sharing
=head1 VERSION
version 0.01
=head1 SYNOPSYS
=head1 METHODS
use Mediafire::Api;
# Create Mediafire::Api object
my $mediafire = Mediafire::Api->new();
# Login on service
$mediafire->login(
-login => $login,
-password => $password,
);
# Upload file to server
my $remote_dir = 'myfiles'; # Directory name on server
my $filename = '/tmp/test_file.zip'; # Full file path to upload
# Upload file on server. Return Mediafire::Api::UploadFile object
my $mediafire_file = $mediafire->uploadFile(
-file => $filename,
-path => $remote_dir,
);
# Get uploaded file key
print "Uploaded file key: " . $mediafire_file->getDouploadKey() . "\n";
=head1 Upload Files to server
=head2 new()
=head2 login(%opt)
=head1 DEPENDENCE
L<LWP::UserAgent>, L<JSON::XS>, L<URI::Escape>, L<Encode>, L<HTTP::Request>, L<Carp>, L<File::Basename>
=head1 AUTHORS
=over 4
=item *
Pavel Andryushin <vrag867@gmail.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2019 by Pavel Andryushin.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut

Binary file not shown.

54
lib/Mediafire/Api/File.pm Normal file
View File

@@ -0,0 +1,54 @@
package Mediafire::Api::File;
use 5.008001;
use utf8;
use strict;
use warnings;
our $VERSION = '0.01';
sub new {
my ($class, %opt) = @_;
my $self = {};
for my $field (qw/key size name hash/) {
$self->{$field} = $opt{"-$field"};
}
bless $self, $class;
return $self;
}
########### ACCESSORS #######################
sub key {
if (defined($_[1])) {
$_[0]->{key} = $_[1];
}
return $_[0]->{key};
}
sub size {
if (defined($_[1])) {
$_[0]->{size} = $_[1];
}
return $_[0]->{size};
}
sub name {
if (defined($_[1])) {
$_[0]->{name} = $_[1];
}
return $_[0]->{name};
}
sub hash {
if (defined($_[1])) {
$_[0]->{hash} = $_[1];
}
return $_[0]->{hash};
}
1;

View File

@@ -16,6 +16,8 @@ use Crypt::Digest::SHA256 qw/sha256_hex/;
use Time::HiRes qw/gettimeofday/;
use IO::Socket::SSL;
use Mediafire::Api::File;
use Data::Printer;
our $VERSION = '0.01';
@@ -40,9 +42,9 @@ my $checkUploadFile = sub {
my $microseconds = substr(join('', @sec), 0, 13);
my %param = (
'hash' => $self->{file_hash},
'size' => $self->{file_size},
'filename' => $self->{basename},
'hash' => $self->{file}->hash,
'size' => $self->{file}->size,
'filename' => $self->{file}->name,
'unit_size' => $self->{buff_size},
'resumable' => 'yes',
'preemptive' => 'yes',
@@ -79,8 +81,52 @@ my $checkUploadFile = sub {
p $response;
$self->{preemptive_quickkey} = $response->{preemptive_quickkey};
my $file_key = $response->{preemptive_quickkey} // $response->{duplicate_quickkey};
$self->{file}->key($file_key);
$self->{upload_url} = $response->{upload_url}->{resumable};
return $response;
};
my $getFileFromCache = sub {
my ($self) = @_;
my $url = 'https://www.mediafire.com/api/1.5/upload/instant.php';
my @sec = gettimeofday();
my $microseconds = substr(join('', @sec), 0, 13);
my %param = (
'hash' => $self->{file}->hash,
'size' => $self->{file}->size,
'filename' => $self->{file}->name,
'folder_key' => $self->{path},
'session_token' => $self->{session_token},
'response_format' => 'json',
$microseconds => '',
);
my $param_str = join('&', map {"$_=" . uri_escape($param{$_})} keys %param);
my $full_url = $url . '?' . $param_str;
my $res = $self->{ua}->get($full_url);
my $code = $res->code;
if ($code ne '200') {
croak "Wrong response code checkUploadFile(). Url: '$full_url'. Code: $code";
}
my $json_res = eval {
decode_json($res->decoded_content);
};
if ($@) {
croak "Can't parse respone '" . $res->decoded_content . "' to json";
}
# Get json response
my $response = $json_res->{response};
if ($response->{result} ne 'Success') {
croak "getFileFromCache() not success";
}
my $file_key = $response->{quickkey};
$self->{file}->key($file_key);
return 1;
};
@@ -146,10 +192,10 @@ my $uploadF = sub {
"Content-Type" => "application/octet-stream",
"Referer" => "https://www.mediafire.com/uploads",
"Origin" => "https://www.mediafire.com",
"X-Filesize" => $self->{file_size},
"X-Filename" => $self->{basename},
"X-Filesize" => $self->{file}->size,
"X-Filename" => $self->{file}->name,
"X-Filetype" => $file_type,
"X-Filehash" => $self->{file_hash},
"X-Filehash" => $self->{file}->hash,
"X-Unit-Hash" => $unit_hash,
"X-Unit-Size" => $bytes,
"X-Unit-Id" => $unit_id,
@@ -176,6 +222,7 @@ my $uploadF = sub {
if ($json_res->{response}->{resumable_upload}->{all_units_ready} eq 'yes') {
last;
}
p $json_res;
++$unit_id;
@@ -187,6 +234,8 @@ my $uploadF = sub {
croak "Not all parts of file '$upload_file' uploaded. Wrong answer from server";
}
p $json_res;
return 1;
};
@@ -214,14 +263,31 @@ sub uploadFile {
croak "File '" . $self->{upload_file} . "' not exists";
}
# Get all info about file
$self->{file_hash} = $getSha256Sum->($self->{upload_file});
$self->{file_size} = -s $self->{upload_file};
$self->{basename} = basename($self->{upload_file});
$self->{file} = Mediafire::Api::File->new(
-size => -s $self->{upload_file},
-name => basename($self->{upload_file}),
-hash => $getSha256Sum->($self->{upload_file}),
);
$self->$checkUploadFile();
$self->$checkResumeUpload();
$self->$uploadF();
# Get upload url
my $response = $self->$checkUploadFile();
if ($response->{hash_exists} eq 'yes') {
# No need upload file. Get file from cache
$self->$getFileFromCache();
}
else {
# Upload file
$self->$checkResumeUpload();
$self->$uploadF();
}
# Check exists file key
if (not defined($self->{file}->key)) {
croak "Key of upload file '$self->{upload_file}' not exists. Error on upload file to server";
}
return $self->{file};
}

View File

@@ -73,7 +73,11 @@ sub testUploadFile {
my ($mediafire, $file) = @_;
my $upload_file = $mediafire->uploadFile(
-file => $file,
-path => 'myfiles',
);
my $doupload_key = $upload_file->key;
ok($doupload_key, "Test upload file. DouploadKey: $doupload_key");
}