diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 9a6ccbe..44a16f9 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -45,6 +45,7 @@ jobs: LEANCLOUD_API_SERVER: https://us.avoscloud.com LEANCLOUD_APP_ID: wnDg0lPt0wcYGJSiHRwHBhD4 LEANCLOUD_APP_KEY: u9ekx9HFSFFBErWwyWHFmPDy + LEANCLOUD_APP_MASTER_KEY: ${{ secrets.MASTER_KEY }} LEANCLOUD_REGION: US LEANCLOUD_APP_HOST: 127.0.0.1 LEANCLOUD_APP_PORT: 8081 diff --git a/src/LeanCloud/File.php b/src/LeanCloud/File.php index 2918969..827a885 100644 --- a/src/LeanCloud/File.php +++ b/src/LeanCloud/File.php @@ -49,6 +49,7 @@ class File { */ public function __construct($name, $data=null, $mimeType=null) { $this->_data["name"] = $name; + $this->_data["key"] = null; $this->_source = $data; if (!$mimeType) { @@ -136,6 +137,24 @@ public function getName() { return $this->get("name"); } + /** + * Get key of file + * + * @return string + */ + public function getKey() { + return $this->get("key"); + } + /** + * Set key of file + * + * @return self + */ + public function setKey($val) { + $this->_data["key"] = $val; + return $this; + } + /** * Get objectId of file * @@ -258,19 +277,6 @@ public function getMeta($key=null) { return null; } - /** - * Generate pseudo-uuid key for filename - * - * @return string - */ - private static function genFileKey() { - $octets = array_map(function() { - $num = floor((1 + Client::randomFloat()) * 0x10000); - return substr(dechex($num), 1); - }, range(0, 4)); - return implode("", $octets); - } - /** * Is the file exteranl * @@ -366,12 +372,13 @@ public function save() { if ($this->isExternal()) { $data["url"] = $this->getUrl(); - $resp = Client::post("/files/{$this->getName()}", $data); + $resp = Client::post("/files", $data); $this->mergeAfterSave($resp); } else { - $key = static::genFileKey(); - $key = "{$key}." . pathinfo($this->getName(), PATHINFO_EXTENSION); - $data["key"] = $key; + $key = $this->getKey(); + if (isset($key)) { + $data["key"] = $key; + } $data["__type"] = "File"; $resp = Client::post("/fileTokens", $data); if (!isset($resp["token"])) { @@ -401,6 +408,7 @@ public function save() { unset($resp[$k]); } } + $this->mergeAfterSave($resp); } } diff --git a/test/FileTest.php b/test/FileTest.php index 52754f3..ba12cbf 100644 --- a/test/FileTest.php +++ b/test/FileTest.php @@ -41,8 +41,10 @@ public function testCreateWithLocalFile() { public function testSaveTextFile() { $file = File::createWithData("test.txt", "Hello World!"); + $this->assertNull($file->getKey()); $file->save(); $this->assertNotEmpty($file->getObjectId()); + $this->assertNotEmpty($file->getKey()); $this->assertNotEmpty($file->getUrl()); $this->assertNotEmpty($file->getName()); $this->assertEquals("text/plain", $file->getMimeType()); @@ -63,6 +65,27 @@ public function testSaveUTF8TextFile() { $file->destroy(); } + public function testSaveWithSpecifiedKeyWithoutMasterKey() { + $file = File::createWithData("test.txt", "Hello World!"); + $file->setKey("abc"); + $this->assertEquals("abc", $file->getKey()); + $unsupportedKeyError = "Unsupported file key. Please use masterKey to set file key."; + $this->setExpectedException("LeanCloud\CloudException", $unsupportedKeyError, 1); + $file->save(); + $this->assertEmpty($file->getObjectId()); + } + + public function testSaveExternalFile() { + $file = File::createWithUrl("blabla.png", "https://leancloud.cn/favicon.png"); + $file->save(); + $this->assertNotEmpty($file->getObjectId()); + $this->assertEquals("blabla.png", $file->getName()); + $this->assertEquals("https://leancloud.cn/favicon.png", $file->getUrl()); + $this->assertEquals("image/png", $file->getMimeType()); + + $file->destroy(); + } + public function testFetchFile() { $file = File::createWithData("testFetch.txt", "你好,中国!"); $file->save(); diff --git a/test/MasterTest.php b/test/MasterTest.php new file mode 100644 index 0000000..f86624a --- /dev/null +++ b/test/MasterTest.php @@ -0,0 +1,47 @@ +setKey("abc"); + $this->assertEquals("abc", $file->getKey()); + $file->save(); + $this->assertNotEmpty($file->getObjectId()); + $this->assertNotEmpty($file->getName()); + + $this->assertEquals("abc", $file->getKey()); + $url = $file->getUrl(); + $parsedUrl = parse_url($url); + $path = $parsedUrl["path"]; + $oldSchoolAwsS3FileUrlPrefix = "/avos-cloud-"; + $newAwsS3GluttonyUrlPrefix = "https://lc-gluttony"; + if (substr($path, 0, strlen($oldSchoolAwsS3FileUrlPrefix)) === $oldSchoolAwsS3FileUrlPrefix) { + $splitPath = explode("/", $path, 3); + $this->assertEquals("abc", $splitPath[2]); + } else if (substr($url, 0, strlen($newAwsS3GluttonyUrlPrefix)) === $newAwsS3GluttonyUrlPrefix) { + $gluttonyPath = "/" + substr(getenv("LEANCLOUD_APP_KEY"), 0, 12) + "/abc"; + $this->assertEquals($gluttonyPath, $path); + } else { + $this->assertEquals("/abc", $path); + } + + $this->assertEquals("text/plain", $file->getMimeType()); + $content = file_get_contents($url); + $this->assertEquals("Hello World!", $content); + + $file->destroy(); + } +} \ No newline at end of file