Skip to content

Commit 70b9fa5

Browse files
author
kang.chen
committed
feat: 新增复制文件和移动文件功能
1 parent b4819fd commit 70b9fa5

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

doc.md

+50
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,56 @@ Upyun::usage( string $path = '/' )
346346

347347

348348

349+
---
350+
351+
352+
### copy
353+
354+
复制文件。只能操作文件,不能操作文件夹。
355+
356+
```php
357+
Upyun::copy( string $source, string $target )
358+
```
359+
360+
361+
**参数列表:**
362+
363+
- **string** `$source`
364+
源文件地址
365+
- **string** `$target`
366+
目标文件地址
367+
368+
369+
**返回值:**
370+
371+
复制成功返回 true,否则返回 false
372+
373+
374+
---
375+
376+
377+
### move
378+
379+
移动文件。可以进行文件重命名、文件移动,只能操作文件,不能操作文件夹。
380+
381+
```php
382+
Upyun::move( string $source, string $target )
383+
```
384+
385+
386+
**参数列表:**
387+
388+
- **string** `$source`
389+
需要移动的文件地址
390+
- **string** `$target`
391+
目标文件地址
392+
393+
394+
**返回值:**
395+
396+
移动成功返回 true,否则返回 false
397+
398+
349399
---
350400

351401

src/Upyun/Upyun.php

+36
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,42 @@ public function usage($path = '/')
294294
return $response->getBody()->getContents();
295295
}
296296

297+
/**
298+
* 复制文件。只能操作文件,不能操作文件夹。
299+
*
300+
* @param string $source 源文件地址
301+
* @param string $target 目标文件地址
302+
* @return bool 复制成功返回 true,否则 false
303+
* @throws \Exception
304+
*/
305+
public function copy($source, $target)
306+
{
307+
$source = '/' . $this->config->serviceName . '/' . ltrim($source, '/');
308+
$req = new Rest($this->config);
309+
$response = $req->request('PUT', $target)
310+
->withHeader('X-Upyun-Copy-Source', $source)
311+
->send();
312+
return $response->getStatusCode() === 200;
313+
}
314+
315+
/**
316+
* 移动文件。可以进行文件重命名、文件移动,只能操作文件,不能操作文件夹。
317+
*
318+
* @param string $source 源文件地址
319+
* @param string $target 目标文件地址
320+
* @return bool 移动成功返回 true,否则 false
321+
* @throws \Exception
322+
*/
323+
public function move($source, $target)
324+
{
325+
$source = '/' . $this->config->serviceName . '/' . ltrim($source, '/');
326+
$req = new Rest($this->config);
327+
$response = $req->request('PUT', $target)
328+
->withHeader('X-Upyun-Move-Source', $source)
329+
->send();
330+
return $response->getStatusCode() === 200;
331+
}
332+
297333
/**
298334
* 刷新缓存
299335
*

tests/UpyunTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function testHas()
136136
$name = 'test-has.txt';
137137
self::$upyun->write($name, 'test file content 4');
138138
$this->assertEquals(self::$upyun->has($name), true);
139+
sleep(5);
139140
self::$upyun->delete($name);
140141
sleep(5);
141142
$this->assertEquals(self::$upyun->has($name), false);
@@ -204,6 +205,33 @@ public function testUsage()
204205
$this->assertTrue($size > 0);
205206
}
206207

208+
/**
209+
* @depends testWriteString
210+
*/
211+
public function testCopy()
212+
{
213+
$source = 'test-copy.txt';
214+
$target = 'test-copy-target.txt';
215+
self::$upyun->write($source, 'test file content 6');
216+
sleep(5);
217+
self::$upyun->copy($source, $target);
218+
$this->assertEquals(self::$upyun->has($target), true);
219+
}
220+
221+
/**
222+
* @depends testWriteString
223+
*/
224+
public function testMove()
225+
{
226+
$source = 'test-move.txt';
227+
$target = 'test-move-target.txt';
228+
self::$upyun->write($source, 'test file content 7');
229+
sleep(5);
230+
self::$upyun->move($source, $target);
231+
$this->assertEquals(self::$upyun->has($source), false);
232+
$this->assertEquals(self::$upyun->has($target), true);
233+
}
234+
207235
public function testPurge()
208236
{
209237
$urls = self::$upyun->purge(getFileUrl('test.txt'));
@@ -265,6 +293,7 @@ public function testAvMeta()
265293

266294
public function testSnapshot()
267295
{
296+
sleep(5);
268297
$source = 'php-sdk-sample.mp4';
269298
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
270299
$result = self::$upyun->snapshot('/php-sdk-sample.mp4', '/snapshot.jpg', '00:00:01', '720x480', 'jpg');

0 commit comments

Comments
 (0)