-
Notifications
You must be signed in to change notification settings - Fork 14
/
StorageClient.cs
162 lines (126 loc) · 6.02 KB
/
StorageClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using ManagedCode.Communication;
using ManagedCode.Storage.Core;
using ManagedCode.Storage.Core.Models;
namespace ManagedCode.Storage.Client;
public class StorageClient : IStorageClient
{
private readonly HttpClient _httpClient;
public StorageClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<Result<BlobMetadata>> UploadFile(Stream stream, string apiUrl, string contentName, CancellationToken cancellationToken = default)
{
var streamContent = new StreamContent(stream);
using (var formData = new MultipartFormDataContent())
{
formData.Add(streamContent, contentName, contentName);
var response = await _httpClient.PostAsync(apiUrl, formData, cancellationToken);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
return result;
}
string content = await response.Content.ReadAsStringAsync(cancellationToken: cancellationToken);
return Result<BlobMetadata>.Fail(response.StatusCode, content);
}
}
public async Task<Result<BlobMetadata>> UploadFile(FileInfo fileInfo, string apiUrl, string contentName, CancellationToken cancellationToken = default)
{
using var streamContent = new StreamContent(fileInfo.OpenRead());
using (var formData = new MultipartFormDataContent())
{
formData.Add(streamContent, contentName, contentName);
var response = await _httpClient.PostAsync(apiUrl, formData, cancellationToken);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
return result;
}
return Result<BlobMetadata>.Fail(response.StatusCode);
}
}
public async Task<Result<BlobMetadata>> UploadFile(byte[] bytes, string apiUrl, string contentName, CancellationToken cancellationToken = default)
{
using (var stream = new MemoryStream())
{
stream.Write(bytes, 0, bytes.Length);
using var streamContent = new StreamContent(stream);
using (var formData = new MultipartFormDataContent())
{
formData.Add(streamContent, contentName, contentName);
var response = await _httpClient.PostAsync(apiUrl, formData, cancellationToken);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
return result;
}
return Result<BlobMetadata>.Fail(response.StatusCode);
}
}
}
public async Task<Result<BlobMetadata>> UploadFile(string base64, string apiUrl, string contentName, CancellationToken cancellationToken = default)
{
byte[] fileAsBytes = Convert.FromBase64String(base64);
using var fileContent = new ByteArrayContent(fileAsBytes);
using var formData = new MultipartFormDataContent();
formData.Add(fileContent, contentName, contentName);
var response = await _httpClient.PostAsync(apiUrl, formData, cancellationToken);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
}
return Result<BlobMetadata>.Fail(response.StatusCode);
}
public async Task<Result<LocalFile>> DownloadFile(string fileName, string apiUrl, string? path = null, CancellationToken cancellationToken = default)
{
try
{
var response = await _httpClient.GetStreamAsync($"{apiUrl}/{fileName}", cancellationToken);
var localFile = path is null ? await LocalFile.FromStreamAsync(response, fileName) : await LocalFile.FromStreamAsync(response, path, fileName);
return Result<LocalFile>.Succeed(localFile);
}
catch (HttpRequestException e)
{
return Result<LocalFile>.Fail(e.StatusCode ?? HttpStatusCode.InternalServerError);
}
}
public async Task<Result> UploadFileInChunks(Stream file, string apiUrl, int chunkSize, CancellationToken cancellationToken)
{
var buffer = new byte[chunkSize];
int bytesRead;
int chunkIndex = 0;
while ((bytesRead = await file.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
{
// Create a MemoryStream for the current chunk.
using (var memoryStream = new MemoryStream(buffer, 0, bytesRead))
{
var content = new StreamContent(memoryStream);
using (var chunk = new MultipartFormDataContent())
{
chunk.Add(content, "file", "file");
var byteArrayContent = new ByteArrayContent(await chunk.ReadAsByteArrayAsync(cancellationToken));
// Send the current chunk to the API endpoint.
var response = await _httpClient.PostAsync(apiUrl, byteArrayContent, cancellationToken);
if (!response.IsSuccessStatusCode)
{
return Result.Fail();
}
}
}
}
var mergeResult = await _httpClient.PostAsync(apiUrl + "/complete", JsonContent.Create("file"));
return await mergeResult.Content.ReadFromJsonAsync<Result>();
}
}