mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
4f3f4ec066
Moved service implementations and interfaces from Govor.API and Govor.Core to new Govor.Application and Govor.Contracts projects. Updated namespaces and references throughout the solution. Added custom exception classes for authentication and invite services. Adjusted dependency injection and project references to use the new structure. This refactor improves separation of concerns and prepares the codebase for better maintainability and scalability.
63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using Govor.Application.Interfaces;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
namespace Govor.Application.Services;
|
|
|
|
public class LocalStorageService : IStorageService
|
|
{
|
|
private readonly string _storagePath;
|
|
|
|
public LocalStorageService(string contentRootPath)
|
|
{
|
|
_storagePath = Path.Combine(contentRootPath, "uploads");
|
|
|
|
if (!Directory.Exists(_storagePath))
|
|
{
|
|
Directory.CreateDirectory(_storagePath);
|
|
}
|
|
}
|
|
|
|
public async Task<string> SaveAsync(byte[] data, string fileName)
|
|
{
|
|
if(data is null || data.Length == 0)
|
|
throw new ArgumentException("Invalid file", nameof(data));
|
|
|
|
if(fileName is null || fileName.Length == 0)
|
|
throw new ArgumentException("Invalid file name", nameof(fileName));
|
|
|
|
var date = DateTime.UtcNow.ToString("yyyy/MM");
|
|
|
|
var folder = Path.Combine(_storagePath, date);
|
|
Directory.CreateDirectory(folder);
|
|
|
|
var uniqueFileName = $"{Guid.NewGuid()}_{Path.GetFileName(fileName)}";
|
|
var fullPath = Path.Combine(folder, uniqueFileName);
|
|
|
|
await using var stream = new FileStream(fullPath, FileMode.Create);
|
|
stream.WriteAsync(data, 0, data.Length);
|
|
|
|
return Path.Combine(date, uniqueFileName);
|
|
}
|
|
|
|
public async Task<Stream> LoadAsync(string url)
|
|
{
|
|
var filePath = Path.Combine(_storagePath, url);
|
|
|
|
if (!File.Exists(filePath))
|
|
throw new FileNotFoundException("File not found.", filePath);
|
|
|
|
return new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
|
}
|
|
|
|
public async Task RemoveAsync(string url)
|
|
{
|
|
var path = Path.Combine(_storagePath, url);
|
|
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
}
|