Merge pull request #91 from onerain88/load_file

Load file
oneRain 2020-09-02 17:33:23 +08:00 committed by GitHub
commit 0ef89b36ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 20 deletions

View File

@ -5,6 +5,9 @@ using System.Net.Http;
namespace LeanCloud.Common {
public static class LCHttpUtils {
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
if (LCLogger.LogDelegate == null) {
return;
}
if (client == null) {
return;
}
@ -35,6 +38,9 @@ namespace LeanCloud.Common {
}
public static void PrintResponse(HttpResponseMessage response, string content = null) {
if (LCLogger.LogDelegate == null) {
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("=== HTTP Response Start ===");
sb.AppendLine($"URL: {response.RequestMessage.RequestUri}");

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.");