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