Skip to content

Commit 0c6eaf6

Browse files
committed
some work
storage rewrite method and add tests remove samples
1 parent ba47765 commit 0c6eaf6

39 files changed

+348
-613
lines changed

ManagedCode.Storage.Aws/ManagedCode.Storage.Aws.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
5-
<LangVersion>11</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12</LangVersion>
66
<EnableNETAnalyzers>true</EnableNETAnalyzers>
77
<DebugType>embedded</DebugType>
88
<Nullable>enable</Nullable>
@@ -22,10 +22,10 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="ManagedCode.Communication" Version="2.0.26" />
26-
<PackageReference Include="AWSSDK.S3" Version="3.7.205.14" />
25+
<PackageReference Include="ManagedCode.Communication" Version="8.0.0" />
26+
<PackageReference Include="AWSSDK.S3" Version="3.7.305.22" />
2727
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
28-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
28+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
2929
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
3030
</ItemGroup>
3131

ManagedCode.Storage.Azure.DataLake/ManagedCode.Storage.Azure.DataLake.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
5-
<LangVersion>11</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12</LangVersion>
66
<EnableNETAnalyzers>true</EnableNETAnalyzers>
77
<DebugType>embedded</DebugType>
88
<Nullable>enable</Nullable>
@@ -22,11 +22,11 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.16.0" />
26-
<PackageReference Include="ManagedCode.Communication" Version="2.0.26" />
27-
<PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" />
25+
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.17.1" />
26+
<PackageReference Include="ManagedCode.Communication" Version="8.0.0" />
27+
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
2828
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
29-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
29+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
3030
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
3131
</ItemGroup>
3232

ManagedCode.Storage.Azure/ManagedCode.Storage.Azure.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
5-
<LangVersion>11</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12</LangVersion>
66
<EnableNETAnalyzers>true</EnableNETAnalyzers>
77
<DebugType>embedded</DebugType>
88
<Nullable>enable</Nullable>
@@ -22,12 +22,12 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Azure.Identity" Version="1.10.3" />
26-
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.16.0" />
27-
<PackageReference Include="ManagedCode.Communication" Version="2.0.26" />
28-
<PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" />
25+
<PackageReference Include="Azure.Identity" Version="1.10.4" />
26+
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.17.1" />
27+
<PackageReference Include="ManagedCode.Communication" Version="8.0.0" />
28+
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
2929
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
30-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
30+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
3131
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
3232
</ItemGroup>
3333

ManagedCode.Storage.Client.SignalR/ManagedCode.Storage.Client.SignalR.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
5-
<LangVersion>11</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12</LangVersion>
66
<EnableNETAnalyzers>true</EnableNETAnalyzers>
77
<DebugType>embedded</DebugType>
88
<Nullable>enable</Nullable>

ManagedCode.Storage.Client/IStorageClient.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using ManagedCode.Communication;
6+
using ManagedCode.Storage.Core;
67
using ManagedCode.Storage.Core.Models;
78

89
namespace ManagedCode.Storage.Client;
910

