2019-08-27 11:52:53 +08:00
|
|
|
|
using NUnit.Framework;
|
2020-03-02 17:13:02 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
2019-08-27 11:52:53 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-28 17:04:46 +08:00
|
|
|
|
using LeanCloud;
|
2020-03-02 17:13:02 +08:00
|
|
|
|
using LeanCloud.Storage;
|
2019-08-27 11:52:53 +08:00
|
|
|
|
|
2020-04-28 17:04:46 +08:00
|
|
|
|
namespace Storage.Test {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
public class FileTest : BaseTest {
|
2020-04-30 15:36:06 +08:00
|
|
|
|
static readonly string AvatarFilePath = "../../../../../assets/hello.png";
|
|
|
|
|
static readonly string APKFilePath = "../../../../../assets/test.apk";
|
2019-09-12 15:07:19 +08:00
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
|
private LCFile avatar;
|
2019-08-27 11:52:53 +08:00
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Test]
|
|
|
|
|
[Order(0)]
|
|
|
|
|
public async Task SaveFromPath() {
|
|
|
|
|
avatar = new LCFile("avatar", AvatarFilePath);
|
|
|
|
|
await avatar.Save((count, total) => {
|
|
|
|
|
TestContext.WriteLine($"progress: {count}/{total}");
|
|
|
|
|
});
|
|
|
|
|
TestContext.WriteLine(avatar.ObjectId);
|
|
|
|
|
Assert.NotNull(avatar.ObjectId);
|
2020-03-02 17:13:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(1)]
|
2020-03-02 17:13:02 +08:00
|
|
|
|
public async Task QueryFile() {
|
|
|
|
|
LCQuery<LCFile> query = LCFile.GetQuery();
|
2021-04-29 15:58:22 +08:00
|
|
|
|
LCFile file = await query.Get(avatar.ObjectId);
|
2020-03-02 17:13:02 +08:00
|
|
|
|
Assert.NotNull(file.Url);
|
|
|
|
|
TestContext.WriteLine(file.Url);
|
|
|
|
|
TestContext.WriteLine(file.GetThumbnailUrl(32, 32));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task SaveFromMemory() {
|
|
|
|
|
string text = "hello, world";
|
|
|
|
|
byte[] data = Encoding.UTF8.GetBytes(text);
|
|
|
|
|
LCFile file = new LCFile("text", data);
|
|
|
|
|
await file.Save();
|
|
|
|
|
TestContext.WriteLine(file.ObjectId);
|
2019-08-27 11:52:53 +08:00
|
|
|
|
Assert.NotNull(file.ObjectId);
|
|
|
|
|
}
|
2019-08-27 17:05:49 +08:00
|
|
|
|
|
2020-03-02 17:13:02 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task SaveFromUrl() {
|
|
|
|
|
LCFile file = new LCFile("scene", new Uri("http://img95.699pic.com/photo/50015/9034.jpg_wh300.jpg"));
|
|
|
|
|
file.AddMetaData("size", 1024);
|
|
|
|
|
file.AddMetaData("width", 128);
|
|
|
|
|
file.AddMetaData("height", 256);
|
|
|
|
|
file.MimeType = "image/jpg";
|
|
|
|
|
await file.Save();
|
|
|
|
|
TestContext.WriteLine(file.ObjectId);
|
2019-08-29 16:37:16 +08:00
|
|
|
|
Assert.NotNull(file.ObjectId);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 17:13:02 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task Qiniu() {
|
2020-03-03 11:55:57 +08:00
|
|
|
|
LCFile file = new LCFile("avatar", APKFilePath);
|
2020-03-02 17:13:02 +08:00
|
|
|
|
await file.Save();
|
|
|
|
|
TestContext.WriteLine(file.ObjectId);
|
|
|
|
|
Assert.NotNull(file.ObjectId);
|
2019-08-27 17:05:49 +08:00
|
|
|
|
}
|
2019-08-29 16:37:16 +08:00
|
|
|
|
|
2021-04-21 11:06:28 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task FileACL() {
|
|
|
|
|
LCUser user = await LCUser.LoginAnonymously();
|
|
|
|
|
|
|
|
|
|
LCFile file = new LCFile("avatar", AvatarFilePath);
|
|
|
|
|
LCACL acl = new LCACL();
|
|
|
|
|
acl.SetUserReadAccess(user, true);
|
|
|
|
|
file.ACL = acl;
|
|
|
|
|
await file.Save();
|
|
|
|
|
|
|
|
|
|
LCQuery<LCFile> query = LCFile.GetQuery();
|
|
|
|
|
LCFile avatar = await query.Get(file.ObjectId);
|
|
|
|
|
Assert.NotNull(avatar.ObjectId);
|
|
|
|
|
|
|
|
|
|
await LCUser.LoginAnonymously();
|
|
|
|
|
try {
|
|
|
|
|
LCFile forbiddenAvatar = await query.Get(file.ObjectId);
|
|
|
|
|
} catch (LCException e) {
|
|
|
|
|
Assert.AreEqual(e.Code, 403);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 15:58:22 +08:00
|
|
|
|
|
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task AWS() {
|
|
|
|
|
// LCApplication.Initialize("UlCpyvLm8aMzQsW6KnP6W3Wt-MdYXbMMI", "PyCTYoNoxCVoKKg394PBeS4r");
|
|
|
|
|
// LCFile file = new LCFile("avatar", AvatarFilePath);
|
|
|
|
|
// await file.Save((count, total) => {
|
|
|
|
|
// TestContext.WriteLine($"progress: {count}/{total}");
|
|
|
|
|
// });
|
|
|
|
|
// TestContext.WriteLine(file.ObjectId);
|
|
|
|
|
// Assert.NotNull(file.ObjectId);
|
|
|
|
|
//}
|
2019-08-27 11:52:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|