feat: added local storage service

This commit is contained in:
Damien Ostler 2024-02-15 01:25:12 -05:00
parent dc43dbd701
commit 5da91f6bb0
3 changed files with 56 additions and 2 deletions

View File

@ -6,6 +6,7 @@ using comissions.app.api.Services.Payment;
using comissions.app.api.Services.Storage; using comissions.app.api.Services.Storage;
using ArtPlatform.Database; using ArtPlatform.Database;
using Auth0.AspNetCore.Authentication; using Auth0.AspNetCore.Authentication;
using comissions.app.api.Services.Storage.comissions.app.api.Services.Storage;
using comissions.app.database; using comissions.app.database;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@ -19,7 +20,7 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddSingleton<IStorageService,ImgCdnStorageServiceProvider>(); builder.Services.AddSingleton<IStorageService,LocalStorageServiceProvider>();
builder.Services.AddSingleton<IPaymentService,StripePaymentServiceProvider>(); builder.Services.AddSingleton<IPaymentService,StripePaymentServiceProvider>();
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();

View File

@ -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<string> 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<Stream> 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);
}
}
}