Edit File: B2.inc
<?php /** * Author: Shlomi * Date: Sep 4, 2017 10:11:00 AM * Founder: Shlomi Bazak (SBZsoft) (c) * File: B2.inc * * Description: * */ namespace B2; use B2\Http\B2Client; use B2\Entities\Bucket; use B2\Entities\BucketManager; use B2\Entities\FileManager; use B2\Exceptions\B2Exception; // No direct access. defined("__NSS__") or die("Restrected Access."); class B2 { private $_client; private $_bucketManager; private $_fileManager; /** * * @param B2Client $client */ public function __construct(B2Client $client){ $this->_client = $client; $this->_bucketManager = new BucketManager($client); $this->_fileManager = new FileManager($client); } /** * Create a new Private Bucket named $name * @param string $name * @return \B2\Entities\Bucket the bucket created */ public function createBucket($name){ return $this->_bucketManager->createBucket($name, Bucket::TYPE_PRIVATE); } /** * Create a new Public Bucket named $name * @param string $name * @return \B2\Entities\Bucket the bucket created */ public function createPublicBucket($name){ return $this->_bucketManager->createBucket($name, Bucket::TYPE_PUBLIC); } /** * Delete Bucket named $name * @param string $name * @return true on success */ public function deleteBucket($name){ $bucket = $this->_bucketManager->getBucketByName($name); return $this->_bucketManager->deleteBucket($bucket); } /** * List existing buckets. If !$cached api call will be made to B2 server. * Oterwise, the last requested data will be returned. * @param bool $reload * @return \B2\Entities\Bucket[] */ public function listBuckets($cached=true){ return $this->_bucketManager->listBuckets(!$cached); } /** * List directory on B2 servers in bucket$bucketName * @param string $bucketName the bucket to list directory from * @param string $path the directory to list * @return \B2\Entities\File[] file list in $bucketName/$path */ public function listDir($bucketName, $path=""){ $bucket = $this->_bucketManager->getBucketByName($bucketName); return $this->_fileManager->listDir($bucket, $path); } /** * Get a single file on B2 server located on bucket $bucketName and its path is exactly $path * @param string $bucketName * @param string $path * @return \B2\Entities\File|NULL the file data or null if file not found. */ public function getFile($bucketName, $path){ $bucket = $this->_bucketManager->getBucketByName($bucketName); try { return $this->_fileManager->getFile($bucket, $path); } catch(\B2\Exceptions\B2Exception $e) { return null; } } /** * Delete file in bucket $bucketName located at $path * @param string $bucketName * @param string $path * @throws B2Exception if something goes wrong. * @return boolean true on success. */ public function deleteFile($bucketName, $path) { $bucket = $this->_bucketManager->getBucketByName($bucketName); return $this->_fileManager->deleteFile($bucket, $path); } /** * check if file exists on bucket $bucketName and path $path * @param string $bucketName the bucket name * @param string $path path to the file * @return boolean */ public function fileExists($bucketName, $path) { return !!$this->getFile($bucketName, $path); } /** * Upload file to B2 server * @param string $localFilePath path to the local file to upload * @param string $bucketName the bucket to upload to * @param string $path path on the bucket to upload to. * @param int $retries if upload fails, retry this number of times. * @throws B2Exception if anything goes wrong * @return \B2\Entities\File uploaded file data */ public function upload($localFilePath, $bucketName, $path, $retries=0){ $bucket = $this->_bucketManager->getBucketByName($bucketName); return $this->_fileManager->uploadFile($localFilePath, $bucket, $path, $retries); } /** * Download file from B2 setver * @param string $bucketName the bucket to download from * @param string $fileName path on the bucket to download. * @param string $saveTo path on the local filesystem to save the file to * @throws B2Exception if anything goes wrong * @return array download data */ public function download($bucketName, $fileName, $saveTo=null){ return $this->_fileManager->downloadFile($bucketName, $fileName, $saveTo); } /** * Cleanup unfinished large files. * @param String $bucketName * @param String $path */ public function cleanUnfinishedLargeFiles($bucket, $path){ $bucket = $this->_bucketManager->getBucketByName($bucket); return $this->_fileManager->cleanUnfinishedLargeFiles($bucket, $path); } }