From 374e7ef4aeb1ad8cfb23b56b365b631b8880de69 Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 2 Sep 2020 17:15:34 +0800 Subject: [PATCH] * LCAWSUploader.cs: * LCQiniuUploader.cs: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCFile.cs: chore: 优化文件本地资源加载 --- Storage/Storage/Internal/File/LCAWSUploader.cs | 13 +++++++------ Storage/Storage/Internal/File/LCQiniuUploader.cs | 16 ++++++++-------- Storage/Storage/LCFile.cs | 11 +++++------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Storage/Storage/Internal/File/LCAWSUploader.cs b/Storage/Storage/Internal/File/LCAWSUploader.cs index d5484c9..1c9a731 100644 --- a/Storage/Storage/Internal/File/LCAWSUploader.cs +++ b/Storage/Storage/Internal/File/LCAWSUploader.cs @@ -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 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), diff --git a/Storage/Storage/Internal/File/LCQiniuUploader.cs b/Storage/Storage/Internal/File/LCQiniuUploader.cs index 69459ff..29ea743 100644 --- a/Storage/Storage/Internal/File/LCQiniuUploader.cs +++ b/Storage/Storage/Internal/File/LCQiniuUploader.cs @@ -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 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); diff --git a/Storage/Storage/LCFile.cs b/Storage/Storage/LCFile.cs index 8e504f9..a27db7b 100644 --- a/Storage/Storage/LCFile.cs +++ b/Storage/Storage/LCFile.cs @@ -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(); @@ -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.");