Ejemplo n.º 1
0
        public async Task <IActionResult> UploadFile(Guid sessionId, int templateCode)
        {
            var multipartBoundary = Request.GetMultipartBoundary();

            if (string.IsNullOrEmpty(multipartBoundary))
            {
                return(BadRequest($"Expected a multipart request, but got '{Request.ContentType}'."));
            }

            MultipartUploadSession uploadSession = null;

            try
            {
                var formFeature = Request.HttpContext.Features.Get <IFormFeature>();
                var form        = await formFeature.ReadFormAsync(CancellationToken.None);

                if (form.Files.Count != 1)
                {
                    return(BadRequest("Request body must contain single file section."));
                }

                var file = form.Files.First();
                uploadSession = await _sessionManagementService.InitiateMultipartUpload(
                    sessionId,
                    file.FileName,
                    file.ContentType,
                    file.Length,
                    templateCode);

                _logger.LogInformation("Multipart upload for file '{fileName}' in session '{sessionId}' was initiated.", file.FileName, sessionId);

                using (var inputStream = file.OpenReadStream())
                {
                    await _sessionManagementService.UploadFilePart(uploadSession, inputStream, templateCode);
                }

                var uploadedFileInfo = await _sessionManagementService.CompleteMultipartUpload(uploadSession, templateCode);

                return(Created(uploadedFileInfo.DownloadUri, new UploadedFileValue(uploadedFileInfo.Id)));
            }
            catch (ObjectNotFoundException)
            {
                return(NotFound());
            }
            catch (SessionExpiredException ex)
            {
                return(Gone(ex.ExpiredAt));
            }
            catch (InvalidTemplateException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (InvalidDataException ex)
            {
                return(RequestTooLarge(ex.Message));
            }
            catch (MissingFilenameException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (InvalidBinaryException ex)
            {
                return(Unprocessable(GenerateErrorJsonResult(ex)));
            }
            finally
            {
                if (uploadSession != null)
                {
                    await _sessionManagementService.AbortMultipartUpload(uploadSession);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task<IActionResult> UploadFile(
            Guid sessionId,
            int templateCode,
            [FromHeader(Name = Http.HeaderNames.AmsFileType)] string rawFileType,
            [FromHeader(Name = Http.HeaderNames.AmsImageSize)] string rawImageSize)
        {
            var multipartBoundary = Request.GetMultipartBoundary();
            if (string.IsNullOrEmpty(multipartBoundary))
            {
                return BadRequest($"Expected a multipart request, but got '{Request.ContentType}'.");
            }

            MultipartUploadSession uploadSession = null;
            try
            {
                var formFeature = Request.HttpContext.Features.Get<IFormFeature>();
                var form = await formFeature.ReadFormAsync(CancellationToken.None);

                if (form.Files.Count != 1)
                {
                    return BadRequest("Request body must contain single file section.");
                }

                var file = form.Files.First();

                if (!TryParseUploadedFileMetadata(file, rawFileType, rawImageSize, out var uploadedFileMetadata, out var error))
                {
                    return BadRequest(error);
                }

                uploadSession = await _sessionManagementService.InitiateMultipartUpload(sessionId, templateCode, uploadedFileMetadata);
                _logger.LogInformation("Multipart upload for file '{fileName}' in session '{sessionId}' was initiated.", file.FileName, sessionId);

                using (var inputStream = file.OpenReadStream())
                {
                    await _sessionManagementService.UploadFilePart(uploadSession, inputStream, templateCode);
                }

                var uploadedFileKey = await _sessionManagementService.CompleteMultipartUpload(uploadSession);

                return Created(_cdnOptions.AsRawUri(uploadedFileKey), new UploadedFileValue(uploadedFileKey));
            }
            catch (ObjectNotFoundException)
            {
                return NotFound();
            }
            catch (SessionExpiredException ex)
            {
                return Gone(ex.ExpiredAt);
            }
            catch (InvalidTemplateException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (InvalidDataException ex)
            {
                return RequestTooLarge(ex.Message);
            }
            catch (MissingFilenameException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (InvalidBinaryException ex)
            {
                return Unprocessable(GenerateErrorJsonResult(ex));
            }
            finally
            {
                if (uploadSession != null)
                {
                    await _sessionManagementService.AbortMultipartUpload(uploadSession);
                }
            }
        }