Skip to content

Commit

Permalink
Initial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Anisenko committed Feb 28, 2024
1 parent 0c6eaf6 commit cf30c91
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 163 deletions.
46 changes: 25 additions & 21 deletions ManagedCode.Storage.Client/StorageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,7 @@ public void SetChunkSize(long size)

public event EventHandler<ProgressStatus>? OnProgressStatusChanged;

public async Task<Result<BlobMetadata>> UploadFile(Stream stream, string apiUrl, string contentName, CancellationToken cancellationToken = default)
{
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)
{
return await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
}

var 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());
Expand Down Expand Up @@ -211,9 +195,19 @@ public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, CancellationTok
throw new NotImplementedException();
}

public Task<Result<BlobMetadata>> UploadAsync(Stream stream, UploadOptions options, CancellationToken cancellationToken = default)
public async Task<Result<BlobMetadata>> UploadAsync(Stream stream, UploadOptions options, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
using var streamContent = new StreamContent(stream);

var response = await _httpClient.PostAsync(options.ApiEndpoint, streamContent, cancellationToken);

if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
}

var content = await response.Content.ReadAsStringAsync(cancellationToken: cancellationToken);
return Result<BlobMetadata>.Fail(response.StatusCode, content);
}

public Task<Result<BlobMetadata>> UploadAsync(byte[] data, UploadOptions options, CancellationToken cancellationToken = default)
Expand All @@ -226,9 +220,19 @@ public Task<Result<BlobMetadata>> UploadAsync(string content, UploadOptions opti
throw new NotImplementedException();
}

public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, UploadOptions options, CancellationToken cancellationToken = default)
public async Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, UploadOptions options, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
using var streamContent = new StreamContent(fileInfo.OpenRead());

var response = await _httpClient.PostAsync(options.ApiEndpoint, streamContent, cancellationToken);

if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
}

var content = await response.Content.ReadAsStringAsync(cancellationToken: cancellationToken);
return Result<BlobMetadata>.Fail(response.StatusCode, content);
}

public Task<Result<BlobMetadata>> UploadAsync(Stream stream, Action<UploadOptions> action, CancellationToken cancellationToken = default)
Expand Down
2 changes: 2 additions & 0 deletions ManagedCode.Storage.Core/Models/BaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public abstract class BaseOptions
public string FileName { get; set; } = $"{Guid.NewGuid():N}";
public string? Directory { get; set; }

public string? ApiEndpoint { get; set; }