10-
public interface IStorageClient
11+
public interface IStorageClient : IUploader, IDownloader
1112
{
1213
void SetChunkSize(long size);
13-
Task<Result<BlobMetadata>> UploadFile(Stream stream, string apiUrl, string contentName, CancellationToken cancellationToken = default);
14-
Task<Result<BlobMetadata>> UploadFile(FileInfo fileInfo, string apiUrl, string contentName, CancellationToken cancellationToken = default);
15-
Task<Result<BlobMetadata>> UploadFile(byte[] bytes, string apiUrl, string contentName, CancellationToken cancellationToken = default);
16-
Task<Result<BlobMetadata>> UploadFile(string base64, string apiUrl, string contentName, CancellationToken cancellationToken = default);
17-
Task<Result<uint>> UploadLargeFile(Stream file, string uploadApiUrl, string completeApiUrl, Action<double>? onProgressChanged, CancellationToken cancellationToken = default);
18-
Task<Result<LocalFile>> DownloadFile(string fileName, string apiUrl, string? path = null, CancellationToken cancellationToken = default);
19-
}
14+
15+
/// <summary>
16+
/// Event triggered when the progress status changes during an upload or download operation.
17+
/// </summary>
18+
/// <remarks>
19+
/// The event handler receives an argument of type <see cref="ProgressStatus"/> which contains detailed information about the progress of the operation.
20+
/// This includes the file name, progress percentage, total bytes, transferred bytes, elapsed time, remaining time, speed, and any error message.
21+
/// </remarks>
22+
event EventHandler<ProgressStatus> OnProgressStatusChanged;
23+
}
24+
25+
26+
public record ProgressStatus(
27+
string File,
28+
float Progress,
29+
long TotalBytes,
30+
long TransferredBytes,
31+
TimeSpan Elapsed,
32+
TimeSpan Remaining,
33+
string Speed,
34+
string? Error = null);

ManagedCode.Storage.Client/ManagedCode.Storage.Client.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
5-
<LangVersion>11</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12</LangVersion>
66
<EnableNETAnalyzers>true</EnableNETAnalyzers>
77
<DebugType>embedded</DebugType>
88
<Nullable>enable</Nullable>

ManagedCode.Storage.Client/StorageClient.cs

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-

2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.IO;
5-
using System.Linq;
64
using System.Net;
75
using System.Net.Http;
86
using System.Net.Http.Json;
@@ -44,26 +42,23 @@ public void SetChunkSize(long size)
4442
ChunkSize = size;
4543
}
4644

