using Core.Enumerations;
using DataProviders.Buckets;
using DomainObjects.Documents.Users;
using DomainResults.Common;
using DomainResults.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WeatherForecast.Controllers.Abstractions;
using WeatherForecast.Policies;
using WeatherForecast.Services;
namespace WeatherForecast.Controllers;
///
///
///
[Route("api/[controller]")]
public class ImagesController : AuthorizationControllerBase {
private readonly IImageBucketDataProvider _imageBucketDataProvider;
private readonly IImageService _imageService;
///
///
///
///
///
///
public ImagesController(
IAuthorizationService authorizationService,
IImageBucketDataProvider imageBucketDataProvider,
IImageService imageService
) : base (authorizationService) {
_imageBucketDataProvider = imageBucketDataProvider;
_imageService = imageService;
}
///
///
///
///
///
///
///
[HttpPost("{siteId}/{userId}")]
public async Task Post([FromRoute] Guid siteId, [FromRoute] Guid userId, List file) =>
await PostCore(siteId, userId, file);
private async Task PostCore(Guid siteId, Guid? userId, List file) {
if (file.Any(x => !(x.Length > 0)))
return IDomainResult.Failed().ToActionResult();
var newFiles = file.Select(x => {
using var ms = new MemoryStream();
x.CopyTo(ms);
var newFile = new BucketFile(Guid.NewGuid(), siteId, x.FileName, ms.ToArray(), x.ContentType);
if (userId != null)
newFile.UserId = userId;
return newFile;
}).ToList();
return await AuthorizeAsync(User, new ImageAuthorisationRequirement(CrudActions.Create, new List {
new ImageRole(Roles.Admin),
new ImageRole(Roles.Editor),
new ImageRole(Roles.Author) { OwnOnly = true },
new ImageRole(Roles.Contributor) { OwnOnly = true },
new ImageRole(Roles.ShopManager)
}), newFiles, () => {
var result = _imageService.Post(newFiles);
return result.ToActionResult();
});
}
}