// TODO: Check this
public string FullPath => string.IsNullOrWhiteSpace(Directory) ? FileName : $"{Directory}/{FileName}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public static class ApiEndpoints
{
public const string Azure = "azure";
public const string Files = "files";

public static class Base
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace ManagedCode.Storage.IntegrationTests.TestApp.Controllers.Base;

[ApiController]
public abstract class BaseTestController<TStorage> : ControllerBase
where TStorage : IStorage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ManagedCode.Communication;
using ManagedCode.Storage.Core.Models;
using ManagedCode.Storage.FileSystem;
using ManagedCode.Storage.IntegrationTests.TestApp.Controllers.Base;
using ManagedCode.Storage.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ManagedCode.Storage.IntegrationTests.TestApp.Controllers;

[Route("files")]
[ApiController]
public class FilesTestController(IFileSystemStorage storage) : BaseTestController<IFileSystemStorage>(storage)
{
[HttpPost("upload")]
public async Task<Result<BlobMetadata>> UploadToStream(
[FromBody] Stream streamFile,
[FromQuery] UploadOptions? options = null
)
{
return await Storage.UploadAsync(streamFile, options);
}

// [HttpPost("upload")]
// public async Task<Result<BlobMetadata>> UploadToFile([FromBody] IFormFile file,
// UploadOptions? options = null,
// CancellationToken cancellationToken = default)
// {
// var f = await file.ToLocalFileAsync();
// return await Storage.UploadAsync(f.FileInfo, options, cancellationToken);
// }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ManagedCode.Storage.Client;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Xunit;

Expand All @@ -9,20 +10,22 @@ public abstract class BaseControllerTests
{
protected readonly StorageTestApplication TestApplication;
protected readonly string ApiEndpoint;
private static readonly string baseTestApi = "https://localhost:44332/";

protected BaseControllerTests(StorageTestApplication testApplication, string apiEndpoint)
{
TestApplication = testApplication;
ApiEndpoint = apiEndpoint;
}

protected HttpClient GetHttpClient()
protected HttpClient GetHttpClient(string baseAddress = null)
{
return TestApplication.CreateClient();
baseAddress ??= baseTestApi;
return TestApplication.CreateClient(new WebApplicationFactoryClientOptions{ BaseAddress = new Uri(baseAddress)});
}

protected IStorageClient GetStorageClient()
{
return new StorageClient(TestApplication.CreateClient());
return new Client.StorageClient(GetHttpClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ protected BaseDownloadControllerTests(StorageTestApplication testApplication, st
_downloadEndpoint = string.Format(ApiEndpoints.Base.DownloadFile, ApiEndpoint);
}

[Fact]
public async Task DownloadFile_WhenFileExists_SaveToTempStorage_ReturnSuccess()
{
// Arrange
var storageClient = GetStorageClient();
var contentName = "file";

await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
FileHelper.GenerateLocalFile(localFile, 1);
var fileCRC = Crc32Helper.Calculate(await localFile.ReadAllBytesAsync());
var uploadFileBlob = await storageClient.UploadFile(localFile.FileStream, _uploadEndpoint, contentName);

// Act
var downloadedFileResult = await storageClient.DownloadFile(uploadFileBlob.Value.FullName, _downloadEndpoint);

// Assert
downloadedFileResult.IsSuccess.Should().BeTrue();
downloadedFileResult.Value.Should().NotBeNull();
var downloadedFileCRC = Crc32Helper.CalculateFileCRC(downloadedFileResult.Value.FilePath);
downloadedFileCRC.Should().Be(fileCRC);
}

[Fact]
public async Task DownloadFile_WhenFileDoNotExist_ReturnFail()
{
// Arrange
var storageClient = GetStorageClient();;

// Act
var downloadedFileResult = await storageClient.DownloadFile(Guid.NewGuid().ToString(), _downloadEndpoint);

// Assert
downloadedFileResult.IsFailed.Should().BeTrue();
downloadedFileResult.GetError().Value.ErrorCode.Should().Be(HttpStatusCode.InternalServerError.ToString());
}
// [Fact]
// public async Task DownloadFile_WhenFileExists_SaveToTempStorage_ReturnSuccess()
// {
// // Arrange
// var storageClient = GetStorageClient();
// var contentName = "file";
//
// await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
// FileHelper.GenerateLocalFile(localFile, 1);
// var fileCRC = Crc32Helper.Calculate(await localFile.ReadAllBytesAsync());
// var uploadFileBlob = await storageClient.UploadFile(localFile.FileStream, _uploadEndpoint, contentName);
//
// // Act
// var downloadedFileResult = await storageClient.DownloadFile(uploadFileBlob.Value.FullName, _downloadEndpoint);
//
// // Assert
// downloadedFileResult.IsSuccess.Should().BeTrue();
// downloadedFileResult.Value.Should().NotBeNull();
// var downloadedFileCRC = Crc32Helper.CalculateFileCRC(downloadedFileResult.Value.FilePath);
// downloadedFileCRC.Should().Be(fileCRC);
// }
//
// [Fact]
// public async Task DownloadFile_WhenFileDoNotExist_ReturnFail()
// {
// // Arrange
// var storageClient = GetStorageClient();;
//
// // Act
// var downloadedFileResult = await storageClient.DownloadFile(Guid.NewGuid().ToString(), _downloadEndpoint);
//
// // Assert
// downloadedFileResult.IsFailed.Should().BeTrue();
// downloadedFileResult.GetError().Value.ErrorCode.Should().Be(HttpStatusCode.InternalServerError.ToString());
// }
}
Loading

0 comments on commit cf30c91

Please sign in to comment.