1 | <?php |
||
10 | class Files |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var B2Client |
||
15 | */ |
||
16 | public $B2Client; |
||
17 | |||
18 | /** |
||
19 | * Files constructor. |
||
20 | * @param B2Client $B2Client |
||
21 | */ |
||
22 | 6 | public function __construct(B2Client $B2Client) |
|
23 | { |
||
24 | 6 | $this->B2Client = $B2Client; |
|
25 | 6 | } |
|
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['files']); |
||
55 | |||
56 | if ($result['nextFileName'] !== null) { |
||
57 | $allresults = array_merge($allresults, |
||
58 | $this->listAllFileNames($bucketId, $result['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 | |||
120 | /** |
||
121 | * @param $bucketId |
||
122 | * @param $fileName |
||
123 | */ |
||
124 | public function hideFile($bucketId, $fileName) |
||
128 | |||
129 | /** |
||
130 | * @param $fileName |
||
131 | * @param $fileId |
||
132 | */ |
||
133 | public function deleteFileVersion($fileName, $fileId) { |
||
134 | return $this->B2Client->call('b2_delete_file_version', 'POST', ['fileName' => $fileName, 'fileId' => $fileId]); |
||
135 | } |
||
136 | } |
||
137 |