mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-05 21:12:11 +00:00
109 lines
4.1 KiB
C#
109 lines
4.1 KiB
C#
|
|
using Microsoft.AspNetCore.Hosting;
|
||
|
|
using Microsoft.AspNetCore.Http;
|
||
|
|
using SeaHaven.Services.Interfaces;
|
||
|
|
|
||
|
|
namespace Api.SeaHavenIndustries.Infrastructure
|
||
|
|
{
|
||
|
|
public class FileStorageAdapter : IFileStoragePort
|
||
|
|
{
|
||
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
||
|
|
private readonly IHttpContextAccessor _httpContext;
|
||
|
|
|
||
|
|
public FileStorageAdapter(IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContext)
|
||
|
|
{
|
||
|
|
_webHostEnvironment = webHostEnvironment;
|
||
|
|
_httpContext = httpContext;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string> SaveFileAsync(IFormFile file)
|
||
|
|
{
|
||
|
|
var fileName = Path.GetFileName(file.FileName);
|
||
|
|
if (string.IsNullOrWhiteSpace(fileName))
|
||
|
|
throw new InvalidDataException("The uploaded file must have a valid name.");
|
||
|
|
|
||
|
|
var uniqueFileName = $"{Guid.NewGuid()}_{fileName}";
|
||
|
|
var uploadPath = Path.Combine("Assets", "Documents");
|
||
|
|
var webRoot = _webHostEnvironment.WebRootPath
|
||
|
|
?? Path.Combine(_webHostEnvironment.ContentRootPath, "wwwroot");
|
||
|
|
var fullPath = Path.Combine(webRoot, uploadPath, uniqueFileName);
|
||
|
|
|
||
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
|
||
|
|
|
||
|
|
using (var stream = System.IO.File.Create(fullPath))
|
||
|
|
{
|
||
|
|
await file.CopyToAsync(stream);
|
||
|
|
}
|
||
|
|
|
||
|
|
var request = _httpContext.HttpContext?.Request
|
||
|
|
?? throw new InvalidOperationException("An active HTTP request is required to build the file URL.");
|
||
|
|
var domain = $"{request.Scheme}://{request.Host}";
|
||
|
|
return $"{domain}/{uploadPath.Replace("\\", "/")}/{uniqueFileName}";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class DispatchEmailAdapter : IDispatchEmailPort
|
||
|
|
{
|
||
|
|
private readonly Helper.SendMessage _sendMessage;
|
||
|
|
|
||
|
|
public DispatchEmailAdapter(Helper.SendMessage sendMessage)
|
||
|
|
{
|
||
|
|
_sendMessage = sendMessage;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> SendDispatchEmailAsync(string emailTo, string subject, string htmlBody, string? replyTo)
|
||
|
|
{
|
||
|
|
return await _sendMessage.SendDispatchEmail(emailTo, subject, htmlBody, replyTo);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class VendorTokenAdapter : IVendorTokenPort
|
||
|
|
{
|
||
|
|
private readonly IVendorPortalTokenService _tokenService;
|
||
|
|
|
||
|
|
public VendorTokenAdapter(IVendorPortalTokenService tokenService)
|
||
|
|
{
|
||
|
|
_tokenService = tokenService;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string> GetOrCreateActiveTokenAsync(int vendorId)
|
||
|
|
{
|
||
|
|
var token = await _tokenService.GetOrCreateActiveTokenAsync(
|
||
|
|
vendorId, CancellationToken.None);
|
||
|
|
return token.Token;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class VendorDocumentStorageAdapter : IVendorDocumentStoragePort
|
||
|
|
{
|
||
|
|
private readonly IWebHostEnvironment _environment;
|
||
|
|
|
||
|
|
public VendorDocumentStorageAdapter(IWebHostEnvironment environment)
|
||
|
|
{
|
||
|
|
_environment = environment;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task SaveAsync(int vendorId, int dispatchId, string storedFileName, Stream content, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var path = Helper.VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, vendorId, dispatchId, storedFileName);
|
||
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||
|
|
await using var file = File.Create(path);
|
||
|
|
await content.CopyToAsync(file, cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public Stream OpenRead(int vendorId, int dispatchId, string storedFileName)
|
||
|
|
{
|
||
|
|
var path = Helper.VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, vendorId, dispatchId, storedFileName);
|
||
|
|
return File.OpenRead(path);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Delete(int vendorId, int dispatchId, string storedFileName)
|
||
|
|
{
|
||
|
|
var path = Helper.VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, vendorId, dispatchId, storedFileName);
|
||
|
|
if (File.Exists(path))
|
||
|
|
{
|
||
|
|
File.Delete(path);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|