Skip to content

Commit 34aba14

Browse files
committed
add support for fname in form upload and resume upload
1 parent 5ae0200 commit 34aba14

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/Qiniu/Storage/FormUploader.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ public static function put(
3131
$config,
3232
$params,
3333
$mime,
34-
$checkCrc,
35-
$filePath
34+
$fname
3635
) {
3736

3837
$fields = array('token' => $upToken);
3938
if ($key === null) {
40-
$fname = $filePath;
39+
$fname='nullkey';
4140
} else {
42-
$fname = $filePath;
4341
$fields['key'] = $key;
4442
}
4543

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(
7171
/**
7272
* 上传操作
7373
*/
74-
public function upload($filePath)
74+
public function upload($fname)
7575
{
7676
$uploaded = 0;
7777
while ($uploaded < $this->size) {
@@ -106,7 +106,7 @@ public function upload($filePath)
106106
array_push($this->contexts, $ret['ctx']);
107107
$uploaded += $blockSize;
108108
}
109-
return $this->makeFile($filePath);
109+
return $this->makeFile($fname);
110110
}
111111

112112
/**
@@ -118,14 +118,14 @@ private function makeBlock($block, $blockSize)
118118
return $this->post($url, $block);
119119
}
120120

121-
private function fileUrl($filePath)
121+
private function fileUrl($fname)
122122
{
123123
$url = $this->host . '/mkfile/' . $this->size;
124124
$url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
125125
if ($this->key != null) {
126126
$url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
127127
}
128-
$url .= '/fname/' . \Qiniu\base64_urlSafeEncode($filePath);
128+
$url .= '/fname/' . \Qiniu\base64_urlSafeEncode($fname);
129129
if (!empty($this->params)) {
130130
foreach ($this->params as $key => $value) {
131131
$val = \Qiniu\base64_urlSafeEncode($value);
@@ -138,9 +138,9 @@ private function fileUrl($filePath)
138138
/**
139139
* 创建文件
140140
*/
141-
private function makeFile($filePath)
141+
private function makeFile($fname)
142142
{
143-
$url = $this->fileUrl($filePath);
143+
$url = $this->fileUrl($fname);
144144
$body = implode(',', $this->contexts);
145145
$response = $this->post($url, $body);
146146
if ($response->needRetry()) {

src/Qiniu/Storage/UploadManager.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ public function put(
4646
$data,
4747
$params = null,
4848
$mime = 'application/octet-stream',
49-
$checkCrc = false
49+
$fname = null
5050
) {
51+
5152
$params = self::trimParams($params);
5253
return FormUploader::put(
5354
$upToken,
@@ -56,8 +57,7 @@ public function put(
5657
$this->config,
5758
$params,
5859
$mime,
59-
$checkCrc,
60-
$filePath
60+
$fname
6161
);
6262
}
6363

@@ -87,6 +87,7 @@ public function putFile(
8787
$mime = 'application/octet-stream',
8888
$checkCrc = false
8989
) {
90+
9091
$file = fopen($filePath, 'rb');
9192
if ($file === false) {
9293
throw new \Exception("file can not open", 1);
@@ -108,7 +109,7 @@ public function putFile(
108109
$params,
109110
$mime,
110111
$checkCrc,
111-
$filePath
112+
basename($filePath)
112113
);
113114
}
114115

@@ -121,7 +122,7 @@ public function putFile(
121122
$mime,
122123
$this->config
123124
);
124-
$ret = $up->upload($filePath);
125+
$ret = $up->upload(basename($filePath));
125126
fclose($file);
126127
return $ret;
127128
}

tests/Qiniu/Tests/FormUpTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function setUp()
2424
public function testData()
2525
{
2626
$token = $this->auth->uploadToken($this->bucketName);
27-
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', true);
27+
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', null);
2828
$this->assertNull($error);
2929
$this->assertNotNull($ret['hash']);
3030
}
@@ -33,7 +33,7 @@ public function testData2()
3333
{
3434
$upManager = new UploadManager();
3535
$token = $this->auth->uploadToken($this->bucketName);
36-
list($ret, $error) = $upManager->put($token, 'formput', 'hello world', null, 'text/plain', true);
36+
list($ret, $error) = $upManager->put($token, 'formput', 'hello world', null, 'text/plain', null);
3737
$this->assertNull($error);
3838
$this->assertNotNull($ret['hash']);
3939
}
@@ -42,7 +42,7 @@ public function testFile()
4242
{
4343
$key = 'formPutFile';
4444
$token = $this->auth->uploadToken($this->bucketName, $key);
45-
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', true);
45+
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', null);
4646
$this->assertNull($error);
4747
$this->assertNotNull($ret['hash']);
4848
}
@@ -52,7 +52,7 @@ public function testFile2()
5252
$key = 'formPutFile';
5353
$token = $this->auth->uploadToken($this->bucketName, $key);
5454
$upManager = new UploadManager();
55-
list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain', true);
55+
list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain', null);
5656
$this->assertNull($error);
5757
$this->assertNotNull($ret['hash']);
5858
}

0 commit comments

Comments
 (0)