Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Threading.Tasks;
  6. using Kicksharing.Contracts.Dtos;
  7. using Kicksharing.Domain.Entities;
  8. using Kicksharing.Domain.Extensions;
  9. using Kicksharing.Infrastructure.Commands;
  10. using Kicksharing.Infrastructure.Controllers;
  11. using Kicksharing.Infrastructure.Services.Interfaces;
  12. using Kicksharing.Infrastructure.Specifications;
  13. using Kicksharing.Infrastructure.Translation;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Http.Features;
  16. using Microsoft.AspNetCore.WebUtilities;
  17. using Microsoft.Net.Http.Headers;
  18. using Kicksharing.Infrastructure.Services;
  19. using Kicksharing.Domain.Services;
  20. using Kicksharing.Infrastructure.Services.Auth;
  21. using Kicksharing.WebApi.Services;
  22.  
  23. namespace Kicksharing.WebApi.Commands.File
  24. {
  25.     class StoreFileCommand : WebCommand<CommandResult<FileDto>>
  26.     {
  27.         private static readonly FormOptions DefaultFormOptions = new FormOptions();
  28.  
  29.         private readonly bool _isPrivate;
  30.         private readonly IRepository _repository;
  31.         private readonly ITranslatorFactory _translatorFactory;
  32.         private readonly IFileHelperService _fileHelperService;
  33.         private readonly S3InteractionService _s3InteractionService;
  34.         private readonly ICurrentUserProvider _currentUserProvider;
  35.         private HttpRequest Request { get; }
  36.  
  37.         public StoreFileCommand(HttpRequest request,
  38.             bool isPrivate,
  39.             IRepository repository,
  40.             ITranslatorFactory translatorFactory,
  41.             ILogger logger,
  42.             IFileHelperService fileHelperService,
  43.             S3InteractionService s3InteractionService,
  44.             ICurrentUserProvider currentUserProvider)
  45.             :base(logger)
  46.         {
  47.             Request = request;
  48.             _isPrivate = isPrivate;
  49.             _repository = repository;
  50.             _translatorFactory = translatorFactory;
  51.             _fileHelperService = fileHelperService;
  52.             _s3InteractionService = s3InteractionService;
  53.             _currentUserProvider = currentUserProvider;
  54.         }
  55.  
  56.         protected override async Task Body()
  57.         {
  58.             var file = new UploadFile
  59.             {
  60.                 StoredFileName = Guid.NewGuid().ToString(),
  61.                 FileName = ContentDispositionHeaderValue.Parse(Request.Form.Files.First().ContentDisposition).FileName.Value,
  62.                 Owner = _currentUserProvider.CurrentUser,
  63.                 IsPrivate = _isPrivate
  64.             };
  65.             file.MarkAsNew();
  66.             _repository.Save(file);
  67.            
  68.            
  69.             var boundary = MultipartRequestHelper.GetBoundary(
  70.                 MediaTypeHeaderValue.Parse(Request.ContentType),
  71.                 DefaultFormOptions.MultipartBoundaryLengthLimit);
  72.             var reader = new MultipartReader(boundary, Request.Body);
  73.  
  74.             using (var dst = new MemoryStream())
  75.             {
  76.                 var section = await reader.ReadNextSectionAsync();
  77.                 while (section != null)
  78.                 {
  79.                     var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
  80.                     if (hasContentDispositionHeader)
  81.                     {
  82.                         if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
  83.                         {
  84.                            
  85.                                 await section.Body.CopyToAsync(dst);
  86.                            
  87.                         }
  88.                     }
  89.                
  90.                     section = await reader.ReadNextSectionAsync();
  91.                 }
  92.  
  93.                 if (!await _s3InteractionService.SaveFile(file, dst))
  94.                 {
  95.                     SetFailed($"Cannot save file {file.FileName}", 301, HttpStatusCode.InternalServerError);
  96.                 }
  97.             }
  98.            
  99.  
  100.             file.MarkAsModified();
  101.             _repository.Save(file);
  102.             var translator = _translatorFactory.GetTranslator<UploadFile, FileDto>();
  103.             var dto        = translator.Translate(file);
  104.             SetSuccess(CommandResult<FileDto>.CreateSuccess(dto), HttpStatusCode.OK);
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement