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(); } }
public async Task SaveFile(IFileSource uploadedFile, DocumentAsset documentAsset) { documentAsset.ContentType = _mimeTypeService.MapFromFileName(uploadedFile.FileName, uploadedFile.MimeType); documentAsset.FileExtension = Path.GetExtension(uploadedFile.FileName).TrimStart('.'); documentAsset.FileNameOnDisk = "file-not-saved"; _assetFileTypeValidator.ValidateAndThrow(documentAsset.FileExtension, documentAsset.ContentType, "File"); var fileStamp = AssetFileStampHelper.ToFileStamp(documentAsset.FileUpdateDate); using (var inputSteam = await uploadedFile.OpenReadStreamAsync()) { bool isNew = documentAsset.DocumentAssetId < 1; documentAsset.FileSizeInBytes = inputSteam.Length; using (var scope = _transactionScopeFactory.Create(_dbContext)) { // Save at this point if it's a new file if (isNew) { await _dbContext.SaveChangesAsync(); } // update the filename documentAsset.FileNameOnDisk = $"{documentAsset.DocumentAssetId}-{fileStamp}"; var fileName = Path.ChangeExtension(documentAsset.FileNameOnDisk, documentAsset.FileExtension); // Save the raw file directly await CreateFileAsync(isNew, fileName, inputSteam); // Update the filename await _dbContext.SaveChangesAsync(); await scope.CompleteAsync(); } } }
public async Task SaveAsync( IFileSource uploadedFile, ImageAsset imageAsset, string propertyName ) { Image 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 new PropertyValidationException("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(); }; } } }