Skip to content

Commit 2abbfd3

Browse files
authored
Merge pull request #220 from leancloud/deprecate-file-api
feat(file): new file key mechanism
2 parents aa41670 + 918e5ca commit 2abbfd3

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
LEANCLOUD_API_SERVER: https://us.avoscloud.com
4646
LEANCLOUD_APP_ID: wnDg0lPt0wcYGJSiHRwHBhD4
4747
LEANCLOUD_APP_KEY: u9ekx9HFSFFBErWwyWHFmPDy
48+
LEANCLOUD_APP_MASTER_KEY: ${{ secrets.MASTER_KEY }}
4849
LEANCLOUD_REGION: US
4950
LEANCLOUD_APP_HOST: 127.0.0.1
5051
LEANCLOUD_APP_PORT: 8081

src/LeanCloud/File.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class File {
4949
*/
5050
public function __construct($name, $data=null, $mimeType=null) {
5151
$this->_data["name"] = $name;
52+
$this->_data["key"] = null;
5253
$this->_source = $data;
5354

5455
if (!$mimeType) {
@@ -136,6 +137,24 @@ public function getName() {
136137
return $this->get("name");
137138
}
138139

140+
/**
141+
* Get key of file
142+
*
143+
* @return string
144+
*/
145+
public function getKey() {
146+
return $this->get("key");
147+
}
148+
/**
149+
* Set key of file
150+
*
151+
* @return self
152+
*/
153+
public function setKey($val) {
154+
$this->_data["key"] = $val;
155+
return $this;
156+
}
157+
139158
/**
140159
* Get objectId of file
141160
*
@@ -258,19 +277,6 @@ public function getMeta($key=null) {
258277
return null;
259278
}
260279

261-
/**
262-
* Generate pseudo-uuid key for filename
263-
*
264-
* @return string
265-
*/
266-
private static function genFileKey() {
267-
$octets = array_map(function() {
268-
$num = floor((1 + Client::randomFloat()) * 0x10000);
269-
return substr(dechex($num), 1);
270-
}, range(0, 4));
271-
return implode("", $octets);
272-
}
273-
274280
/**
275281
* Is the file exteranl
276282
*
@@ -366,12 +372,13 @@ public function save() {
366372

367373
if ($this->isExternal()) {
368374
$data["url"] = $this->getUrl();
369-
$resp = Client::post("/files/{$this->getName()}", $data);
375+
$resp = Client::post("/files", $data);
370376
$this->mergeAfterSave($resp);
371377
} else {
372-
$key = static::genFileKey();
373-
$key = "{$key}." . pathinfo($this->getName(), PATHINFO_EXTENSION);
374-
$data["key"] = $key;
378+
$key = $this->getKey();
379+
if (isset($key)) {
380+
$data["key"] = $key;
381+
}
375382
$data["__type"] = "File";
376383
$resp = Client::post("/fileTokens", $data);
377384
if (!isset($resp["token"])) {
@@ -401,6 +408,7 @@ public function save() {
401408
unset($resp[$k]);
402409
}
403410
}
411+
404412
$this->mergeAfterSave($resp);
405413
}
406414
}

test/FileTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public function testCreateWithLocalFile() {
4141

4242
public function testSaveTextFile() {
4343
$file = File::createWithData("test.txt", "Hello World!");
44+
$this->assertNull($file->getKey());
4445
$file->save();
4546
$this->assertNotEmpty($file->getObjectId());
47+
$this->assertNotEmpty($file->getKey());
4648
$this->assertNotEmpty($file->getUrl());
4749
$this->assertNotEmpty($file->getName());
4850
$this->assertEquals("text/plain", $file->getMimeType());
@@ -63,6 +65,27 @@ public function testSaveUTF8TextFile() {
6365
$file->destroy();
6466
}
6567

68+
public function testSaveWithSpecifiedKeyWithoutMasterKey() {
69+
$file = File::createWithData("test.txt", "Hello World!");
70+
$file->setKey("abc");
71+
$this->assertEquals("abc", $file->getKey());
72+
$unsupportedKeyError = "Unsupported file key. Please use masterKey to set file key.";
73+
$this->setExpectedException("LeanCloud\CloudException", $unsupportedKeyError, 1);
74+
$file->save();
75+
$this->assertEmpty($file->getObjectId());
76+
}
77+
78+
public function testSaveExternalFile() {
79+
$file = File::createWithUrl("blabla.png", "https://leancloud.cn/favicon.png");
80+
$file->save();
81+
$this->assertNotEmpty($file->getObjectId());
82+
$this->assertEquals("blabla.png", $file->getName());
83+
$this->assertEquals("https://leancloud.cn/favicon.png", $file->getUrl());
84+
$this->assertEquals("image/png", $file->getMimeType());
85+
86+
$file->destroy();
87+
}
88+
6689
public function testFetchFile() {
6790
$file = File::createWithData("testFetch.txt", "你好,中国!");
6891
$file->save();

test/MasterTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use LeanCloud\LeanObject;
4+
use LeanCloud\Client;
5+
use LeanCloud\File;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class MasterTest extends TestCase {
9+
public static function setUpBeforeClass() {
10+
Client::initialize(
11+
getenv("LEANCLOUD_APP_ID"),
12+
getenv("LEANCLOUD_APP_KEY"),
13+
getenv("LEANCLOUD_APP_MASTER_KEY"));
14+
Client::useMasterKey(true);
15+
}
16+
17+
public function testSaveWithSpecifiedKey() {
18+
$file = File::createWithData("test.txt", "Hello World!");
19+
$file->setKey("abc");
20+
$this->assertEquals("abc", $file->getKey());
21+
$file->save();
22+
$this->assertNotEmpty($file->getObjectId());
23+
$this->assertNotEmpty($file->getName());
24+
25+
$this->assertEquals("abc", $file->getKey());
26+
$url = $file->getUrl();
27+
$parsedUrl = parse_url($url);
28+
$path = $parsedUrl["path"];
29+
$oldSchoolAwsS3FileUrlPrefix = "/avos-cloud-";
30+
$newAwsS3GluttonyUrlPrefix = "https://lc-gluttony";
31+
if (substr($path, 0, strlen($oldSchoolAwsS3FileUrlPrefix)) === $oldSchoolAwsS3FileUrlPrefix) {
32+
$splitPath = explode("/", $path, 3);
33+
$this->assertEquals("abc", $splitPath[2]);
34+
} else if (substr($url, 0, strlen($newAwsS3GluttonyUrlPrefix)) === $newAwsS3GluttonyUrlPrefix) {
35+
$gluttonyPath = "/" + substr(getenv("LEANCLOUD_APP_KEY"), 0, 12) + "/abc";
36+
$this->assertEquals($gluttonyPath, $path);
37+
} else {
38+
$this->assertEquals("/abc", $path);
39+
}
40+
41+
$this->assertEquals("text/plain", $file->getMimeType());
42+
$content = file_get_contents($url);
43+
$this->assertEquals("Hello World!", $content);
44+
45+
$file->destroy();
46+
}
47+
}

0 commit comments

Comments
 (0)