Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- using Kicksharing.Contracts.Dtos;
- using Kicksharing.Domain.Entities;
- using Kicksharing.Domain.Extensions;
- using Kicksharing.Infrastructure.Commands;
- using Kicksharing.Infrastructure.Controllers;
- using Kicksharing.Infrastructure.Services.Interfaces;
- using Kicksharing.Infrastructure.Specifications;
- using Kicksharing.Infrastructure.Translation;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.AspNetCore.WebUtilities;
- using Microsoft.Net.Http.Headers;
- using Kicksharing.Infrastructure.Services;
- using Kicksharing.Domain.Services;
- using Kicksharing.Infrastructure.Services.Auth;
- using Kicksharing.WebApi.Services;
- namespace Kicksharing.WebApi.Commands.File
- {
- class StoreFileCommand : WebCommand<CommandResult<FileDto>>
- {
- private static readonly FormOptions DefaultFormOptions = new FormOptions();
- private readonly bool _isPrivate;
- private readonly IRepository _repository;
- private readonly ITranslatorFactory _translatorFactory;
- private readonly IFileHelperService _fileHelperService;
- private readonly S3InteractionService _s3InteractionService;
- private readonly ICurrentUserProvider _currentUserProvider;
- private HttpRequest Request { get; }
- public StoreFileCommand(HttpRequest request,
- bool isPrivate,
- IRepository repository,
- ITranslatorFactory translatorFactory,
- ILogger logger,
- IFileHelperService fileHelperService,
- S3InteractionService s3InteractionService,
- ICurrentUserProvider currentUserProvider)
- :base(logger)
- {
- Request = request;
- _isPrivate = isPrivate;
- _repository = repository;
- _translatorFactory = translatorFactory;
- _fileHelperService = fileHelperService;
- _s3InteractionService = s3InteractionService;
- _currentUserProvider = currentUserProvider;
- }
- protected override async Task Body()
- {
- var file = new UploadFile
- {
- StoredFileName = Guid.NewGuid().ToString(),
- FileName = ContentDispositionHeaderValue.Parse(Request.Form.Files.First().ContentDisposition).FileName.Value,
- Owner = _currentUserProvider.CurrentUser,
- IsPrivate = _isPrivate
- };
- file.MarkAsNew();
- _repository.Save(file);
- var boundary = MultipartRequestHelper.GetBoundary(
- MediaTypeHeaderValue.Parse(Request.ContentType),
- DefaultFormOptions.MultipartBoundaryLengthLimit);
- var reader = new MultipartReader(boundary, Request.Body);
- using (var dst = new MemoryStream())
- {
- var section = await reader.ReadNextSectionAsync();
- while (section != null)
- {
- var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
- if (hasContentDispositionHeader)
- {
- if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
- {
- await section.Body.CopyToAsync(dst);
- }
- }
- section = await reader.ReadNextSectionAsync();
- }
- if (!await _s3InteractionService.SaveFile(file, dst))
- {
- SetFailed($"Cannot save file {file.FileName}", 301, HttpStatusCode.InternalServerError);
- }
- }
- file.MarkAsModified();
- _repository.Save(file);
- var translator = _translatorFactory.GetTranslator<UploadFile, FileDto>();
- var dto = translator.Translate(file);
- SetSuccess(CommandResult<FileDto>.CreateSuccess(dto), HttpStatusCode.OK);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement