Skip to content

Commit

Permalink
feat: UploadAsync returns full FileAggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenskov authored Mar 1, 2024
2 parents bcd6c84 + b3fd5b3 commit a38a74e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Authors>Steffen Skov</Authors>
<Company>Steffen Skov</Company>
<Description>Extension to SegregatedStorage adding support for Azure Blob storage</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Authors>Steffen Skov</Authors>
<Company>Steffen Skov</Company>
<Description>Extension to SegregatedStorage adding support for MongoDB for storing metadata</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/SegregatedStorage/SegregatedStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Authors>Steffen Skov</Authors>
<Company>Steffen Skov</Company>
<Description>A small library for providing segregated storage abstraction</Description>
Expand Down
4 changes: 2 additions & 2 deletions src/SegregatedStorage/Services/IStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IStorageService<in TKey>
/// <param name="data">The actual data contents of the file.</param>
/// <param name="cancellationToken">CancellationToken, can be omitted</param>
/// <returns>Id of the file uploaded.</returns>
ValueTask<Guid> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);
ValueTask<FileAggregate> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);

/// <summary>
/// Uploads a file to the storage service using the given id.
Expand All @@ -24,7 +24,7 @@ public interface IStorageService<in TKey>
/// <param name="data">The actual data contents of the file.</param>
/// <param name="cancellationToken">CancellationToken, can be omitted</param>
/// <returns>Id of the file uploaded.</returns>
ValueTask UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);
ValueTask<FileAggregate> UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);

/// <summary>
/// Downloads a file from the storage service.
Expand Down
8 changes: 4 additions & 4 deletions src/SegregatedStorage/Services/StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ public StorageService(IKeyServiceLocator<TKey, IFileRepository> repositoryLocato
_repositoryLocator = repositoryLocator;
}

public async ValueTask<Guid> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
public async ValueTask<FileAggregate> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
{
var id = Guid.NewGuid();
await UploadAsync(key, id, filename, mimeType, data, cancellationToken);
return id;
return await UploadAsync(key, id, filename, mimeType, data, cancellationToken);
}

public async ValueTask UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
public async ValueTask<FileAggregate> UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
{
var repository = _repositoryLocator.GetService(key);
var storageProvider = _storageProviderLocator.GetService(key);
Expand All @@ -31,6 +30,7 @@ public async ValueTask UploadAsync(TKey key, Guid id, string filename, string mi
await storageProvider.UploadAsync(FilePathGenerator.GenerateFilePath(file.Id), data, cancellationToken);
file = file.Uploaded();
await repository.PersistAsync(file, CancellationToken.None);
return file;
}
catch (OperationCanceledException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public async Task UploadAsync_DoesNotExist_IsUploaded()
var ms = new MemoryStream(bytes);

// Act
var id = await _service.UploadAsync(42, "hello.txt", "text/plain", ms);
var uploadedFile = await _service.UploadAsync(42, "hello.txt", "text/plain", ms);

// Assert
var (file, data) = await _service.DownloadAsync(42, id);
var (file, data) = await _service.DownloadAsync(42, uploadedFile.Id);
Assert.NotNull(file);
Assert.NotNull(data);
}
Expand Down Expand Up @@ -97,14 +97,14 @@ public async Task DownloadAsync_Exists_IsDownloaded()
// Arrange
var bytes = "Hello world"u8.ToArray();
var ms = new MemoryStream(bytes);
var id = await _service.UploadAsync(42, "hello.txt", "text/plain", ms);
var uploadedFile = await _service.UploadAsync(42, "hello.txt", "text/plain", ms);

// Act
var (file, data) = await _service.DownloadAsync(42, id);
var (file, data) = await _service.DownloadAsync(42, uploadedFile.Id);
Assert.Equal("hello.txt", file.FileName);
Assert.Equal("text/plain", file.MimeType);
Assert.Equal(FileState.Available, file.State);
Assert.Equal(id, file.Id);
Assert.Equal(uploadedFile, file);

using var ms2 = new MemoryStream();
await data.CopyToAsync(ms2);
Expand Down Expand Up @@ -136,10 +136,10 @@ public async Task DeleteAsync_Exists_IsDeleted()
// Arrange
var bytes = "Hello world"u8.ToArray();
var ms = new MemoryStream(bytes);
var id = await _service.UploadAsync(42, "hello.txt", "text/plain", ms);
var uploadedFile = await _service.UploadAsync(42, "hello.txt", "text/plain", ms);

// Act
var ex = await Record.ExceptionAsync(async () => await _service.DeleteAsync(42, id));
var ex = await Record.ExceptionAsync(async () => await _service.DeleteAsync(42, uploadedFile.Id));

// Assert
Assert.Null(ex);
Expand Down

0 comments on commit a38a74e

Please sign in to comment.