* LCAWSUploader.cs:

* LCQiniuUploader.cs:

* LCFile.cs: chore: 优化文件本地资源加载
oneRain 2020-09-02 17:15:34 +08:00
parent bf3e690618
commit 374e7ef4ae
3 changed files with 20 additions and 20 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Net;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
@ -7,20 +8,20 @@ using LeanCloud.Common;
namespace LeanCloud.Storage.Internal.File {
internal class LCAWSUploader {
string uploadUrl;
private string uploadUrl;
string mimeType;
private string mimeType;
byte[] data;
private Stream stream;
internal LCAWSUploader(string uploadUrl, string mimeType, byte[] data) {
internal LCAWSUploader(string uploadUrl, string mimeType, Stream stream) {
this.uploadUrl = uploadUrl;
this.mimeType = mimeType;
this.data = data;
this.stream = stream;
}
internal async Task Upload(Action<long, long> onProgress) {
LCProgressableStreamContent content = new LCProgressableStreamContent(new ByteArrayContent(data), onProgress);
LCProgressableStreamContent content = new LCProgressableStreamContent(new StreamContent(stream), onProgress);
HttpRequestMessage request = new HttpRequestMessage {
RequestUri = new Uri(uploadUrl),

View File

@ -1,32 +1,32 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using LeanCloud.Common;
namespace LeanCloud.Storage.Internal.File {
internal class LCQiniuUploader {
string uploadUrl;
private string uploadUrl;
string token;
private string token;
string key;
private string key;
byte[] data;
private Stream stream;
internal LCQiniuUploader(string uploadUrl, string token, string key, byte[] data) {
internal LCQiniuUploader(string uploadUrl, string token, string key, Stream stream) {
this.uploadUrl = uploadUrl;
this.token = token;
this.key = key;
this.data = data;
this.stream = stream;
}
internal async Task Upload(Action<long, long> onProgress) {
MultipartFormDataContent dataContent = new MultipartFormDataContent();
dataContent.Add(new StringContent(key), "key");
dataContent.Add(new StringContent(token), "token");
dataContent.Add(new ByteArrayContent(data), "file");
dataContent.Add(new StreamContent(stream), "file");
LCProgressableStreamContent content = new LCProgressableStreamContent(dataContent, onProgress);

View File

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using LeanCloud.Storage.Internal.File;
using LeanCloud.Storage.Internal.Object;
using LeanCloud.Common;
namespace LeanCloud.Storage {
public class LCFile : LCObject {
@ -42,7 +41,7 @@ namespace LeanCloud.Storage {
}
}
readonly byte[] data;
readonly Stream stream;
public LCFile() : base(CLASS_NAME) {
MetaData = new Dictionary<string, object>();
@ -50,13 +49,13 @@ namespace LeanCloud.Storage {
public LCFile(string name, byte[] bytes) : this() {
Name = name;
data = bytes;
stream = new MemoryStream(bytes);
}
public LCFile(string name, string path) : this() {
Name = name;
MimeType = LCMimeTypeMap.GetMimeType(path);
data = File.ReadAllBytes(path);
stream = new FileStream(path, FileMode.Open);
}
public LCFile(string name, Uri url) : this() {
@ -82,11 +81,11 @@ namespace LeanCloud.Storage {
try {
if (provider == "s3") {
// AWS
LCAWSUploader uploader = new LCAWSUploader(uploadUrl, MimeType, data);
LCAWSUploader uploader = new LCAWSUploader(uploadUrl, MimeType, stream);
await uploader.Upload(onProgress);
} else if (provider == "qiniu") {
// Qiniu
LCQiniuUploader uploader = new LCQiniuUploader(uploadUrl, token, key, data);
LCQiniuUploader uploader = new LCQiniuUploader(uploadUrl, token, key, stream);
await uploader.Upload(onProgress);
} else {
throw new Exception($"{provider} is not support.");