45+
public event EventHandler<ProgressStatus>? OnProgressStatusChanged;
46+
4747
public async Task<Result<BlobMetadata>> UploadFile(Stream stream, string apiUrl, string contentName, CancellationToken cancellationToken = default)
4848
{
49-
var streamContent = new StreamContent(stream);
50-
51-
using (var formData = new MultipartFormDataContent())
52-
{
53-
formData.Add(streamContent, contentName, contentName);
54-
55-
var response = await _httpClient.PostAsync(apiUrl, formData, cancellationToken);
56-
57-
if (response.IsSuccessStatusCode)
58-
{
59-
var result = await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
60-
return result;
61-
}
49+
using var streamContent = new StreamContent(stream);
50+
using var formData = new MultipartFormDataContent();
51+
formData.Add(streamContent, contentName, contentName);
6252

63-
string content = await response.Content.ReadAsStringAsync(cancellationToken: cancellationToken);
53+
var response = await _httpClient.PostAsync(apiUrl, formData, cancellationToken);
6454

65-
return Result<BlobMetadata>.Fail(response.StatusCode, content);
55+
if (response.IsSuccessStatusCode)
56+
{
57+
return await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
6658
}
59+
60+
var content = await response.Content.ReadAsStringAsync(cancellationToken: cancellationToken);
61+
return Result<BlobMetadata>.Fail(response.StatusCode, content);
6762
}
6863

6964
public async Task<Result<BlobMetadata>> UploadFile(FileInfo fileInfo, string apiUrl, string contentName, CancellationToken cancellationToken = default)
@@ -130,19 +125,21 @@ public async Task<Result<BlobMetadata>> UploadFile(string base64, string apiUrl,
130125
return Result<BlobMetadata>.Fail(response.StatusCode);
131126
}
132127

133-
public async Task<Result<LocalFile>> DownloadFile(string fileName, string apiUrl, string? path = null, CancellationToken cancellationToken = default)
128+
public async Task<Result<LocalFile>> DownloadFile(string fileName, string apiUrl, string? path = null, CancellationToken cancellationToken = default)
134129
{
135130
try
136131
{
137-
var response = await _httpClient.GetStreamAsync($"{apiUrl}/{fileName}", cancellationToken);
138-
132+
using var response = await _httpClient.GetStreamAsync($"{apiUrl}/{fileName}", cancellationToken);
139133
var localFile = path is null ? await LocalFile.FromStreamAsync(response, fileName) : await LocalFile.FromStreamAsync(response, path, fileName);
140-
141134
return Result<LocalFile>.Succeed(localFile);
142135
}
143-
catch (HttpRequestException e)
136+
catch (HttpRequestException e) when (e.StatusCode != null)
144137
{
145-
return Result<LocalFile>.Fail(e.StatusCode ?? HttpStatusCode.InternalServerError);
138+
return Result<LocalFile>.Fail(e.StatusCode.Value);
139+
}
140+
catch (Exception)
141+
{
142+
return Result<LocalFile>.Fail(HttpStatusCode.InternalServerError);
146143
}
147144
}
148145

@@ -193,4 +190,79 @@ public async Task<Result<uint>> UploadLargeFile(Stream file,
193190

194191
return await mergeResult.Content.ReadFromJsonAsync<Result<uint>>(cancellationToken: cancellationToken);
195192
}
193+
194+
public Task<Result<BlobMetadata>> UploadAsync(Stream stream, CancellationToken cancellationToken = default)
195+
{
196+
throw new NotImplementedException();
197+
}
198+
199+
public Task<Result<BlobMetadata>> UploadAsync(byte[] data, CancellationToken cancellationToken = default)
200+
{
201+
throw new NotImplementedException();
202+
}
203+
204+
public Task<Result<BlobMetadata>> UploadAsync(string content, CancellationToken cancellationToken = default)
205+
{
206+
throw new NotImplementedException();
207+
}
208+
209+
public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, CancellationToken cancellationToken = default)
210+
{
211+
throw new NotImplementedException();
212+
}
213+
214+
public Task<Result<BlobMetadata>> UploadAsync(Stream stream, UploadOptions options, CancellationToken cancellationToken = default)
215+
{
216+
throw new NotImplementedException();
217+
}
218+
219+
public Task<Result<BlobMetadata>> UploadAsync(byte[] data, UploadOptions options, CancellationToken cancellationToken = default)
220+
{
221+
throw new NotImplementedException();
222+
}
223+
224+
public Task<Result<BlobMetadata>> UploadAsync(string content, UploadOptions options, CancellationToken cancellationToken = default)
225+
{
226+
throw new NotImplementedException();
227+
}
228+
229+
public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, UploadOptions options, CancellationToken cancellationToken = default)
230+
{
231+
throw new NotImplementedException();
232+
}
233+
234+
public Task<Result<BlobMetadata>> UploadAsync(Stream stream, Action<UploadOptions> action, CancellationToken cancellationToken = default)
235+
{
236+
throw new NotImplementedException();
237+
}
238+
239+
public Task<Result<BlobMetadata>> UploadAsync(byte[] data, Action<UploadOptions> action, CancellationToken cancellationToken = default)
240+
{
241+
throw new NotImplementedException();
242+
}
243+
244+
public Task<Result<BlobMetadata>> UploadAsync(string content, Action<UploadOptions> action, CancellationToken cancellationToken = default)
245+
{
246+
throw new NotImplementedException();
247+
}
248+
249+
public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, Action<UploadOptions> action, CancellationToken cancellationToken = default)
250+
{
251+
throw new NotImplementedException();
252+
}
253+
254+
public Task<Result<LocalFile>> DownloadAsync(string fileName, CancellationToken cancellationToken = default)
255+
{
256+
throw new NotImplementedException();
257+
}
258+
259+
public Task<Result<LocalFile>> DownloadAsync(DownloadOptions options, CancellationToken cancellationToken = default)
260+
{
261+
throw new NotImplementedException();
262+
}
263+
264+
public Task<Result<LocalFile>> DownloadAsync(Action<DownloadOptions> action, CancellationToken cancellationToken = default)
265+
{
266+
throw new NotImplementedException();
267+
}
196268
}

