our
$VERSION
=
'0.01'
;
my
$MEGA_CMD
= {
'mega_login'
=>
'mega-login'
,
'mega_logout'
=>
'mega-logout'
,
'mega_mkdir'
=>
'mega-mkdir'
,
'mega_put'
=>
'mega-put'
,
'mega_get'
=>
'mega-get'
,
'mega_export'
=>
'mega-export'
,
};
my
$DEFAULT_PATH
=
'/usr/bin'
;
my
$DEFAULT_LOCK_PORT
= 40000;
sub
new {
my
(
$class
,
%opt
) =
@_
;
my
$self
= {};
$self
->{path} =
$opt
{-path} //
$DEFAULT_PATH
;
$self
->{lock_port} =
$opt
{-lock_port} //
$DEFAULT_LOCK_PORT
;
$self
->{
lock
} = Lock::Socket->new(
port
=>
$self
->{lock_port});
eval
{
$self
->{
lock
}->
lock
;
};
if
($@) {
croak
"Can't lock port $self->{lock_port}"
;
}
for
my
$cmd
(
keys
%$MEGA_CMD
) {
my
$cmd_path
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{
$cmd
});
if
(not -f
$cmd_path
) {
croak
"Command: '$MEGA_CMD->{$cmd}' not found in path: '$cmd_path'"
;
}
}
bless
$self
,
$class
;
return
$self
;
}
sub
login {
my
(
$self
,
%opt
) =
@_
;
$self
->{login} =
$opt
{-login} // croak
"You must specify '-login' param"
;
$self
->{password} =
$opt
{-password} // croak
"You must specify '-password' param"
;
$self
->logout();
my
$cmd
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{mega_login});
my
$login_res
= `
$cmd
'$self->{login}'
'$self->{password}'
`;
if
(
$login_res
) {
croak
"Can't login to mega: $login_res"
;
}
return
1;
}
sub
uploadFile {
my
(
$self
,
%opt
) =
@_
;
my
$local_file
=
$opt
{-local_file} // croak
"You must specify '-local_file' param"
;
my
$remote_file
=
$opt
{-remote_file} // croak
"You must specify '-remote_file' param"
;
my
$create_dir
=
$opt
{-create_dir};
my
$param
=
$create_dir
?
'-c '
:
''
;
my
$cmd
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{mega_put});
my
$res
= `
$cmd
$param
'$local_file'
'$remote_file'
`;
if
(
$res
) {
croak
"Can't upload file: '$local_file' to '$remote_file'. Error: $res"
;
}
return
1;
}
sub
downloadFile {
my
(
$self
,
%opt
) =
@_
;
my
$local_file
=
$opt
{-local_file} // croak
"You must specify '-local_file' param"
;
my
$remote_file
=
$opt
{-remote_file} // croak
"You must specify '-remote_file' param"
;
my
$cmd
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{mega_get});
my
$res
= `
$cmd
'$remote_file'
'$local_file'
`;
if
(
$res
) {
croak
"Can't download file: '$remote_file' to '$local_file'. Error: $res"
;
}
return
1;
}
sub
createDir {
my
(
$self
,
%opt
) =
@_
;
my
$dir
=
$opt
{-dir} // croak
"You must specify '-dir' param"
;
my
$cmd
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{mega_mkdir});
my
$create_dir_res
= `
$cmd
-p
'$dir'
`;
if
(
$create_dir_res
) {
croak
"Can't create folder: $dir. Error: $create_dir_res"
;
}
return
1;
}
sub
shareResource {
my
(
$self
,
%opt
) =
@_
;
$opt
{-action} =
'-a'
;
my
$res
=
$self
->__share(
%opt
);
if
(
$res
=~ /^Exported.+(https:\/\/.+)$/) {
return
$1;
}
croak
"Can't share resource '$opt{-remote_resource}'. Error: $res"
;
}
sub
unshareResource {
my
(
$self
,
%opt
) =
@_
;
$opt
{-action} =
'-d'
;
my
$res
=
$self
->__share(
%opt
);
if
(
$res
=~ /^Disabled export/) {
return
1;
}
croak
"Can't unshare resource: '$opt{-remote_resource}'. Error: $res"
;
}
sub
__share {
my
(
$self
,
%opt
) =
@_
;
my
$remote_resource
=
$opt
{-remote_resource} // croak
"You must specify param '-remote_resource'"
;
my
$action
=
$opt
{-action};
my
$cmd
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{mega_export});
my
$res
= `
$cmd
$action
-f
'$remote_resource'
`;
return
$res
;
}
sub
logout {
my
(
$self
) =
@_
;
my
$cmd
= File::Spec->catfile(
$self
->{path},
$MEGA_CMD
->{mega_logout});
`
$cmd
`;
return
1;
}
sub
DESTROY {
my
(
$self
) =
@_
;
$self
->logout;
if
(
$self
->{
lock
}) {
$self
->{
lock
}->unlock;
}
}
1;