Skip to content

Commit 73acf61

Browse files
committed
Merge pull request #51 from dtynn/wanglin/putpolicy
Wanglin/putpolicy
2 parents 23b920e + 00a8520 commit 73acf61

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

qiniu/rs.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ function Qiniu_RS_MakeBaseUrl($domain, $key) // => $baseUrl
4141

4242
class Qiniu_RS_PutPolicy
4343
{
44-
public $Scope;
44+
public $Scope; //必填
45+
public $Expires; //默认为3600s
4546
public $CallbackUrl;
4647
public $CallbackBody;
4748
public $ReturnUrl;
4849
public $ReturnBody;
4950
public $AsyncOps;
5051
public $EndUser;
51-
public $Expires;
52+
public $InsertOnly; //若非0,则任何情况下无法覆盖上传
53+
public $DetectMime; //若非0,则服务端根据内容自动确定MimeType
54+
public $FsizeLimit;
55+
public $SaveKey;
5256
public $PersistentOps;
5357
public $PersistentNotifyUrl;
5458

@@ -84,6 +88,18 @@ public function Token($mac) // => $token
8488
if (!empty($this->EndUser)) {
8589
$policy['endUser'] = $this->EndUser;
8690
}
91+
if (!empty($this->InsertOnly)) {
92+
$policy['exclusive'] = $this->InsertOnly;
93+
}
94+
if (!empty($this->DetectMime)) {
95+
$policy['detectMime'] = $this->DetectMime;
96+
}
97+
if (!empty($this->FsizeLimit)) {
98+
$policy['fsizeLimit'] = $this->FsizeLimit;
99+
}
100+
if (!empty($this->SaveKey)) {
101+
$policy['saveKey'] = $this->SaveKey;
102+
}
87103
if (!empty($this->PersistentOps)) {
88104
$policy['persistentOps'] = $this->PersistentOps;
89105
}

tests/IoTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,68 @@ public function testPut()
5555
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
5656
$this->assertNull($err);
5757
}
58+
59+
public function testPut_sizelimit()
60+
{
61+
$key = 'testPut_sizelimit' . getTid();
62+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
63+
64+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
65+
$putPolicy->FsizeLimit = 1;
66+
$upToken = $putPolicy->Token(null);
67+
list($ret, $err) = Qiniu_Put($upToken, $key, "hello world!", null);
68+
$this->assertNull($ret);
69+
$this->assertEquals($err->Err, 'exceed FsizeLimit');
70+
var_dump($err);
71+
}
72+
73+
public function testPut_mime_save()
74+
{
75+
$key = 'testPut_mime_save' . getTid();
76+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
77+
78+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
79+
$putPolicy->DetectMime = 1;
80+
$putPolicy->SaveKey = $key;
81+
$upToken = $putPolicy->Token(null);
82+
$putExtra = new Qiniu_PutExtra();
83+
$putExtra->MimeType = 'image/jpg';
84+
list($ret, $err) = Qiniu_PutFile($upToken, null, __file__, $putExtra);
85+
$this->assertNull($err);
86+
87+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
88+
$this->assertNull($err);
89+
$this->assertEquals($ret['mimeType'], 'application/x-httpd-php');
90+
var_dump($ret);
91+
92+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
93+
$this->assertNull($err);
94+
}
95+
96+
public function testPut_exclusive()
97+
{
98+
$key = 'testPut_exclusive' . getTid();
99+
$scope = $this->bucket . ':' . $key;
100+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
101+
102+
$putPolicy = new Qiniu_RS_PutPolicy($scope);
103+
$putPolicy->InsertOnly = 1;
104+
$upToken = $putPolicy->Token(null);
105+
106+
list($ret, $err) = Qiniu_Put($upToken, $key, "hello world!", null);
107+
$this->assertNull($err);
108+
list($ret, $err) = Qiniu_PutFile($upToken, $key, __file__, null);
109+
$this->assertNull($ret);
110+
$this->assertEquals($err->Err, 'file exists');
111+
var_dump($err);
112+
113+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
114+
$this->assertNull($err);
115+
$this->assertEquals($ret['mimeType'], 'application/octet-stream');
116+
var_dump($ret);
117+
118+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
119+
$this->assertNull($err);
120+
}
58121
}
59122

0 commit comments

Comments
 (0)