83 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Enumerations;
 | |
| using DataProviders;
 | |
| using DataProviders.Buckets;
 | |
| using DomainResults.Common;
 | |
| using DomainResults.Mvc;
 | |
| using Microsoft.AspNetCore.Authorization;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using WeatherForecast.Policies;
 | |
| 
 | |
| namespace WeatherForecast.Controllers;
 | |
| 
 | |
| /// <summary>
 | |
| /// 
 | |
| /// </summary>
 | |
| [AllowAnonymous]
 | |
| [Route("api/[controller]")]
 | |
| public class FilesController : ControllerBase {
 | |
| 
 | |
|   private readonly IAuthorizationService _authorizationService;
 | |
|   private readonly IImageBucketDataProvider _imageBucketDataProvider;
 | |
| 
 | |
|   /// <summary>
 | |
|   /// 
 | |
|   /// </summary>
 | |
|   /// <param name="filesService"></param>
 | |
|   public FilesController(
 | |
|     IAuthorizationService authorizationService,
 | |
|     IImageBucketDataProvider imageBucketDataProvider
 | |
|   ) {
 | |
|     _authorizationService = authorizationService;
 | |
|     _imageBucketDataProvider = imageBucketDataProvider;
 | |
|   }
 | |
| 
 | |
|   /// <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) {
 | |
| 
 | |
|     if (file.Any(x => !(x.Length > 0)))
 | |
|       return IDomainResult.Failed().ToActionResult();
 | |
| 
 | |
|     var newFiles = file.Select(x => {
 | |
|       using var ms = new MemoryStream();
 | |
|       x.CopyTo(ms);
 | |
|       return new BucketFile(Guid.NewGuid(), siteId, userId, x.FileName, ms.ToArray(), x.ContentType);
 | |
|     }).ToList();
 | |
| 
 | |
|     if ((await _authorizationService.AuthorizeAsync(User, newFiles, new FileAuthorisationRequirement {
 | |
|       Action = CrudActions.Create
 | |
|     })).Succeeded) {
 | |
|       var result = _imageBucketDataProvider.UploadMany(newFiles);
 | |
|       return result.ToActionResult();
 | |
|     }
 | |
| 
 | |
|     return Unauthorized();
 | |
|   }
 | |
| 
 | |
| 
 | |
|   //[HttpDelete("{siteId}/{userId}")]
 | |
|   //public async Task<IActionResult> Delete([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromBody] FileRequestModel requestData) {
 | |
| 
 | |
|   //  var (files, fileDownloadResult) = _imageBucketDataProvider.DownloadMany(siteId, userId, requestData.Ids);
 | |
|   //  if (!fileDownloadResult.IsSuccess || files == null)
 | |
|   //    return fileDownloadResult.ToActionResult();
 | |
| 
 | |
|   //  if ((await _authorizationService.AuthorizeAsync(User, files, new FileAuthorisationRequirement {
 | |
|   //    WhiteListToken = true,
 | |
|   //    SameAuthor = true,
 | |
|   //    Action = CrudActions.Delete
 | |
|   //  })).Succeeded) {
 | |
|   //    var result = _imageBucketDataProvider.DeletMany(siteId, userId);
 | |
|   //    return result.ToActionResult();
 | |
|   //  }
 | |
| 
 | |
|   // return  Unauthorized();
 | |
|   //}
 | |
| }
 |