Skip to content

Commit

Permalink
Merge pull request #57 from upyun/feature/copy-and-move
Browse files Browse the repository at this point in the history
feat: 新增复制文件和移动文件功能
  • Loading branch information
yejingx authored Apr 22, 2020
2 parents b4819fd + 266df96 commit c9f8246
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
50 changes: 50 additions & 0 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,56 @@ Upyun::usage( string $path = '/' )



---


### copy

复制文件。只能操作文件,不能操作文件夹。

```php
Upyun::copy( string $source, string $target )
```


**参数列表:**

- **string** `$source`
源文件地址
- **string** `$target`
目标文件地址


**返回值:**

复制成功返回 true,否则返回 false


---


### move

移动文件。可以进行文件重命名、文件移动,只能操作文件,不能操作文件夹。

```php
Upyun::move( string $source, string $target )
```


**参数列表:**

- **string** `$source`
需要移动的文件地址
- **string** `$target`
目标文件地址


**返回值:**

移动成功返回 true,否则返回 false


---


Expand Down
36 changes: 36 additions & 0 deletions src/Upyun/Upyun.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,42 @@ public function usage($path = '/')
return $response->getBody()->getContents();
}

/**
* 复制文件。只能操作文件,不能操作文件夹。
*
* @param string $source 源文件地址
* @param string $target 目标文件地址
* @return bool 复制成功返回 true,否则 false
* @throws \Exception
*/
public function copy($source, $target)
{
$source = '/' . $this->config->serviceName . '/' . ltrim($source, '/');
$req = new Rest($this->config);
$response = $req->request('PUT', $target)
->withHeader('X-Upyun-Copy-Source', $source)
->send();
return util::isSuccess($response->getStatusCode());
}

/**
* 移动文件。可以进行文件重命名、文件移动,只能操作文件,不能操作文件夹。
*
* @param string $source 源文件地址
* @param string $target 目标文件地址
* @return bool 移动成功返回 true,否则 false
* @throws \Exception
*/
public function move($source, $target)
{
$source = '/' . $this->config->serviceName . '/' . ltrim($source, '/');
$req = new Rest($this->config);
$response = $req->request('PUT', $target)
->withHeader('X-Upyun-Move-Source', $source)
->send();
return util::isSuccess($response->getStatusCode());
}

/**
* 刷新缓存
*
Expand Down
5 changes: 5 additions & 0 deletions src/Upyun/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public static function encodeURI($url)
);
return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score));
}

public static function isSuccess($code)
{
return $code >= 200 && $code < 300;
}
}
29 changes: 29 additions & 0 deletions tests/UpyunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function testHas()
$name = 'test-has.txt';
self::$upyun->write($name, 'test file content 4');
$this->assertEquals(self::$upyun->has($name), true);
sleep(5);
self::$upyun->delete($name);
sleep(5);
$this->assertEquals(self::$upyun->has($name), false);
Expand Down Expand Up @@ -204,6 +205,33 @@ public function testUsage()
$this->assertTrue($size > 0);
}

/**
* @depends testWriteString
*/
public function testCopy()
{
$source = 'test-copy.txt';
$target = 'test-copy-target.txt';
self::$upyun->write($source, 'test file content 6');
sleep(5);
self::$upyun->copy($source, $target);
$this->assertEquals(self::$upyun->has($target), true);
}

/**
* @depends testWriteString
*/
public function testMove()
{
$source = 'test-move.txt';
$target = 'test-move-target.txt';
self::$upyun->write($source, 'test file content 7');
sleep(5);
self::$upyun->move($source, $target);
$this->assertEquals(self::$upyun->has($source), false);
$this->assertEquals(self::$upyun->has($target), true);
}

public function testPurge()
{
$urls = self::$upyun->purge(getFileUrl('test.txt'));
Expand Down Expand Up @@ -265,6 +293,7 @@ public function testAvMeta()

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

0 comments on commit c9f8246

Please sign in to comment.