Skip to content

Commit

Permalink
refactor Client vs Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Anisenko committed Feb 28, 2024
1 parent cf30c91 commit 34e6099
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
33 changes: 17 additions & 16 deletions ManagedCode.Storage.Client/StorageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ManagedCode.Communication;
Expand Down Expand Up @@ -198,32 +199,31 @@ public Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, CancellationTok
public async Task<Result<BlobMetadata>> UploadAsync(Stream stream, UploadOptions options, CancellationToken cancellationToken = default)
{
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);
return await PostStreamContentAsync(options, cancellationToken, streamContent);
}

public Task<Result<BlobMetadata>> UploadAsync(byte[] data, UploadOptions options, CancellationToken cancellationToken = default)
public async Task<Result<BlobMetadata>> UploadAsync(byte[] data, UploadOptions options, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
MemoryStream stream = new MemoryStream(data);
using var streamContent = new StreamContent(stream);
return await PostStreamContentAsync(options, cancellationToken, streamContent); }

public Task<Result<BlobMetadata>> UploadAsync(string content, UploadOptions options, CancellationToken cancellationToken = default)
public async Task<Result<BlobMetadata>> UploadAsync(string content, UploadOptions options, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(content));
using var streamContent = new StreamContent(stream);
return await PostStreamContentAsync(options, cancellationToken, streamContent);
}

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

private async Task<Result<BlobMetadata>> PostStreamContentAsync(UploadOptions options, CancellationToken cancellationToken,
StreamContent streamContent)
{
var response = await _httpClient.PostAsync(options.ApiEndpoint, streamContent, cancellationToken);

if (response.IsSuccessStatusCode)
Expand All @@ -232,9 +232,10 @@ public async Task<Result<BlobMetadata>> UploadAsync(FileInfo fileInfo, UploadOpt
}

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


public Task<Result<BlobMetadata>> UploadAsync(Stream stream, Action<UploadOptions> action, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public static class ApiEndpoints

public static class Base
{
public const string UploadFile = "{0}/upload";
public const string UploadStream = "{0}/UploadStream";
public const string UploadFile = "{0}/UploadFile";
public const string UploadLargeFile = "{0}/upload-chunks";
public const string DownloadFile = "{0}/download";
}
Expand Down
2 changes: 1 addition & 1 deletion ManagedCode.Storage.IntegrationTests/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class FileHelper

public static LocalFile GenerateLocalFile(LocalFile file, int sizeInMegabytes)
{
var sizeInBytes = sizeInMegabytes * 1024 * 1024;
var sizeInBytes = sizeInMegabytes * 1024;

using (var fileStream = file.FileStream)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ namespace ManagedCode.Storage.IntegrationTests.TestApp.Controllers;
[ApiController]
public class FilesTestController(IFileSystemStorage storage) : BaseTestController<IFileSystemStorage>(storage)
{
[HttpPost("upload")]
public async Task<Result<BlobMetadata>> UploadToStream(
[HttpPost("UploadStream")]
public async Task<Result<BlobMetadata>> UploadStream(
[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);
// }
[HttpPost("UploadFile")]
public async Task<Result<BlobMetadata>> UploadToFile(
[FromBody] IFormFile formFile,
[FromQuery] UploadOptions? options = null)
{
return await Storage.UploadToStorageAsync(formFile, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ namespace ManagedCode.Storage.IntegrationTests.Tests;

public abstract class BaseUploadControllerTests : BaseControllerTests
{
private readonly string _uploadEndpoint;
private readonly string _streamEndpoint;
private readonly string _fileEndpoint;
private readonly string _uploadLargeFile;

protected BaseUploadControllerTests(StorageTestApplication testApplication, string apiEndpoint) : base(testApplication, apiEndpoint)
{
_uploadEndpoint = string.Format(ApiEndpoints.Base.UploadFile, ApiEndpoint);
_fileEndpoint = string.Format(ApiEndpoints.Base.UploadFile, ApiEndpoint);
_streamEndpoint = string.Format(ApiEndpoints.Base.UploadStream, ApiEndpoint);
_uploadLargeFile = string.Format(ApiEndpoints.Base.UploadLargeFile, ApiEndpoint);
}

Expand All @@ -24,7 +26,7 @@ public async Task UploadFileFromStream_WhenFileValid_ReturnSuccess()
var storageClient = GetStorageClient();
await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
FileHelper.GenerateLocalFile(localFile, 1);
var options = new UploadOptions { ApiEndpoint = _uploadEndpoint};
var options = new UploadOptions { ApiEndpoint = _streamEndpoint};

var result = await storageClient.UploadAsync(localFile.FileStream, options);

Expand All @@ -38,7 +40,7 @@ public async Task UploadFileFromFileInfo_WhenFileValid_ReturnSuccess()
var storageClient = GetStorageClient();
await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
FileHelper.GenerateLocalFile(localFile, 1);
var options = new UploadOptions { ApiEndpoint = _uploadEndpoint};
var options = new UploadOptions { ApiEndpoint = _fileEndpoint};

var result = await storageClient.UploadAsync(localFile.FileInfo, options);

Expand Down
9 changes: 9 additions & 0 deletions ManagedCode.Storage.Server/Controllers/FilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ManagedCode.Storage.Core.Models;
using ManagedCode.Storage.FileSystem;
using ManagedCode.Storage.FileSystem.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ManagedCode.Storage.Server;
Expand All @@ -22,4 +23,12 @@ public async Task<Result<BlobMetadata>> UploadStream(
return await Storage.UploadAsync(streamFile, options);
}

[HttpPost("upload")]
public async Task<Result<BlobMetadata>> UploadFile(
[FromBody] IFormFile formFile,
[FromQuery] UploadOptions? options = null)
{
FileSystemStorage Storage = new (new FileSystemStorageOptions{ BaseFolder = options?.Directory});
return await Storage.UploadToStorageAsync(formFile, options);
}
}

0 comments on commit 34e6099

Please sign in to comment.