148 lines
4.2 KiB
C#
148 lines
4.2 KiB
C#
using Core;
|
|
using Core.Enumerations;
|
|
using DataProviders;
|
|
using DataProviders.Buckets;
|
|
using DomainResults.Common;
|
|
using FileSecurityService;
|
|
|
|
namespace WeatherForecast.Services {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public interface IFileService {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
(Guid?, IDomainResult) Post(Guid siteId, Guid userId, IFormFile file);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <returns></returns>
|
|
(BucketFile?, IDomainResult) Get(Guid siteId, Guid userId, Guid fileId);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <returns></returns>
|
|
IDomainResult Delete(Guid siteId, Guid userId, Guid fileId);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class FileService : IFileService {
|
|
|
|
private readonly ILogger<FilesService> _logger;
|
|
private readonly IFileSecurityService _fileSecurityService;
|
|
private readonly IBucketDataProvider _imageBucketDataProvider;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
/// <param name="fileSecurityService"></param>
|
|
/// <param name="imageBucketDataProvider"></param>
|
|
public FileService(
|
|
ILogger<FilesService> logger,
|
|
IFileSecurityService fileSecurityService,
|
|
IBucketDataProvider imageBucketDataProvider
|
|
) {
|
|
_logger = logger;
|
|
_fileSecurityService = fileSecurityService;
|
|
_imageBucketDataProvider = imageBucketDataProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
public (Guid?, IDomainResult) Post(Guid siteId, Guid userId, IFormFile file) {
|
|
try {
|
|
if (!(file.Length > 0))
|
|
return IDomainResult.Failed<Guid?>();
|
|
|
|
using var ms = new MemoryStream();
|
|
file.CopyTo(ms);
|
|
var bytes = ms.ToArray();
|
|
|
|
var (mediaType, signatureResult) = _fileSecurityService.CheckFileSignature(file.FileName, bytes, file.ContentType);
|
|
if(!signatureResult.IsSuccess || mediaType == null)
|
|
return IDomainResult.Failed<Guid?>();
|
|
|
|
var bucketFile = new BucketFile(file.FileName, bytes, file.ContentType);
|
|
|
|
IDomainResult result;
|
|
Guid? fileId;
|
|
|
|
if (mediaType == MediaTypes.Image)
|
|
(fileId, result) = _imageBucketDataProvider.Upload(siteId, userId, bucketFile);
|
|
else
|
|
return IDomainResult.Failed<Guid?>();
|
|
|
|
if (!result.IsSuccess || fileId == null)
|
|
return IDomainResult.Failed<Guid?>();
|
|
|
|
return IDomainResult.Success(fileId);
|
|
}
|
|
catch (Exception ex) {
|
|
_logger.LogError(ex, "Unhandled exception");
|
|
return IDomainResult.Failed<Guid?>(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <returns></returns>
|
|
public (BucketFile?, IDomainResult) Get(Guid siteId, Guid userId, Guid fileId) {
|
|
try {
|
|
var (file, result) = _imageBucketDataProvider.Download(siteId, userId, fileId);
|
|
if (!result.IsSuccess || file == null)
|
|
return (null, result);
|
|
|
|
return IDomainResult.Success(file);
|
|
}
|
|
catch (Exception ex) {
|
|
_logger.LogError(ex, "Unhandled exception");
|
|
return IDomainResult.Failed<BucketFile?>(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <returns></returns>
|
|
public IDomainResult Delete(Guid siteId, Guid userId, Guid fileId) {
|
|
try {
|
|
return _imageBucketDataProvider.DeleteOne(siteId, userId, fileId);
|
|
}
|
|
catch (Exception ex) {
|
|
_logger.LogError(ex, "Unhandled exception");
|
|
return IDomainResult.Failed(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|