78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
public class ImagesController : AuthorizationControllerBase {
|
|
|
|
|
|
private readonly IImageBucketDataProvider _imageBucketDataProvider;
|
|
private readonly IImageService _imageService;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="authorizationService"></param>
|
|
/// <param name="imageBucketDataProvider"></param>
|
|
/// <param name="imageService"></param>
|
|
public ImagesController(
|
|
IAuthorizationService authorizationService,
|
|
IImageBucketDataProvider imageBucketDataProvider,
|
|
IImageService imageService
|
|
) : base (authorizationService) {
|
|
_imageBucketDataProvider = imageBucketDataProvider;
|
|
_imageService = imageService;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="userId"></param>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("{siteId}/{userId}")]
|
|
public async Task<IActionResult> Post([FromRoute] Guid siteId, [FromRoute] Guid userId, List<IFormFile> file) =>
|
|
await PostCore(siteId, userId, file);
|
|
|
|
private async Task<IActionResult> PostCore(Guid siteId, Guid? userId, List<IFormFile> 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<ImageRole> {
|
|
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();
|
|
});
|
|
}
|
|
}
|