Skip to content

Commit

Permalink
Merge pull request #220 from leancloud/deprecate-file-api
Browse files Browse the repository at this point in the history
feat(file): new file key mechanism
  • Loading branch information
weakish authored Dec 14, 2020
2 parents aa41670 + 918e5ca commit 2abbfd3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 25 additions & 17 deletions src/LeanCloud/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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"])) {
Expand Down Expand Up @@ -401,6 +408,7 @@ public function save() {
unset($resp[$k]);
}
}

$this->mergeAfterSave($resp);
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions test/MasterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use LeanCloud\LeanObject;
use LeanCloud\Client;
use LeanCloud\File;
use PHPUnit\Framework\TestCase;

class MasterTest extends TestCase {
public static function setUpBeforeClass() {
Client::initialize(
getenv("LEANCLOUD_APP_ID"),
getenv("LEANCLOUD_APP_KEY"),
getenv("LEANCLOUD_APP_MASTER_KEY"));
Client::useMasterKey(true);
}

public function testSaveWithSpecifiedKey() {
$file = File::createWithData("test.txt", "Hello World!");
$file->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();
}
}

0 comments on commit 2abbfd3

Please sign in to comment.