2020-02-19 18:50:51 +08:00
|
|
|
|
using System;
|
2020-03-02 17:13:02 +08:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using LeanCloud.Common;
|
|
|
|
|
|
|
|
|
|
namespace LeanCloud.Storage.Internal.File {
|
|
|
|
|
internal class LCAWSUploader {
|
|
|
|
|
string uploadUrl;
|
|
|
|
|
|
|
|
|
|
string mimeType;
|
|
|
|
|
|
|
|
|
|
byte[] data;
|
|
|
|
|
|
|
|
|
|
internal LCAWSUploader(string uploadUrl, string mimeType, byte[] data) {
|
|
|
|
|
this.uploadUrl = uploadUrl;
|
|
|
|
|
this.mimeType = mimeType;
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 11:55:57 +08:00
|
|
|
|
internal async Task Upload(Action<long, long> onProgress) {
|
|
|
|
|
LCProgressableStreamContent content = new LCProgressableStreamContent(new ByteArrayContent(data), onProgress);
|
|
|
|
|
|
2020-03-02 17:13:02 +08:00
|
|
|
|
HttpRequestMessage request = new HttpRequestMessage {
|
|
|
|
|
RequestUri = new Uri(uploadUrl),
|
|
|
|
|
Method = HttpMethod.Put,
|
2020-03-03 11:55:57 +08:00
|
|
|
|
Content = content
|
2020-03-02 17:13:02 +08:00
|
|
|
|
};
|
|
|
|
|
HttpClient client = new HttpClient();
|
|
|
|
|
request.Headers.CacheControl = new CacheControlHeaderValue {
|
|
|
|
|
Public = true,
|
|
|
|
|
MaxAge = TimeSpan.FromMilliseconds(31536000)
|
|
|
|
|
};
|
|
|
|
|
request.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
|
2020-03-10 16:54:59 +08:00
|
|
|
|
LCHttpUtils.PrintRequest(client, request);
|
2020-03-02 17:13:02 +08:00
|
|
|
|
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
|
request.Dispose();
|
|
|
|
|
|
|
|
|
|
string resultString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
response.Dispose();
|
2020-03-10 16:54:59 +08:00
|
|
|
|
LCHttpUtils.PrintResponse(response, resultString);
|
2020-03-02 17:13:02 +08:00
|
|
|
|
|
|
|
|
|
HttpStatusCode statusCode = response.StatusCode;
|
|
|
|
|
|
2020-02-19 18:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|