Edit File: FileManager.inc
<?php namespace B2\Entities; use B2\Http\B2Client; use B2\Exceptions\NotFoundException; use B2\Exceptions\BadPathException; /** * Author: Shlomi * Date: Sep 4, 2017 2:14:30 PM * Founder: Shlomi Bazak (SBZsoft) (c) * File: FileManager.inc * * Description: * */ // No direct access. defined("__NSS__") or die("Restrected Access."); class FileManager { private $_client; private $_uploader; public function __construct(B2Client $client){ $this->_client = $client; $this->_uploader = new FileUploader($client); } protected function checkPrefix($path){ if(preg_match("/^\//", $path)) throw new BadPathException("B2 path can't start with / ($path)"); } protected function _listfiles($bucketId, $prefix=null, $startFileName=null, $maxFileCount=0, $delimiter="/"){ $this->checkPrefix($prefix); $params = [ 'bucketId' => $bucketId ]; if($prefix) $params += [ 'prefix' => $prefix ]; if($startFileName) $params += [ 'startFileName' => $startFileName ]; if($maxFileCount) $params += [ 'maxFileCount' => $maxFileCount ]; if($delimiter) $params += [ 'delimiter' => $delimiter ]; $files = []; do { $data = $this->_client->requestApi(B2Api::LIST_FILE_NAMES, $params); foreach ($data['files'] as $file) { $files[] = new File($file); } $params['startFileName'] = $data["nextFileName"]; }while($params['startFileName'] !== null); return $files; } /** * * @param Bucket $bucket * @param string $path * @throws NotFoundException * @return \B2\Entities\File */ public function getFile(Bucket $bucket, $path){ $data = $this->_listfiles($bucket->getId(), $path, $path, 1); if(!sizeof($data)) throw new NotFoundException("File $path not found."); $file = $data[0]; if($file->getName() != $path) throw new NotFoundException("File $path not found."); return $file; } /** * * @param Bucket $bucket * @param string $path * @return \B2\Entities\File[] */ public function listDir(Bucket $bucket, $path){ return $this->_listfiles($bucket->getId(), $path); } /** * * @param Bucket $bucket * @param string $path * @return boolean */ public function deleteFile(Bucket $bucket, $path) { try { $file = $this->getFile($bucket, $path); } catch (NotFoundException $e){ return true; } $params = [ 'fileId' => $file->getId(), 'fileName' => $file->getName() ]; $data = $this->_client->requestApi(B2Api::DELETE_FILE_VERSION, $params); return true; } /** * * @param string $file path to the file to upload * @param Bucket $bucket * @param string $path path on the b2 server * @param int $retries if upload fails, retry this number of times. * @return \B2\Entities\File */ public function uploadFile($file, Bucket $bucket, $path, $retries){ return $this->_uploader->Upload($file, $bucket, $path, $retries); } public function downloadFile($bucketName, $fileName, $saveTo=null){ return $this->_client->requestDownload("{$bucketName}/{$fileName}", $saveTo); } public function cleanUnfinishedLargeFiles(Bucket $bucket, $prefix){ $this->checkPrefix($prefix); $params = [ 'bucketId' => $bucket->getId() ]; if($prefix) $params += [ 'namePrefix' => $prefix ]; $params['startFileId'] = null; do { $data = $this->_client->requestApi(B2Api::LIST_UNFINISHED_LARGE_FILES, $params); foreach ($data['files'] as $file) { $file = new File($file); try { $data = $this->_client->requestApi(B2Api::DELETE_FILE_VERSION, ['fileId' => $file->getId(), 'fileName' => $file->getName()]); }catch(\Exception $e) { } } $params['startFileId'] = @$data["nextFileId"]; }while($params['startFileId'] !== null); } }