diff --git a/src/comissions.app.api/Program.cs b/src/comissions.app.api/Program.cs index 45c1980..250e860 100644 --- a/src/comissions.app.api/Program.cs +++ b/src/comissions.app.api/Program.cs @@ -6,6 +6,7 @@ using comissions.app.api.Services.Payment; using comissions.app.api.Services.Storage; using ArtPlatform.Database; using Auth0.AspNetCore.Authentication; +using comissions.app.api.Services.Storage.comissions.app.api.Services.Storage; using comissions.app.database; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; @@ -19,7 +20,7 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHttpContextAccessor(); diff --git a/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs b/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs index 69dbe3b..9d3e4ed 100644 --- a/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs +++ b/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs @@ -24,7 +24,7 @@ namespace comissions.app.api.Services.Storage using var content = new MultipartFormDataContent(); content.Add(new StringContent(ApiKey), "key"); content.Add(new StreamContent(fileStream), "source", fileName); - + var response = await _client.PostAsync("api/1/upload", content); if (!response.IsSuccessStatusCode) diff --git a/src/comissions.app.api/Services/Storage/LocalStorageServiceProvider.cs b/src/comissions.app.api/Services/Storage/LocalStorageServiceProvider.cs new file mode 100644 index 0000000..26a9703 --- /dev/null +++ b/src/comissions.app.api/Services/Storage/LocalStorageServiceProvider.cs @@ -0,0 +1,53 @@ + +using System; +using System.IO; +using System.Threading.Tasks; + +namespace comissions.app.api.Services.Storage +{ + public class LocalStorageServiceProvider : IStorageService + { + private readonly string _storageFolderPath; + + public LocalStorageServiceProvider(string storageFolderPath) + { + _storageFolderPath = storageFolderPath; + + // Create storage folder if it does not exist + if (!Directory.Exists(_storageFolderPath)) + { + Directory.CreateDirectory(_storageFolderPath); + } + } + + public async Task UploadImageAsync(Stream fileStream, string fileName) + { + // Generate a GUID for the file reference + string fileReference = Guid.NewGuid().ToString(); + + // Save the file to the storage folder with the GUID as the file name + string filePath = Path.Combine(_storageFolderPath, fileReference); + using (FileStream outputFileStream = File.Create(filePath)) + { + await fileStream.CopyToAsync(outputFileStream); + } + + return fileReference; + } + + public async Task DownloadImageAsync(string fileReference) + { + // Get the file path based on the provided file reference + string filePath = Path.Combine(_storageFolderPath, fileReference); + + // Check if the file exists + if (!File.Exists(filePath)) + { + throw new FileNotFoundException("File not found", filePath); + } + + // Open the file and return the stream + return new FileStream(filePath, FileMode.Open, FileAccess.Read); + } + } +}