using DataProviders;
using DataProviders.Buckets;
using DomainResults.Common;
using Microsoft.AspNetCore.Mvc;
namespace WeatherForecast.Services {
///
///
///
public interface IFilesService {
///
///
///
///
///
///
///
(List?, IDomainResult) Post(Guid siteId, Guid userId, List files);
///
///
///
///
///
///
IDomainResult Delete(Guid siteId, Guid userId);
}
///
///
///
public class FilesService : IFilesService {
private readonly ILogger _logger;
private readonly IImagesBucketDataProvider _imageBucketDataProvider;
///
///
///
///
///
public FilesService(
ILogger logger,
IImagesBucketDataProvider imageBucketDataProvider
) {
_logger = logger;
_imageBucketDataProvider = imageBucketDataProvider;
}
///
/// Process uploaded files
///
///
///
///
///
public (List?, IDomainResult) Post(Guid siteId, Guid userId, List files) {
try {
// Don't rely on or trust the FileName property without validation.
var newFiles = new List();
foreach (var formFile in files) {
if (formFile.Length > 0) {
using var ms = new MemoryStream();
formFile.CopyTo(ms);
newFiles.Add(new BucketFile(formFile.FileName, ms.ToArray(), formFile.ContentType));
}
}
var (list, result) = _imageBucketDataProvider.UploadMany(siteId, userId, newFiles);
if (!result.IsSuccess || list == null)
return IDomainResult.Failed?>();
return IDomainResult.Success(list);
}
catch (Exception ex) {
return IDomainResult.Failed?> (ex.Message);
}
}
///
///
///
///
///
///
public IDomainResult Delete(Guid siteId, Guid userId) {
try {
return _imageBucketDataProvider.DeletMany(siteId, userId);
}
catch (Exception ex) {
return IDomainResult.Failed(ex.Message);
}
}
}
}