Merge pull request #91 from onerain88/load_file
Load file
commit
0ef89b36ea
|
@ -5,6 +5,9 @@ using System.Net.Http;
|
||||||
namespace LeanCloud.Common {
|
namespace LeanCloud.Common {
|
||||||
public static class LCHttpUtils {
|
public static class LCHttpUtils {
|
||||||
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
|
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
|
||||||
|
if (LCLogger.LogDelegate == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -35,6 +38,9 @@ namespace LeanCloud.Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PrintResponse(HttpResponseMessage response, string content = null) {
|
public static void PrintResponse(HttpResponseMessage response, string content = null) {
|
||||||
|
if (LCLogger.LogDelegate == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.AppendLine("=== HTTP Response Start ===");
|
sb.AppendLine("=== HTTP Response Start ===");
|
||||||
sb.AppendLine($"URL: {response.RequestMessage.RequestUri}");
|
sb.AppendLine($"URL: {response.RequestMessage.RequestUri}");
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -7,20 +8,20 @@ using LeanCloud.Common;
|
||||||
|
|
||||||
namespace LeanCloud.Storage.Internal.File {
|
namespace LeanCloud.Storage.Internal.File {
|
||||||
internal class LCAWSUploader {
|
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.uploadUrl = uploadUrl;
|
||||||
this.mimeType = mimeType;
|
this.mimeType = mimeType;
|
||||||
this.data = data;
|
this.stream = stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task Upload(Action<long, long> onProgress) {
|
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 {
|
HttpRequestMessage request = new HttpRequestMessage {
|
||||||
RequestUri = new Uri(uploadUrl),
|
RequestUri = new Uri(uploadUrl),
|
||||||
|
|
|
@ -1,32 +1,32 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using LeanCloud.Common;
|
using LeanCloud.Common;
|
||||||
|
|
||||||
namespace LeanCloud.Storage.Internal.File {
|
namespace LeanCloud.Storage.Internal.File {
|
||||||
internal class LCQiniuUploader {
|
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.uploadUrl = uploadUrl;
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.data = data;
|
this.stream = stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task Upload(Action<long, long> onProgress) {
|
internal async Task Upload(Action<long, long> onProgress) {
|
||||||
MultipartFormDataContent dataContent = new MultipartFormDataContent();
|
MultipartFormDataContent dataContent = new MultipartFormDataContent();
|
||||||
dataContent.Add(new StringContent(key), "key");
|
dataContent.Add(new StringContent(key), "key");
|
||||||
dataContent.Add(new StringContent(token), "token");
|
dataContent.Add(new StringContent(token), "token");
|
||||||
dataContent.Add(new ByteArrayContent(data), "file");
|
dataContent.Add(new StreamContent(stream), "file");
|
||||||
|
|
||||||
LCProgressableStreamContent content = new LCProgressableStreamContent(dataContent, onProgress);
|
LCProgressableStreamContent content = new LCProgressableStreamContent(dataContent, onProgress);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using LeanCloud.Storage.Internal.File;
|
using LeanCloud.Storage.Internal.File;
|
||||||
using LeanCloud.Storage.Internal.Object;
|
using LeanCloud.Storage.Internal.Object;
|
||||||
using LeanCloud.Common;
|
|
||||||
|
|
||||||
namespace LeanCloud.Storage {
|
namespace LeanCloud.Storage {
|
||||||
public class LCFile : LCObject {
|
public class LCFile : LCObject {
|
||||||
|
@ -42,7 +41,7 @@ namespace LeanCloud.Storage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly byte[] data;
|
readonly Stream stream;
|
||||||
|
|
||||||
public LCFile() : base(CLASS_NAME) {
|
public LCFile() : base(CLASS_NAME) {
|
||||||
MetaData = new Dictionary<string, object>();
|
MetaData = new Dictionary<string, object>();
|
||||||
|
@ -50,13 +49,13 @@ namespace LeanCloud.Storage {
|
||||||
|
|
||||||
public LCFile(string name, byte[] bytes) : this() {
|
public LCFile(string name, byte[] bytes) : this() {
|
||||||
Name = name;
|
Name = name;
|
||||||
data = bytes;
|
stream = new MemoryStream(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LCFile(string name, string path) : this() {
|
public LCFile(string name, string path) : this() {
|
||||||
Name = name;
|
Name = name;
|
||||||
MimeType = LCMimeTypeMap.GetMimeType(path);
|
MimeType = LCMimeTypeMap.GetMimeType(path);
|
||||||
data = File.ReadAllBytes(path);
|
stream = new FileStream(path, FileMode.Open);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LCFile(string name, Uri url) : this() {
|
public LCFile(string name, Uri url) : this() {
|
||||||
|
@ -82,11 +81,11 @@ namespace LeanCloud.Storage {
|
||||||
try {
|
try {
|
||||||
if (provider == "s3") {
|
if (provider == "s3") {
|
||||||
// AWS
|
// AWS
|
||||||
LCAWSUploader uploader = new LCAWSUploader(uploadUrl, MimeType, data);
|
LCAWSUploader uploader = new LCAWSUploader(uploadUrl, MimeType, stream);
|
||||||
await uploader.Upload(onProgress);
|
await uploader.Upload(onProgress);
|
||||||
} else if (provider == "qiniu") {
|
} else if (provider == "qiniu") {
|
||||||
// Qiniu
|
// Qiniu
|
||||||
LCQiniuUploader uploader = new LCQiniuUploader(uploadUrl, token, key, data);
|
LCQiniuUploader uploader = new LCQiniuUploader(uploadUrl, token, key, stream);
|
||||||
await uploader.Upload(onProgress);
|
await uploader.Upload(onProgress);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception($"{provider} is not support.");
|
throw new Exception($"{provider} is not support.");
|
||||||
|
|
Loading…
Reference in New Issue