mirror of
https://github.com/D4M13N-D3V/comissions-app-core-api.git
synced 2025-03-14 10:04:55 +00:00
feat: added local storage service
This commit is contained in:
parent
dc43dbd701
commit
5da91f6bb0
@ -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<IStorageService,ImgCdnStorageServiceProvider>();
|
||||
builder.Services.AddSingleton<IStorageService,LocalStorageServiceProvider>();
|
||||
builder.Services.AddSingleton<IPaymentService,StripePaymentServiceProvider>();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user