ManagedCode.Storage.Core/IStorage.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@ public interface IStorage<out T, TOptions> : IStorage where TOptions : IStorageO
1515
Task<Result> SetStorageOptions(Action<TOptions> options, CancellationToken cancellationToken = default);
1616
}
1717

18-
public interface IStorage
18+
19+
public interface IDownloader
1920
{
2021
/// <summary>
21-
/// Create a container if it does not already exist.
22+
/// Downloads and saves the file to the local file system.
2223
/// </summary>
23-
Task<Result> CreateContainerAsync(CancellationToken cancellationToken = default);
24+
Task<Result<LocalFile>> DownloadAsync(string fileName, CancellationToken cancellationToken = default);
2425

2526
/// <summary>
26-
/// Delete a container if it does not already exist.
27+
/// Downloads and saves the file to the local file system.
2728
/// </summary>
28-
Task<Result> RemoveContainerAsync(CancellationToken cancellationToken = default);
29+
Task<Result<LocalFile>> DownloadAsync(DownloadOptions options, CancellationToken cancellationToken = default);
2930

3031
/// <summary>
31-
/// Delete the folder along with its contents
32+
/// Downloads and saves the file to the local file system.
3233
/// </summary>
33-
Task<Result> DeleteDirectoryAsync(string directory, CancellationToken cancellationToken = default);
34+
Task<Result<LocalFile>> DownloadAsync(Action<DownloadOptions> action, CancellationToken cancellationToken = default);
35+
}
3436

37+
public interface IUploader
38+
{
3539
/// <summary>
3640
/// Upload data from the stream into the blob storage.
3741
/// </summary>
@@ -92,21 +96,10 @@ public interface IStorage
9296
/// </summary>
9397
Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, Action<UploadOptions> action, CancellationToken cancellationToken = default);
9498

95-
/// <summary>
96-
/// Downloads and saves the file to the local file system.
97-
/// </summary>
98-
Task<Result<LocalFile>> DownloadAsync(string fileName, CancellationToken cancellationToken = default);
99-
100-
/// <summary>
101-
/// Downloads and saves the file to the local file system.
102-
/// </summary>
103-
Task<Result<LocalFile>> DownloadAsync(DownloadOptions options, CancellationToken cancellationToken = default);
104-
105-
/// <summary>
106-
/// Downloads and saves the file to the local file system.
107-
/// </summary>
108-
Task<Result<LocalFile>> DownloadAsync(Action<DownloadOptions> action, CancellationToken cancellationToken = default);
99+
}
109100

101+
public interface IStorageOperations
102+
{
110103
/// <summary>
111104
/// Deletes a file from the blob storage
112105
/// </summary>
@@ -156,6 +149,11 @@ public interface IStorage
156149
/// Returns metadata of all files in the specified path from the blob storage
157150
/// </summary>
158151
IAsyncEnumerable<BlobMetadata> GetBlobMetadataListAsync(string? directory = null, CancellationToken cancellationToken = default);
152+
153+
/// <summary>
154+
/// Delete the folder along with its contents
155+
/// </summary>
156+
Task<Result> DeleteDirectoryAsync(string directory, CancellationToken cancellationToken = default);
159157

160158
/// <summary>
161159
/// Set LegalHold
@@ -186,4 +184,17 @@ public interface IStorage
186184
/// Check LegalHold
187185
/// </summary
188186
Task<Result<bool>> HasLegalHoldAsync(Action<LegalHoldOptions> action, CancellationToken cancellationToken = default);
187+
}
188+
189+
public interface IStorage : IUploader, IDownloader, IStorageOperations
190+
{
191+
/// <summary>
192+
/// Create a container if it does not already exist.
193+
/// </summary>
194+
Task<Result> CreateContainerAsync(CancellationToken cancellationToken = default);
195+
196+
/// <summary>
197+
/// Delete a container if it does not already exist.
198+
/// </summary>
199+
Task<Result> RemoveContainerAsync(CancellationToken cancellationToken = default);
189200
}

0 commit comments

Comments
 (0)