/// <summary> /// Initiate upload session. /// </summary> /// <param name="sessionId">Session identifier.</param> /// <param name="templateCode">Element's template code.</param> /// <param name="uploadedFileMetadata">File metadata.</param> /// <exception cref="MissingFilenameException">Filename is not specified.</exception> /// <exception cref="InvalidTemplateException">Binary content is not expected for specified template code.</exception> /// <exception cref="ObjectNotFoundException">Session or template has not been found.</exception> /// <exception cref="SessionExpiredException">Specified session is expired.</exception> /// <exception cref="S3Exception">Error making request to S3.</exception> /// <exception cref="InvalidBinaryException">Binary does not meet template's constraints.</exception> /// <returns>Initiated upload session instance.</returns> public async Task <MultipartUploadSession> InitiateMultipartUpload( Guid sessionId, int templateCode, IUploadedFileMetadata uploadedFileMetadata) { var(sessionDescriptor, _, expiresAt) = await _sessionStorageReader.GetSessionDescriptor(sessionId); return(await InitiateMultipartUploadInternal(sessionId, templateCode, uploadedFileMetadata, sessionDescriptor, expiresAt)); }
private static void EnsureFileHeaderIsValid( int templateCode, ElementDescriptorType elementDescriptorType, IElementConstraints elementConstraints, Stream inputStream, IUploadedFileMetadata uploadedFileMetadata) { var fileFormat = DetectFileFormat(uploadedFileMetadata.FileName); switch (elementDescriptorType) { case ElementDescriptorType.BitmapImage: BitmapImageValidator.ValidateBitmapImageHeader(templateCode, (BitmapImageElementConstraints)elementConstraints, fileFormat, inputStream); break; case ElementDescriptorType.CompositeBitmapImage: if (uploadedFileMetadata.FileType == FileType.SizeSpecificBitmapImage) { var imageMetadata = (UploadedImageMetadata)uploadedFileMetadata; BitmapImageValidator.ValidateSizeSpecificBitmapImageHeader( templateCode, (CompositeBitmapImageElementConstraints)elementConstraints, fileFormat, inputStream, imageMetadata.Size); } else { BitmapImageValidator.ValidateCompositeBitmapImageOriginalHeader( templateCode, (CompositeBitmapImageElementConstraints)elementConstraints, fileFormat, inputStream); } break; case ElementDescriptorType.VectorImage: VectorImageValidator.ValidateVectorImageHeader(templateCode, fileFormat, inputStream); break; case ElementDescriptorType.Article: break; case ElementDescriptorType.PlainText: case ElementDescriptorType.FormattedText: case ElementDescriptorType.FasComment: case ElementDescriptorType.Link: case ElementDescriptorType.Phone: case ElementDescriptorType.VideoLink: case ElementDescriptorType.Color: throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}"); default: throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type"); } }
public async Task <MultipartUploadSession> InitiateMultipartUpload( Guid sessionId, int templateCode, IUploadedFileMetadata uploadedFileMetadata) { if (string.IsNullOrEmpty(uploadedFileMetadata.FileName)) { throw new MissingFilenameException($"Filename has not been provided for the item '{templateCode}'"); } var(sessionDescriptor, _, expiresAt) = await _sessionStorageReader.GetSessionDescriptor(sessionId); if (sessionDescriptor.BinaryElementTemplateCodes.All(x => x != templateCode)) { throw new InvalidTemplateException( $"Binary content is not expected for the item '{templateCode}' within template '{sessionDescriptor.TemplateId}' " + $"with version Id '{sessionDescriptor.TemplateVersionId}'."); } var elementDescriptor = await GetElementDescriptor(sessionDescriptor.TemplateId, sessionDescriptor.TemplateVersionId, templateCode); EnsureFileMetadataIsValid(elementDescriptor, sessionDescriptor.Language, uploadedFileMetadata); var fileKey = Guid.NewGuid().ToString(); var key = sessionId.AsS3ObjectKey(fileKey); var request = new InitiateMultipartUploadRequest { BucketName = _filesBucketName, Key = key, ContentType = uploadedFileMetadata.ContentType }; var metadataWrapper = MetadataCollectionWrapper.For(request.Metadata); metadataWrapper.Write(MetadataElement.Filename, uploadedFileMetadata.FileName); var uploadResponse = await _cephS3Client.InitiateMultipartUploadAsync(request); return(new MultipartUploadSession( sessionId, sessionDescriptor, expiresAt, elementDescriptor, uploadedFileMetadata, fileKey, uploadResponse.UploadId)); }
public MultipartUploadSession( Guid sessionId, SessionDescriptor sessionDescriptor, DateTime expiresAt, IElementDescriptor elementDescriptor, IUploadedFileMetadata uploadedFileMetadata, string fileKey, string uploadId) { SessionId = sessionId; SessionDescriptor = sessionDescriptor; ElementDescriptor = elementDescriptor; UploadedFileMetadata = uploadedFileMetadata; FileKey = fileKey; UploadId = uploadId; SessionExpiresAt = expiresAt; }
private static void EnsureFileContentIsValid( int templateCode, ElementDescriptorType elementDescriptorType, IElementConstraints elementConstraints, Stream inputStream, IUploadedFileMetadata uploadedFileMetadata) { switch (elementDescriptorType) { case ElementDescriptorType.BitmapImage: BitmapImageValidator.ValidateBitmapImage(templateCode, (BitmapImageElementConstraints)elementConstraints, inputStream); break; case ElementDescriptorType.VectorImage: var fileFormat = DetectFileFormat(uploadedFileMetadata.FileName); VectorImageValidator.ValidateVectorImage(templateCode, fileFormat, (VectorImageElementConstraints)elementConstraints, inputStream); break; case ElementDescriptorType.Article: ArticleValidator.ValidateArticle(templateCode, inputStream); break; case ElementDescriptorType.CompositeBitmapImage: break; case ElementDescriptorType.ScalableBitmapImage: break; case ElementDescriptorType.PlainText: case ElementDescriptorType.FormattedText: case ElementDescriptorType.FasComment: case ElementDescriptorType.Link: case ElementDescriptorType.Phone: case ElementDescriptorType.VideoLink: case ElementDescriptorType.Color: throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}"); default: throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type"); } }
private static bool TryParseUploadedFileMetadata( IFormFile file, string rawFileType, string rawImageSize, out IUploadedFileMetadata uploadedFileMetadata, out string error) { uploadedFileMetadata = null; error = null; if (string.IsNullOrEmpty(rawFileType)) { uploadedFileMetadata = new GenericUploadedFileMetadata(file.FileName, file.ContentType, file.Length); return(true); } if (!Enum.TryParse <FileType>(rawFileType, true, out var fileType)) { error = $"Cannot parse '{Http.HeaderNames.AmsFileType}' header value '{fileType}'"; return(false); } switch (fileType) { case FileType.SizeSpecificBitmapImage: if (!ImageSize.TryParse(rawImageSize, out var imageSize)) { error = $"Cannot parse '{Http.HeaderNames.AmsImageSize}' header value '{rawImageSize}'"; return(false); } uploadedFileMetadata = new UploadedImageMetadata(file.FileName, file.ContentType, file.Length, imageSize); return(true); default: error = $"Unexpected '{Http.HeaderNames.AmsFileType}' header value '{fileType}'"; return(false); } }
private static void EnsureFileMetadataIsValid(IElementDescriptor elementDescriptor, Language language, IUploadedFileMetadata uploadedFileMetadata) { var constraints = (IBinaryElementConstraints)elementDescriptor.Constraints.For(language); if (constraints.MaxFilenameLength < uploadedFileMetadata.FileName.Length) { throw new InvalidBinaryException(elementDescriptor.TemplateCode, new FilenameTooLongError(uploadedFileMetadata.FileName.Length)); } if (constraints.MaxSize < uploadedFileMetadata.FileLength) { throw new InvalidBinaryException(elementDescriptor.TemplateCode, new BinaryTooLargeError(uploadedFileMetadata.FileLength)); } if (constraints is CompositeBitmapImageElementConstraints compositeBitmapImageElementConstraints && uploadedFileMetadata.FileType == FileType.SizeSpecificBitmapImage && compositeBitmapImageElementConstraints.SizeSpecificImageMaxSize < uploadedFileMetadata.FileLength) { throw new InvalidBinaryException(elementDescriptor.TemplateCode, new SizeSpecificImageTooLargeError(uploadedFileMetadata.FileLength)); } var extension = GetDotLessExtension(uploadedFileMetadata.FileName); if (!ValidateFileExtension(extension, constraints)) { throw new InvalidBinaryException(elementDescriptor.TemplateCode, new BinaryInvalidFormatError(extension)); } }