public async Task SaveAsync(IFileSource fileToSave, ImageAsset imageAsset, string validationErrorPropertyName) { var fileExtension = Path.GetExtension(fileToSave.FileName); if (WidthInPixels.HasValue) { imageAsset.WidthInPixels = WidthInPixels.Value; } if (HeightInPixels.HasValue) { imageAsset.HeightInPixels = HeightInPixels.Value; } imageAsset.FileExtension = fileExtension; using var inputSteam = await fileToSave.OpenReadStreamAsync(); imageAsset.FileSizeInBytes = inputSteam.Length; var fileName = Path.ChangeExtension(imageAsset.FileNameOnDisk, imageAsset.FileExtension); using (var scope = _transactionScopeManager.Create(_dbContext)) { if (SaveFile) { await _fileStoreService.CreateAsync(ASSET_FILE_CONTAINER_NAME, fileName, inputSteam); } await _dbContext.SaveChangesAsync(); await scope.CompleteAsync(); } }
private Task CreateFileAsync(bool isNew, string fileName, Stream outputStream) { if (isNew) { return(_fileStoreService.CreateAsync(DocumentAssetConstants.FileContainerName, fileName, outputStream)); } else { return(_fileStoreService.CreateOrReplaceAsync(DocumentAssetConstants.FileContainerName, fileName, outputStream)); } }
public async Task SaveAsync( IUploadedFile uploadedFile, ImageAsset imageAsset, string propertyName ) { Image <Rgba32> imageFile = null; IImageFormat imageFormat = null; using (var inputSteam = await uploadedFile.OpenReadStreamAsync()) { try { imageFile = Image.Load(inputSteam, out imageFormat); } catch (ArgumentException ex) { // We'll get an argument exception if the image file is invalid // so lets check to see if we can identify if it is an invalid file type and show that error // This might not always be the case since a file extension or mime type might not be supplied. var ext = Path.GetExtension(uploadedFile.FileName); if ((!string.IsNullOrEmpty(ext) && !ImageAssetConstants.PermittedImageTypes.ContainsKey(ext)) || (!string.IsNullOrEmpty(uploadedFile.MimeType) && !ImageAssetConstants.PermittedImageTypes.ContainsValue(uploadedFile.MimeType))) { throw ValidationErrorException.CreateWithProperties("The file is not a supported image type.", propertyName); } throw; } using (imageFile) // validate image file { ValidateImage(propertyName, imageFile, imageFormat); var requiredReEncoding = true; var fileExtension = "jpg"; var foundExtension = _permittedImageFileExtensions .FirstOrDefault(e => imageFormat.FileExtensions.Contains(e)); if (foundExtension != null) { fileExtension = foundExtension; requiredReEncoding = false; } imageAsset.WidthInPixels = imageFile.Width; imageAsset.HeightInPixels = imageFile.Height; imageAsset.FileExtension = fileExtension; imageAsset.FileSizeInBytes = inputSteam.Length; using (var scope = _transactionScopeManager.Create(_dbContext)) { var fileName = Path.ChangeExtension(imageAsset.FileNameOnDisk, imageAsset.FileExtension); if (requiredReEncoding) { // Convert the image to jpg using (var outputStream = new MemoryStream()) { if (requiredReEncoding) { imageFile.Save(outputStream, new JpegEncoder()); } else { imageFile.Save(outputStream, imageFormat); } await _fileStoreService.CreateAsync(ASSET_FILE_CONTAINER_NAME, fileName, outputStream); // recalculate size and save imageAsset.FileSizeInBytes = outputStream.Length; } } else { // Save the raw file directly await _fileStoreService.CreateAsync(ASSET_FILE_CONTAINER_NAME, fileName, inputSteam); } await _dbContext.SaveChangesAsync(); await scope.CompleteAsync(); }; } } }