Skip to content

Commit

Permalink
fixed MEGA upload-remote
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas0610 committed Aug 9, 2017
1 parent 5af8e7b commit 3c32ad2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
65 changes: 59 additions & 6 deletions core/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ function __mkdir($name) {
__exec("mkdir -p {$name}");
}

function __exec__no_assert($cmdline, $censoring = array( )) {
function __exec__internal($blocking, $cmdline, $censoring = array( )) {
$output = array( );
$rc = 0;
$dcmdline = $cmdline;
$tempfile = "";
$is_external = (strpos($cmdline, "\n") !== false);

if (strpos($cmdline, "\n") !== false) {
if ($is_external) {
$tempfile = tempnam(sys_get_temp_dir(), "aabs-exec-");
file_put_contents($tempfile, $cmdline);
chmod($tempfile, 0777);
Expand All @@ -22,20 +24,71 @@ function __exec__no_assert($cmdline, $censoring = array( )) {
}

echo "{$dcmdline}\n";
system("{$cmdline}", $rc);


if ($blocking) {
passthru($cmdline, $rc);
} else {
$descriptorspec = array(
0 => array( "pipe", "r" ), // stdin is a pipe that the child will read from
1 => array( "pipe", "w" ), // stdout is a pipe that the child will write to
2 => array( "pipe", "w" ) // stderr is a pipe that the child will write to
);
$process = proc_open("{$cmdline}", $descriptorspec, $pipes, realpath('./'), array());

if (is_resource($process)) {
// all pipes to non-blocking
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);

while ($line = fgets($pipes[1])) {
print($line);
flush();
}
$rc = proc_close($process);
} else {
$rc = 1;
}
}

if ($is_external && $tempfile != "") {
unlink($tempfile);
}

return $rc;
}

function __exec__internal__blocking($cmdline, $censoring = array( )) {
return __exec__internal(true, $cmdline, $censoring);
}

function __exec__internal__non_blocking($cmdline, $censoring = array( )) {
return __exec__internal(false, $cmdline, $censoring);
}

function __exec($cmdline, $censoring = array( )) {
$rc = __exec__no_assert($cmdline, $censoring);
$rc = __exec__internal__blocking($cmdline, $censoring);
if ($rc != 0) {
die("Previous command failed with {$rc}\n");
}
}

function __exec__non_blocking($cmdline, $censoring = array( )) {
$rc = __exec__internal__non_blocking($cmdline, $censoring);
if ($rc != 0) {
die("Previous command failed with {$rc}\n");
}
}

function __exec__allow_single_error($cmdline, $code, $censoring = array( )) {
$rc = __exec__no_assert($cmdline, $censoring);
$rc = __exec__internal__blocking($cmdline, $censoring);
if ($rc != 0 && $rc != $code) {
die("Previous command failed with {$rc}\n");
}
}

function __exec__allow_single_error__non_blocking($cmdline, $code, $censoring = array( )) {
$rc = __exec__internal__non_blocking($cmdline, $censoring);
if ($rc != 0 && $rc != $code) {
die("Previous command failed with {$rc}\n");
}
Expand Down
29 changes: 18 additions & 11 deletions remote/mega.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ function upload_to_mega($data) {
$outputfile = $data['output']['file'];
$uploaddir = $data['upload']['dir'];
$uploadfile = $data['upload']['file'];


// create temporary PID file
/* $megapid = tempnam(sys_get_temp_dir(), "aabs-megacmd-pid-");
file_put_contents($megapid, ""); */

// check for megacmd
__exec("command -v mega-cmd >/dev/null 2>&1 || exit 1");


// start megacmd
/* __exec__non_blocking(
"#!/bin/bash" . "\n" .
"mega-cmd &" . "\n" .
"echo $! > \"{$megapid}\""
); */

// login
$user = str_replace("\"", "\\\"", $user);
$pass = str_replace("\"", "\\\"", $pass);
__exec__allow_single_error("mega-login \"{$user}\" \"{$pass}\"", 202, array( $pass )); /* Allow "Already logged in."-error */

// rename output file
$new_output = $outputdir . '/' . $uploadfile;
__exec("mv {$output} {$new_output}");


// upload file
__exec("mega-put {$new_output} -c {$uploaddir}");
// rename it back
__exec("mv {$new_output} {$output}");
__exec("mega-put {$output} -c {$uploaddir}/{$uploadfile}");

// start megacmd
/* __exec("kill " . file_get_contents($megapid)); */
}

0 comments on commit 3c32ad2

Please sign in to comment.