diff --git a/inc/core.php b/inc/core.php index 0cbd1aa..8b95d79 100644 --- a/inc/core.php +++ b/inc/core.php @@ -145,9 +145,12 @@ function storageControllerUpload($hash) { // Lets' check all storage controllers and tell them that a new file was uploaded $sc = getStorageControllers(); + $allgood = true; + $uploadedhash =$hash; foreach($sc as $contr) { - if((new $contr())->isEnabled()===true) + $controller = new $contr(); + if($controller->isEnabled()===true) { $source = ROOT.DS.'data'.DS.$hash.DS.$hash; if(defined('ENCRYPTION_KEY') && ENCRYPTION_KEY) //ok so we got an encryption key which means we'll store only the encrypted file @@ -155,14 +158,44 @@ function storageControllerUpload($hash) $enc = new Encryption; $encoded_file = ROOT.DS.'tmp'.DS.$hash.'.enc'; $enc->encryptFile($source,$encoded_file,base64_decode(ENCRYPTION_KEY)); - (new $contr())->pushFile($encoded_file,$hash.'.enc'); + $controller->pushFile($encoded_file,$hash.'.enc'); + unlink($encoded_file); + $uploadedhash = $hash.'.enc'; } else // not encrypted - (new $contr())->pushFile($source,$hash); - + $controller->pushFile($source,$hash); + + //let's check if the file is really there. If not, queue it for later + if(!$controller->hashExists($uploadedhash)) + { + $allgood = false; + $queuefile=ROOT.DS.'tmp'.DS.'controllerqueue.txt'; + if(!file_exists($queuefile) || !stringInFile($hash,$queuefile)) + { + $fp=fopen($queuefile,'a'); + if($fp) + { + fwrite($fp,$hash."\n"); + fclose($fp); + } + } + } } } + + return $allgood; +} + +function stringInFile($string,$file) +{ + $handle = fopen($file, 'r'); + while (($line = fgets($handle)) !== false) { + $line=trim($line); + if($line==$string) return true; + } + fclose($handle); + return false; } function getNewHash($type,$length=10) diff --git a/storage-controllers/s3.controller.php b/storage-controllers/s3.controller.php index 06f80c0..d1326db 100644 --- a/storage-controllers/s3.controller.php +++ b/storage-controllers/s3.controller.php @@ -13,7 +13,7 @@ class S3Storage implements StorageController { private $s3; function connect(){ - require ROOT.DS.'storage-controllers'.DS.'s3'.DS.'aws-autoloader.php'; + require_once(ROOT.DS.'storage-controllers'.DS.'s3'.DS.'aws-autoloader.php'); $this->s3 = new Aws\S3\S3Client([ 'version' => 'latest', 'region' => 'us-east-1', diff --git a/tools/cron.php b/tools/cron.php new file mode 100644 index 0000000..1c92ab0 --- /dev/null +++ b/tools/cron.php @@ -0,0 +1,46 @@ + SUCCESS. Removing from queue':'FAILED. Will be re-added to queue')."\n"; + if(!$success) + $newqueue[]=$hash; + } + } + + file_put_contents($queuefile,implode("\n",$newqueue)); +} \ No newline at end of file