@@ -10,111 +10,111 @@ |
||
| 10 | 10 | class Files |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var B2Client |
|
| 15 | - */ |
|
| 16 | - public $B2Client; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Files constructor. |
|
| 20 | - * @param B2Client $B2Client |
|
| 21 | - */ |
|
| 22 | - public function __construct(B2Client $B2Client) |
|
| 23 | - { |
|
| 24 | - $this->B2Client = $B2Client; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @param $bucketId |
|
| 29 | - * @param $startFileName |
|
| 30 | - * @return mixed |
|
| 31 | - * @throws \Exception |
|
| 32 | - */ |
|
| 33 | - public function listFileNames($bucketId, $startFileName = null, $maxFileCount = 100) |
|
| 34 | - { |
|
| 35 | - return $this->B2Client->call('b2_list_file_names', 'POST', [ |
|
| 36 | - 'startFileName' => $startFileName, |
|
| 37 | - 'bucketId' => $bucketId, |
|
| 38 | - 'maxFileCount' => $maxFileCount |
|
| 39 | - ]); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Lists all files in a bucket and starting filename. For very large buckets this may fail, use listFileNames() directly |
|
| 44 | - * |
|
| 45 | - * @param $bucketId |
|
| 46 | - * @param $startFileName |
|
| 47 | - * @return array |
|
| 48 | - */ |
|
| 49 | - public function listAllFileNames($bucketId, $startFileName) |
|
| 50 | - { |
|
| 51 | - |
|
| 52 | - $allresults = []; |
|
| 53 | - $result = $this->listFileNames($bucketId, $startFileName); |
|
| 54 | - $allresults = array_merge($allresults, $result['responseBody']['files']); |
|
| 55 | - |
|
| 56 | - if ($result['responseBody']['nextFileName'] !== null) { |
|
| 57 | - $allresults = array_merge($allresults, |
|
| 58 | - $this->listAllFileNames($bucketId, $result['responseBody']['nextFileName'])); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return $allresults; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param $fileID |
|
| 66 | - * @return mixed |
|
| 67 | - * @throws \Exception |
|
| 68 | - */ |
|
| 69 | - public function getFileInfo($fileId) |
|
| 70 | - { |
|
| 71 | - return $this->B2Client->call('b2_get_file_info', 'POST', ['fileId' => $fileId]); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param $bucketId |
|
| 76 | - * @return mixed |
|
| 77 | - * @throws \Exception |
|
| 78 | - */ |
|
| 79 | - public function getUploadUrl($bucketId) |
|
| 80 | - { |
|
| 81 | - return $this->B2Client->call('b2_get_upload_url', 'POST', ['bucketId' => $bucketId]); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param $bucketId |
|
| 86 | - * @param string $filePath The path to the local file |
|
| 87 | - * @param string $fileName The name/path on B2 |
|
| 88 | - * @param string $contentType |
|
| 89 | - * @param array $uploadUrlResponse |
|
| 90 | - * @return array |
|
| 91 | - */ |
|
| 92 | - public function uploadFile($bucketId, $filePath, $fileName, $contentType, $uploadUrlResponse = []) |
|
| 93 | - { |
|
| 94 | - |
|
| 95 | - if (empty($uploadUrlResponse)) { |
|
| 96 | - $uploadUrlResponse = $this->getUploadUrl($bucketId); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $handle = fopen($filePath, 'r'); |
|
| 100 | - $fileData = fread($handle, filesize($filePath)); |
|
| 101 | - $fileDataSha1 = sha1_file($filePath); |
|
| 102 | - |
|
| 103 | - return $this->B2Client->uploadData($fileData, $fileDataSha1, $fileName, $contentType, |
|
| 104 | - $uploadUrlResponse['uploadUrl'], |
|
| 105 | - $uploadUrlResponse['authorizationToken']); |
|
| 106 | - |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param $bucketId |
|
| 111 | - * @param $fileName |
|
| 112 | - * @return mixed |
|
| 113 | - * @throws \Exception |
|
| 114 | - */ |
|
| 115 | - public function downloadFileByName($bucketName, $fileName) |
|
| 116 | - { |
|
| 117 | - return $this->B2Client->downloadFileByName($bucketName . '/' . $fileName); |
|
| 118 | - } |
|
| 13 | + /** |
|
| 14 | + * @var B2Client |
|
| 15 | + */ |
|
| 16 | + public $B2Client; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Files constructor. |
|
| 20 | + * @param B2Client $B2Client |
|
| 21 | + */ |
|
| 22 | + public function __construct(B2Client $B2Client) |
|
| 23 | + { |
|
| 24 | + $this->B2Client = $B2Client; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @param $bucketId |
|
| 29 | + * @param $startFileName |
|
| 30 | + * @return mixed |
|
| 31 | + * @throws \Exception |
|
| 32 | + */ |
|
| 33 | + public function listFileNames($bucketId, $startFileName = null, $maxFileCount = 100) |
|
| 34 | + { |
|
| 35 | + return $this->B2Client->call('b2_list_file_names', 'POST', [ |
|
| 36 | + 'startFileName' => $startFileName, |
|
| 37 | + 'bucketId' => $bucketId, |
|
| 38 | + 'maxFileCount' => $maxFileCount |
|
| 39 | + ]); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Lists all files in a bucket and starting filename. For very large buckets this may fail, use listFileNames() directly |
|
| 44 | + * |
|
| 45 | + * @param $bucketId |
|
| 46 | + * @param $startFileName |
|
| 47 | + * @return array |
|
| 48 | + */ |
|
| 49 | + public function listAllFileNames($bucketId, $startFileName) |
|
| 50 | + { |
|
| 51 | + |
|
| 52 | + $allresults = []; |
|
| 53 | + $result = $this->listFileNames($bucketId, $startFileName); |
|
| 54 | + $allresults = array_merge($allresults, $result['responseBody']['files']); |
|
| 55 | + |
|
| 56 | + if ($result['responseBody']['nextFileName'] !== null) { |
|
| 57 | + $allresults = array_merge($allresults, |
|
| 58 | + $this->listAllFileNames($bucketId, $result['responseBody']['nextFileName'])); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return $allresults; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param $fileID |
|
| 66 | + * @return mixed |
|
| 67 | + * @throws \Exception |
|
| 68 | + */ |
|
| 69 | + public function getFileInfo($fileId) |
|
| 70 | + { |
|
| 71 | + return $this->B2Client->call('b2_get_file_info', 'POST', ['fileId' => $fileId]); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param $bucketId |
|
| 76 | + * @return mixed |
|
| 77 | + * @throws \Exception |
|
| 78 | + */ |
|
| 79 | + public function getUploadUrl($bucketId) |
|
| 80 | + { |
|
| 81 | + return $this->B2Client->call('b2_get_upload_url', 'POST', ['bucketId' => $bucketId]); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param $bucketId |
|
| 86 | + * @param string $filePath The path to the local file |
|
| 87 | + * @param string $fileName The name/path on B2 |
|
| 88 | + * @param string $contentType |
|
| 89 | + * @param array $uploadUrlResponse |
|
| 90 | + * @return array |
|
| 91 | + */ |
|
| 92 | + public function uploadFile($bucketId, $filePath, $fileName, $contentType, $uploadUrlResponse = []) |
|
| 93 | + { |
|
| 94 | + |
|
| 95 | + if (empty($uploadUrlResponse)) { |
|
| 96 | + $uploadUrlResponse = $this->getUploadUrl($bucketId); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $handle = fopen($filePath, 'r'); |
|
| 100 | + $fileData = fread($handle, filesize($filePath)); |
|
| 101 | + $fileDataSha1 = sha1_file($filePath); |
|
| 102 | + |
|
| 103 | + return $this->B2Client->uploadData($fileData, $fileDataSha1, $fileName, $contentType, |
|
| 104 | + $uploadUrlResponse['uploadUrl'], |
|
| 105 | + $uploadUrlResponse['authorizationToken']); |
|
| 106 | + |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param $bucketId |
|
| 111 | + * @param $fileName |
|
| 112 | + * @return mixed |
|
| 113 | + * @throws \Exception |
|
| 114 | + */ |
|
| 115 | + public function downloadFileByName($bucketName, $fileName) |
|
| 116 | + { |
|
| 117 | + return $this->B2Client->downloadFileByName($bucketName . '/' . $fileName); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | } |