@@ -6,15 +6,15 @@ discard block |
||
6 | 6 | class MediaEncodeOption extends MediaResOption |
7 | 7 | { |
8 | 8 | /* 以下属性是"视频转码"方法必须的属性 */ |
9 | - private $encodeTemplate; //模板名称。可以设置系统或者用户自定义模板。用户模板名称可以登录后台查看和设置,系统模板见附录系统模板列表 |
|
10 | - public $usePreset = 0; //是否使用系统模板。默认0。如果为1,则encodeTemplate必须设置系统模板名称 |
|
11 | - public $force = 0; //是否强制覆盖。默认0,如果为1,当output文件已经存在的时候会强制覆盖,否则不执行转码并结束任务 |
|
9 | + private $encodeTemplate; //模板名称。可以设置系统或者用户自定义模板。用户模板名称可以登录后台查看和设置,系统模板见附录系统模板列表 |
|
10 | + public $usePreset = 0; //是否使用系统模板。默认0。如果为1,则encodeTemplate必须设置系统模板名称 |
|
11 | + public $force = 0; //是否强制覆盖。默认0,如果为1,当output文件已经存在的时候会强制覆盖,否则不执行转码并结束任务 |
|
12 | 12 | /* 以下属性是"视频转码"方法可选的属性 */ |
13 | - private $watermark; //水印资源 |
|
14 | - private $watermarkTemplate; //用户自定义水印模板 |
|
15 | - private $notifyUrl; //通知url,任务结束之后会调用这个url |
|
16 | - private $seek; //截取音视频的开始位置 |
|
17 | - private $duration; //截取音视频的长度 |
|
13 | + private $watermark; //水印资源 |
|
14 | + private $watermarkTemplate; //用户自定义水印模板 |
|
15 | + private $notifyUrl; //通知url,任务结束之后会调用这个url |
|
16 | + private $seek; //截取音视频的开始位置 |
|
17 | + private $duration; //截取音视频的长度 |
|
18 | 18 | |
19 | 19 | /**设置转码模板。必须。模板在顽兔控制台"多媒体处理"中配置*/ |
20 | 20 | public function setEncodeTemplate($encodeTemplate) |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | if (isset($this->notifyUrl)) |
51 | 51 | $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
52 | 52 | if (isset($this->seek)) |
53 | - $httpBody .= '&seek=' . $this->seek; |
|
53 | + $httpBody .= '&seek=' . $this->seek; |
|
54 | 54 | if (isset($this->duration)) |
55 | 55 | $httpBody .= '&duration=' . $this->duration; |
56 | 56 | return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
@@ -127,8 +127,8 @@ discard block |
||
127 | 127 | /**多媒体操作的输入输出资源信息。视频截图和视频转码都继承该类*/ |
128 | 128 | abstract class MediaResOption |
129 | 129 | { |
130 | - private $input; //输入的资源 |
|
131 | - private $output; //输出的资源 |
|
130 | + private $input; //输入的资源 |
|
131 | + private $output; //输出的资源 |
|
132 | 132 | /**设置输入的文件。*/ |
133 | 133 | public function setInputResource($namespace = null, $dir = null, $name = null) |
134 | 134 | { |
@@ -25,12 +25,17 @@ discard block |
||
25 | 25 | public function checkOptionParameters() |
26 | 26 | { |
27 | 27 | list($valid, $msg) = parent::checkOptionParameters(); //检测输入输出资源是否合法 |
28 | - if (!$valid) |
|
29 | - return array($valid, $msg, null); |
|
30 | - if (empty($this->encodeTemplate)) |
|
31 | - return array(false, "encodeTemplate is empty.", null); // 判断是否设置输入输出文件,或者转码模板 |
|
32 | - if (($this->usePreset != 0 && $this->usePreset != 1) || ($this->force != 0 && $this->force != 1)) |
|
33 | - return array(false, "parameters 'usePreset' or 'force' is invalid.", null); // 判断usePreset和force参数是否为0或1 |
|
28 | + if (!$valid) { |
|
29 | + return array($valid, $msg, null); |
|
30 | + } |
|
31 | + if (empty($this->encodeTemplate)) { |
|
32 | + return array(false, "encodeTemplate is empty.", null); |
|
33 | + } |
|
34 | + // 判断是否设置输入输出文件,或者转码模板 |
|
35 | + if (($this->usePreset != 0 && $this->usePreset != 1) || ($this->force != 0 && $this->force != 1)) { |
|
36 | + return array(false, "parameters 'usePreset' or 'force' is invalid.", null); |
|
37 | + } |
|
38 | + // 判断usePreset和force参数是否为0或1 |
|
34 | 39 | return $this->getOptionsHttpBody(); //返回http请求体 |
35 | 40 | } |
36 | 41 | /**构建多媒体转码所需的http请求体*/ |
@@ -43,16 +48,21 @@ discard block |
||
43 | 48 | $httpBody .= '&usePreset=' . $this->usePreset; |
44 | 49 | $httpBody .= '&force=' . $this->force; |
45 | 50 | // 可选的参数 |
46 | - if (isset($this->watermark)) |
|
47 | - $httpBody .= '&watermark=' . $this->watermark->buildResourceId(); |
|
48 | - if (isset($this->watermarkTemplate)) |
|
49 | - $httpBody .= '&watermarkTemplate=' . urlencode($this->watermarkTemplate); |
|
50 | - if (isset($this->notifyUrl)) |
|
51 | - $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
52 | - if (isset($this->seek)) |
|
53 | - $httpBody .= '&seek=' . $this->seek; |
|
54 | - if (isset($this->duration)) |
|
55 | - $httpBody .= '&duration=' . $this->duration; |
|
51 | + if (isset($this->watermark)) { |
|
52 | + $httpBody .= '&watermark=' . $this->watermark->buildResourceId(); |
|
53 | + } |
|
54 | + if (isset($this->watermarkTemplate)) { |
|
55 | + $httpBody .= '&watermarkTemplate=' . urlencode($this->watermarkTemplate); |
|
56 | + } |
|
57 | + if (isset($this->notifyUrl)) { |
|
58 | + $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
59 | + } |
|
60 | + if (isset($this->seek)) { |
|
61 | + $httpBody .= '&seek=' . $this->seek; |
|
62 | + } |
|
63 | + if (isset($this->duration)) { |
|
64 | + $httpBody .= '&duration=' . $this->duration; |
|
65 | + } |
|
56 | 66 | return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
57 | 67 | } |
58 | 68 | /*######################以下是可选的参数set方法#######################*/ |
@@ -93,10 +103,13 @@ discard block |
||
93 | 103 | public function checkOptionParameters() |
94 | 104 | { |
95 | 105 | list($valid, $msg) = parent::checkOptionParameters(); //检测输入输出资源是否合法 |
96 | - if (!$valid) |
|
97 | - return array($valid, $msg, null); |
|
98 | - if (empty($this->time) || !is_int($this->time) || $this->time < 0) |
|
99 | - return array(false, "time is empty or invalid.", null); // 是否设置时间,且时间是否合法 |
|
106 | + if (!$valid) { |
|
107 | + return array($valid, $msg, null); |
|
108 | + } |
|
109 | + if (empty($this->time) || !is_int($this->time) || $this->time < 0) { |
|
110 | + return array(false, "time is empty or invalid.", null); |
|
111 | + } |
|
112 | + // 是否设置时间,且时间是否合法 |
|
100 | 113 | return $this->getOptionsHttpBody(); //返回http请求体 |
101 | 114 | } |
102 | 115 | |
@@ -108,8 +121,9 @@ discard block |
||
108 | 121 | $httpBody .= '&output=' . $this->getOutputResId(); |
109 | 122 | $httpBody .= '&time=' . $this->time; |
110 | 123 | // 可选的参数 |
111 | - if (isset($this->notifyUrl)) |
|
112 | - $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
124 | + if (isset($this->notifyUrl)) { |
|
125 | + $httpBody .= '¬ifyUrl=' . urlencode($this->notifyUrl); |
|
126 | + } |
|
113 | 127 | return array(true, "valid", $httpBody); //视频转码参数合法,返回http请求体 |
114 | 128 | } |
115 | 129 | |
@@ -142,14 +156,18 @@ discard block |
||
142 | 156 | /**检测参数选项是否合法*/ |
143 | 157 | public function checkOptionParameters() |
144 | 158 | { |
145 | - if (empty($this->input) || empty($this->output)) |
|
146 | - return array(false, "input or output resources is empty."); // 判断是否设置输入输出文件,或者转码模板 |
|
159 | + if (empty($this->input) || empty($this->output)) { |
|
160 | + return array(false, "input or output resources is empty."); |
|
161 | + } |
|
162 | + // 判断是否设置输入输出文件,或者转码模板 |
|
147 | 163 | list($valid, $msg) = $this->input->checkResourceInfo(true, true); //检测输入的资源信息是否合法 |
148 | - if (!$valid) |
|
149 | - return array($valid, $msg); |
|
164 | + if (!$valid) { |
|
165 | + return array($valid, $msg); |
|
166 | + } |
|
150 | 167 | list($valid, $msg) = $this->output->checkResourceInfo(true, true); //检测输入的资源信息是否合法 |
151 | - if (!$valid) |
|
152 | - return array($valid, $msg); |
|
168 | + if (!$valid) { |
|
169 | + return array($valid, $msg); |
|
170 | + } |
|
153 | 171 | return array(true, null); |
154 | 172 | } |
155 | 173 | public function getInputResId() |
@@ -12,397 +12,397 @@ |
||
12 | 12 | |
13 | 13 | class WantuFileAdapter extends AbstractAdapter |
14 | 14 | { |
15 | - use NotSupportingVisibilityTrait; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var Client|null |
|
19 | - */ |
|
20 | - private $client = null; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $namespace = null; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $domain = null; |
|
31 | - |
|
32 | - /** |
|
33 | - * @param string $accessKey |
|
34 | - * @param string $secretKey |
|
35 | - * @param string $namespace |
|
36 | - */ |
|
37 | - public function __construct($accessKey, $secretKey, $namespace, $origin) |
|
38 | - { |
|
39 | - $client = new Client($accessKey, $secretKey, $namespace); |
|
40 | - $this->setClient($client, $namespace, $origin); |
|
41 | - } |
|
42 | - |
|
43 | - public function setClient(Client $client, $namespace, $origin) |
|
44 | - { |
|
45 | - $this->client = $client; |
|
46 | - $this->namespace = $namespace; |
|
47 | - $this->domain = $origin; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Write a new file. |
|
52 | - * |
|
53 | - * @param string $path |
|
54 | - * @param string $contents |
|
55 | - * @param Config $config Config object |
|
56 | - * |
|
57 | - * @return array|false false on failure file meta data on success |
|
58 | - */ |
|
59 | - public function write($path, $contents, Config $config) |
|
60 | - { |
|
61 | - $uploadPolicy = new UploadPolicy([ |
|
62 | - 'namespace' => $this->namespace, |
|
63 | - 'dir' => preg_replace('/^\./', '', dirname($path)), |
|
64 | - 'name' => last(explode("/", $path)), |
|
65 | - ]); |
|
66 | - |
|
67 | - return $this->client->uploadData($contents, $uploadPolicy); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Write a new file using a stream. |
|
72 | - * |
|
73 | - * @param string $path |
|
74 | - * @param resource $resource |
|
75 | - * @param Config $config Config object |
|
76 | - * |
|
77 | - * @return array|false false on failure file meta data on success |
|
78 | - */ |
|
79 | - public function writeStream($path, $resource, Config $config) |
|
80 | - { |
|
81 | - $contents = ''; |
|
82 | - |
|
83 | - while (!feof($resource)) { |
|
84 | - $contents .= fread($resource, 1024); |
|
85 | - } |
|
86 | - |
|
87 | - $response = $this->write($path, $contents, $config); |
|
88 | - |
|
89 | - if (false === $response) { |
|
90 | - return $response; |
|
91 | - } |
|
92 | - |
|
93 | - return compact('path'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Update a file. |
|
98 | - * |
|
99 | - * @param string $path |
|
100 | - * @param string $contents |
|
101 | - * @param Config $config Config object |
|
102 | - * |
|
103 | - * @return array|false false on failure file meta data on success |
|
104 | - */ |
|
105 | - public function update($path, $contents, Config $config) |
|
106 | - { |
|
107 | - $this->delete($path); |
|
108 | - |
|
109 | - return $this->write($path, $contents, $config); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Update a file using a stream. |
|
114 | - * |
|
115 | - * @param string $path |
|
116 | - * @param resource $resource |
|
117 | - * @param Config $config Config object |
|
118 | - * |
|
119 | - * @return array|false false on failure file meta data on success |
|
120 | - */ |
|
121 | - public function updateStream($path, $resource, Config $config) |
|
122 | - { |
|
123 | - $this->delete($path); |
|
124 | - |
|
125 | - return $this->writeStream($path, $resource, $config); |
|
126 | - } |
|
127 | - |
|
128 | - public function put($path, $contents, Config $config) |
|
129 | - { |
|
130 | - return $this->write($path, $contents, $config); |
|
131 | - } |
|
132 | - |
|
133 | - public function putStream($path, $resource, Config $config) |
|
134 | - { |
|
135 | - return $this->write($path, $resource, $config); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Rename a file. |
|
140 | - * |
|
141 | - * @param string $path |
|
142 | - * @param string $newpath |
|
143 | - * |
|
144 | - * @return bool |
|
145 | - */ |
|
146 | - public function rename($path, $newpath) |
|
147 | - { |
|
148 | - return $this->client->renameFile($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path)), preg_replace('/^\./', '', dirname($newpath)), last(explode("/", $newpath))); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Copy a file. |
|
153 | - * |
|
154 | - * @param string $path |
|
155 | - * @param string $newpath |
|
156 | - * |
|
157 | - * @return bool |
|
158 | - */ |
|
159 | - public function copy($path, $newpath) |
|
160 | - { |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Delete a file. |
|
165 | - * |
|
166 | - * @param string $path |
|
167 | - * |
|
168 | - * @return bool |
|
169 | - */ |
|
170 | - public function delete($path) |
|
171 | - { |
|
172 | - return $this->client->deleteFile($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path))); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Delete a directory. |
|
177 | - * |
|
178 | - * @param string $dirname |
|
179 | - * |
|
180 | - * @return bool |
|
181 | - */ |
|
182 | - public function deleteDir($dirname) |
|
183 | - { |
|
184 | - return $this->client->deleteDir($this->namespace, $dirname); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Create a directory. |
|
189 | - * |
|
190 | - * @param string $dirname directory name |
|
191 | - * @param Config $config |
|
192 | - * |
|
193 | - * @return array|false |
|
194 | - */ |
|
195 | - public function createDir($dirname, Config $config) |
|
196 | - { |
|
197 | - return $this->client->createDir($this->namespace, $dirname); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Check whether a file exists. |
|
202 | - * |
|
203 | - * @param string $path |
|
204 | - * |
|
205 | - * @return array|bool|null |
|
206 | - */ |
|
207 | - public function has($path) |
|
208 | - { |
|
209 | - if (Str::endsWith($path, "/")) { |
|
210 | - return $this->client->existsFolder($this->namespace, preg_replace('/^\./' , '', dirname( $path))); |
|
211 | - } else { |
|
212 | - return $this->client->existsFile($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path))); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * Read a file. |
|
219 | - * |
|
220 | - * @param string $path |
|
221 | - * |
|
222 | - * @return array|false |
|
223 | - */ |
|
224 | - public function read($path) |
|
225 | - { |
|
226 | - $contents = file_get_contents($this->getUrl($path)); |
|
227 | - return compact('contents', 'path'); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Read a file as a stream. |
|
232 | - * |
|
233 | - * @param string $path |
|
234 | - * |
|
235 | - * @return array|false |
|
236 | - */ |
|
237 | - public function readStream($path) |
|
238 | - { |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * List contents of a directory. |
|
243 | - * |
|
244 | - * @param string $directory |
|
245 | - * @param bool $recursive |
|
246 | - * |
|
247 | - * @return array |
|
248 | - */ |
|
249 | - public function listContents($directory = '', $recursive = false) |
|
250 | - { |
|
251 | - return $this->client->listFiles($this->namespace, $directory, 1, 1000); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Get all the meta data of a file or directory. |
|
256 | - * |
|
257 | - * @param string $path |
|
258 | - * |
|
259 | - * @return array|false |
|
260 | - */ |
|
261 | - public function getMetadata($path) |
|
262 | - { |
|
263 | - return $this->client->getFileInfo($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path))); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Get the size of a file. |
|
268 | - * |
|
269 | - * @param string $path |
|
270 | - * |
|
271 | - * @return array|false |
|
272 | - */ |
|
273 | - public function getSize($path) |
|
274 | - { |
|
275 | - } |
|
276 | - |
|
277 | - |
|
278 | - /** |
|
279 | - * Fetch url to bucket. |
|
280 | - * |
|
281 | - * @param string $path |
|
282 | - * @param string $url |
|
283 | - * |
|
284 | - * @return array|false |
|
285 | - */ |
|
286 | - public function fetch($path, $url) |
|
287 | - { |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Get private file download url. |
|
292 | - * |
|
293 | - * @param string $path |
|
294 | - * @param int $expires |
|
295 | - * |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - public function privateDownloadUrl($path, $expires = 3600) |
|
299 | - { |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Refresh file cache. |
|
304 | - * |
|
305 | - * @param string|array $path |
|
306 | - * |
|
307 | - * @return array |
|
308 | - */ |
|
309 | - public function refresh($path) |
|
310 | - { |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * Get the mime-type of a file. |
|
316 | - * |
|
317 | - * @param string $path |
|
318 | - * |
|
319 | - * @return array|false |
|
320 | - */ |
|
321 | - public function getMimeType($path) |
|
322 | - { |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - /** |
|
327 | - * Get the timestamp of a file. |
|
328 | - * |
|
329 | - * @param string $path |
|
330 | - * |
|
331 | - * @return array|false |
|
332 | - */ |
|
333 | - public function getTimestamp($path) |
|
334 | - { |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @return string |
|
339 | - */ |
|
340 | - public function getBucket() |
|
341 | - { |
|
342 | - return $this->bucket; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Get the upload token. |
|
347 | - * |
|
348 | - * @param string|null $key |
|
349 | - * @param int $ttl |
|
350 | - * @param string|null $policy |
|
351 | - * @param string|null $strictPolice |
|
352 | - * |
|
353 | - * @return string |
|
354 | - */ |
|
355 | - public function getUploadToken($option = null) |
|
356 | - { |
|
357 | - if ($option === null) { |
|
358 | - $option = ['name' => null, 'ttl' => 3600, ]; |
|
359 | - } else if (!isset($option['ttl']) || !$option['ttl']) { |
|
360 | - $option['ttl'] = 3600; |
|
361 | - } |
|
362 | - |
|
363 | - return $this->client->getUploadToken(collect([ |
|
364 | - 'expiration' => Carbon::now()->addSeconds($option['ttl'])->timestamp * 1000, |
|
365 | - 'insertOnly' => Conf::INSERT_ONLY_TRUE |
|
366 | - ])->merge(collect($option)->except(['ttl',]))); |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * @param array $stats |
|
371 | - * |
|
372 | - * @return array |
|
373 | - */ |
|
374 | - protected function normalizeFileInfo(array $stats) |
|
375 | - { |
|
376 | - return [ |
|
377 | - 'type' => 'file', |
|
378 | - 'path' => $stats['key'], |
|
379 | - 'timestamp' => floor($stats['putTime'] / 10000000), |
|
380 | - 'size' => $stats['fsize'], |
|
381 | - ]; |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Get resource url. |
|
386 | - * |
|
387 | - * @param string $path |
|
388 | - * |
|
389 | - * @return string |
|
390 | - */ |
|
391 | - public function getUrl($path) |
|
392 | - { |
|
393 | - return $this->normalizeHost($this->domain) . ltrim($path, '/'); |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * @param string $domain |
|
398 | - * |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - protected function normalizeHost($domain) |
|
402 | - { |
|
403 | - if (0 !== stripos($domain, 'https://') && 0 !== stripos($domain, 'http://')) { |
|
404 | - $domain = "http://{$domain}"; |
|
405 | - } |
|
406 | - return rtrim($domain, '/') . '/'; |
|
407 | - } |
|
15 | + use NotSupportingVisibilityTrait; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var Client|null |
|
19 | + */ |
|
20 | + private $client = null; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $namespace = null; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $domain = null; |
|
31 | + |
|
32 | + /** |
|
33 | + * @param string $accessKey |
|
34 | + * @param string $secretKey |
|
35 | + * @param string $namespace |
|
36 | + */ |
|
37 | + public function __construct($accessKey, $secretKey, $namespace, $origin) |
|
38 | + { |
|
39 | + $client = new Client($accessKey, $secretKey, $namespace); |
|
40 | + $this->setClient($client, $namespace, $origin); |
|
41 | + } |
|
42 | + |
|
43 | + public function setClient(Client $client, $namespace, $origin) |
|
44 | + { |
|
45 | + $this->client = $client; |
|
46 | + $this->namespace = $namespace; |
|
47 | + $this->domain = $origin; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Write a new file. |
|
52 | + * |
|
53 | + * @param string $path |
|
54 | + * @param string $contents |
|
55 | + * @param Config $config Config object |
|
56 | + * |
|
57 | + * @return array|false false on failure file meta data on success |
|
58 | + */ |
|
59 | + public function write($path, $contents, Config $config) |
|
60 | + { |
|
61 | + $uploadPolicy = new UploadPolicy([ |
|
62 | + 'namespace' => $this->namespace, |
|
63 | + 'dir' => preg_replace('/^\./', '', dirname($path)), |
|
64 | + 'name' => last(explode("/", $path)), |
|
65 | + ]); |
|
66 | + |
|
67 | + return $this->client->uploadData($contents, $uploadPolicy); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Write a new file using a stream. |
|
72 | + * |
|
73 | + * @param string $path |
|
74 | + * @param resource $resource |
|
75 | + * @param Config $config Config object |
|
76 | + * |
|
77 | + * @return array|false false on failure file meta data on success |
|
78 | + */ |
|
79 | + public function writeStream($path, $resource, Config $config) |
|
80 | + { |
|
81 | + $contents = ''; |
|
82 | + |
|
83 | + while (!feof($resource)) { |
|
84 | + $contents .= fread($resource, 1024); |
|
85 | + } |
|
86 | + |
|
87 | + $response = $this->write($path, $contents, $config); |
|
88 | + |
|
89 | + if (false === $response) { |
|
90 | + return $response; |
|
91 | + } |
|
92 | + |
|
93 | + return compact('path'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Update a file. |
|
98 | + * |
|
99 | + * @param string $path |
|
100 | + * @param string $contents |
|
101 | + * @param Config $config Config object |
|
102 | + * |
|
103 | + * @return array|false false on failure file meta data on success |
|
104 | + */ |
|
105 | + public function update($path, $contents, Config $config) |
|
106 | + { |
|
107 | + $this->delete($path); |
|
108 | + |
|
109 | + return $this->write($path, $contents, $config); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Update a file using a stream. |
|
114 | + * |
|
115 | + * @param string $path |
|
116 | + * @param resource $resource |
|
117 | + * @param Config $config Config object |
|
118 | + * |
|
119 | + * @return array|false false on failure file meta data on success |
|
120 | + */ |
|
121 | + public function updateStream($path, $resource, Config $config) |
|
122 | + { |
|
123 | + $this->delete($path); |
|
124 | + |
|
125 | + return $this->writeStream($path, $resource, $config); |
|
126 | + } |
|
127 | + |
|
128 | + public function put($path, $contents, Config $config) |
|
129 | + { |
|
130 | + return $this->write($path, $contents, $config); |
|
131 | + } |
|
132 | + |
|
133 | + public function putStream($path, $resource, Config $config) |
|
134 | + { |
|
135 | + return $this->write($path, $resource, $config); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Rename a file. |
|
140 | + * |
|
141 | + * @param string $path |
|
142 | + * @param string $newpath |
|
143 | + * |
|
144 | + * @return bool |
|
145 | + */ |
|
146 | + public function rename($path, $newpath) |
|
147 | + { |
|
148 | + return $this->client->renameFile($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path)), preg_replace('/^\./', '', dirname($newpath)), last(explode("/", $newpath))); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Copy a file. |
|
153 | + * |
|
154 | + * @param string $path |
|
155 | + * @param string $newpath |
|
156 | + * |
|
157 | + * @return bool |
|
158 | + */ |
|
159 | + public function copy($path, $newpath) |
|
160 | + { |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Delete a file. |
|
165 | + * |
|
166 | + * @param string $path |
|
167 | + * |
|
168 | + * @return bool |
|
169 | + */ |
|
170 | + public function delete($path) |
|
171 | + { |
|
172 | + return $this->client->deleteFile($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path))); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Delete a directory. |
|
177 | + * |
|
178 | + * @param string $dirname |
|
179 | + * |
|
180 | + * @return bool |
|
181 | + */ |
|
182 | + public function deleteDir($dirname) |
|
183 | + { |
|
184 | + return $this->client->deleteDir($this->namespace, $dirname); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Create a directory. |
|
189 | + * |
|
190 | + * @param string $dirname directory name |
|
191 | + * @param Config $config |
|
192 | + * |
|
193 | + * @return array|false |
|
194 | + */ |
|
195 | + public function createDir($dirname, Config $config) |
|
196 | + { |
|
197 | + return $this->client->createDir($this->namespace, $dirname); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Check whether a file exists. |
|
202 | + * |
|
203 | + * @param string $path |
|
204 | + * |
|
205 | + * @return array|bool|null |
|
206 | + */ |
|
207 | + public function has($path) |
|
208 | + { |
|
209 | + if (Str::endsWith($path, "/")) { |
|
210 | + return $this->client->existsFolder($this->namespace, preg_replace('/^\./' , '', dirname( $path))); |
|
211 | + } else { |
|
212 | + return $this->client->existsFile($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path))); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * Read a file. |
|
219 | + * |
|
220 | + * @param string $path |
|
221 | + * |
|
222 | + * @return array|false |
|
223 | + */ |
|
224 | + public function read($path) |
|
225 | + { |
|
226 | + $contents = file_get_contents($this->getUrl($path)); |
|
227 | + return compact('contents', 'path'); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Read a file as a stream. |
|
232 | + * |
|
233 | + * @param string $path |
|
234 | + * |
|
235 | + * @return array|false |
|
236 | + */ |
|
237 | + public function readStream($path) |
|
238 | + { |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * List contents of a directory. |
|
243 | + * |
|
244 | + * @param string $directory |
|
245 | + * @param bool $recursive |
|
246 | + * |
|
247 | + * @return array |
|
248 | + */ |
|
249 | + public function listContents($directory = '', $recursive = false) |
|
250 | + { |
|
251 | + return $this->client->listFiles($this->namespace, $directory, 1, 1000); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Get all the meta data of a file or directory. |
|
256 | + * |
|
257 | + * @param string $path |
|
258 | + * |
|
259 | + * @return array|false |
|
260 | + */ |
|
261 | + public function getMetadata($path) |
|
262 | + { |
|
263 | + return $this->client->getFileInfo($this->namespace, preg_replace('/^\./', '', dirname($path)), last(explode("/", $path))); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Get the size of a file. |
|
268 | + * |
|
269 | + * @param string $path |
|
270 | + * |
|
271 | + * @return array|false |
|
272 | + */ |
|
273 | + public function getSize($path) |
|
274 | + { |
|
275 | + } |
|
276 | + |
|
277 | + |
|
278 | + /** |
|
279 | + * Fetch url to bucket. |
|
280 | + * |
|
281 | + * @param string $path |
|
282 | + * @param string $url |
|
283 | + * |
|
284 | + * @return array|false |
|
285 | + */ |
|
286 | + public function fetch($path, $url) |
|
287 | + { |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Get private file download url. |
|
292 | + * |
|
293 | + * @param string $path |
|
294 | + * @param int $expires |
|
295 | + * |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + public function privateDownloadUrl($path, $expires = 3600) |
|
299 | + { |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Refresh file cache. |
|
304 | + * |
|
305 | + * @param string|array $path |
|
306 | + * |
|
307 | + * @return array |
|
308 | + */ |
|
309 | + public function refresh($path) |
|
310 | + { |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * Get the mime-type of a file. |
|
316 | + * |
|
317 | + * @param string $path |
|
318 | + * |
|
319 | + * @return array|false |
|
320 | + */ |
|
321 | + public function getMimeType($path) |
|
322 | + { |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + /** |
|
327 | + * Get the timestamp of a file. |
|
328 | + * |
|
329 | + * @param string $path |
|
330 | + * |
|
331 | + * @return array|false |
|
332 | + */ |
|
333 | + public function getTimestamp($path) |
|
334 | + { |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @return string |
|
339 | + */ |
|
340 | + public function getBucket() |
|
341 | + { |
|
342 | + return $this->bucket; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Get the upload token. |
|
347 | + * |
|
348 | + * @param string|null $key |
|
349 | + * @param int $ttl |
|
350 | + * @param string|null $policy |
|
351 | + * @param string|null $strictPolice |
|
352 | + * |
|
353 | + * @return string |
|
354 | + */ |
|
355 | + public function getUploadToken($option = null) |
|
356 | + { |
|
357 | + if ($option === null) { |
|
358 | + $option = ['name' => null, 'ttl' => 3600, ]; |
|
359 | + } else if (!isset($option['ttl']) || !$option['ttl']) { |
|
360 | + $option['ttl'] = 3600; |
|
361 | + } |
|
362 | + |
|
363 | + return $this->client->getUploadToken(collect([ |
|
364 | + 'expiration' => Carbon::now()->addSeconds($option['ttl'])->timestamp * 1000, |
|
365 | + 'insertOnly' => Conf::INSERT_ONLY_TRUE |
|
366 | + ])->merge(collect($option)->except(['ttl',]))); |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * @param array $stats |
|
371 | + * |
|
372 | + * @return array |
|
373 | + */ |
|
374 | + protected function normalizeFileInfo(array $stats) |
|
375 | + { |
|
376 | + return [ |
|
377 | + 'type' => 'file', |
|
378 | + 'path' => $stats['key'], |
|
379 | + 'timestamp' => floor($stats['putTime'] / 10000000), |
|
380 | + 'size' => $stats['fsize'], |
|
381 | + ]; |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Get resource url. |
|
386 | + * |
|
387 | + * @param string $path |
|
388 | + * |
|
389 | + * @return string |
|
390 | + */ |
|
391 | + public function getUrl($path) |
|
392 | + { |
|
393 | + return $this->normalizeHost($this->domain) . ltrim($path, '/'); |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * @param string $domain |
|
398 | + * |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + protected function normalizeHost($domain) |
|
402 | + { |
|
403 | + if (0 !== stripos($domain, 'https://') && 0 !== stripos($domain, 'http://')) { |
|
404 | + $domain = "http://{$domain}"; |
|
405 | + } |
|
406 | + return rtrim($domain, '/') . '/'; |
|
407 | + } |
|
408 | 408 | } |