Edit File: FileUploader.inc
<?php /** * Author: Shlomi * Date: Sep 5, 2017 10:18:08 AM * Founder: Shlomi Bazak (SBZsoft) (c) * File: FileUploader.inc * * Description: * */ namespace B2\Entities; use B2\Exceptions\FileNotPresentException; use B2\Http\B2Client; use B2\FS\FileReader; use B2\Exceptions\UploadFailedException; // No direct access. defined("__NSS__") or die("Restrected Access."); class FileUploader { const CHUNK_SIZE = 1000000000; /** @var B2Client */ private $_client; public function __construct(B2Client $client){ $this->_client = $client; } /** * * @param FileReader $reader * @param Bucket $bucket * @param String $path * @param int $retries * @throws \Exception if faild after retries * @return \B2\Entities\File */ private function _uploadSmallFile(FileReader $reader, Bucket $bucket, $path, $retries){ $params = [ 'bucketId' => $bucket->getId() ]; //$body = $reader->getContents(); $hash = sha1_file($reader->getName()); $headers = [ 'Content-Type' => $reader->getConetnType(), 'Content-Length' => $reader->getSize(), 'X-Bz-File-Name' => $path, 'X-Bz-Content-Sha1' => $hash, 'X-Bz-Info-src_last_modified_millis' => $reader->getModificationTime() * 1000 ]; $max=0; $success=false; $last_exception=null; while(!$success && $max++<=$retries){ try { $data = $this->_client->requestApi(B2Api::GET_UPLOAD_URL, $params); $headers['Authorization'] = $data['authorizationToken']; $data = $this->_client->_requestJson( B2Client::METHOD_POST, $data['uploadUrl'], [ 'headers' => $headers, 'body' => $reader->getFileHandle() ] ); $success = true; }catch(\Exception $e){ $last_exception = $e; } gc_collect_cycles(); // Memory leak .. Fix needed in PHP level of nasted functions and recursion. } if(!$success) { if($last_exception) throw $last_exception; throw new UploadFailedException("Upload failed for {$reader->getName()}."); } return new File($data); } /** * Upload file to BB. * @param String $localFile * @param Bucket $bucket * @param String $path * @param int $retries * @throws FileNotPresentException * @return \B2\Entities\File */ public function Upload($localFile, Bucket $bucket, $path, $retries){ if(!file_exists($localFile)) throw new FileNotPresentException("Local file ($localFile) not found."); $reader = new FileReader($localFile); $reader->setChunkSize(self::CHUNK_SIZE); if($reader->getSize() > self::CHUNK_SIZE){ $chunkUploader = new ChunkFileUploader($this->_client, $reader, $bucket, $path, $retries); return $chunkUploader->upload(); } return $this->_uploadSmallFile($reader, $bucket, $path, $retries); } }