Esempio n. 1
0
        public async Task SaveFile(IUploadedFile uploadedFile, DocumentAsset documentAsset)
        {
            using (var inputSteam = await uploadedFile.GetFileStreamAsync())
            {
                bool isNew = documentAsset.DocumentAssetId < 1;

                documentAsset.FileExtension   = Path.GetExtension(uploadedFile.FileName).TrimStart('.');
                documentAsset.FileSizeInBytes = Convert.ToInt32(inputSteam.Length);
                documentAsset.ContentType     = uploadedFile.MimeType;

                // Save at this point if it's a new image
                if (isNew)
                {
                    await _dbContext.SaveChangesAsync();
                }

                var fileName = Path.ChangeExtension(documentAsset.DocumentAssetId.ToString(), documentAsset.FileExtension);

                using (var scope = _transactionScopeFactory.Create())
                {
                    // Save the raw file directly
                    CreateFile(isNew, fileName, inputSteam);

                    if (!isNew)
                    {
                        await _dbContext.SaveChangesAsync();
                    }
                    scope.Complete();
                }
            }
        }
Esempio n. 2
0
        public async Task SaveFile(IUploadedFile uploadedFile, ImageAsset imageAsset)
        {
            Image imageFile = null;

            using (var inputSteam = await uploadedFile.GetFileStreamAsync())
            {
                try
                {
                    imageFile = Image.FromStream(inputSteam);
                }
                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.", "File");
                    }

                    throw;
                }

                using (imageFile) // validate image file
                {
                    bool requiredReEncoding = !_permittedImageFileExtensionMap.ContainsKey(imageFile.RawFormat);
                    bool isNew = imageAsset.ImageAssetId < 1;

                    var imageFormat = requiredReEncoding ? ImageFormat.Jpeg : imageFile.RawFormat;

                    imageAsset.Width     = imageFile.Width;
                    imageAsset.Height    = imageFile.Height;
                    imageAsset.Extension = _permittedImageFileExtensionMap[imageFormat].First().TrimStart('.');
                    imageAsset.FileSize  = Convert.ToInt32(inputSteam.Length);

                    // Save at this point if it's a new image
                    if (isNew)
                    {
                        await _dbContext.SaveChangesAsync();
                    }

                    using (var scope = _transactionScopeFactory.Create())
                    {
                        var fileName = Path.ChangeExtension(imageAsset.ImageAssetId.ToString(), imageAsset.Extension);

                        if (requiredReEncoding)
                        {
                            // Convert the image to jpg
                            using (var outputStream = new MemoryStream())
                            {
                                imageFile.Save(outputStream, imageFormat);
                                CreateFile(isNew, fileName, outputStream);
                                // recalculate size and save
                                imageAsset.FileSize = Convert.ToInt32(outputStream.Length);
                                await _dbContext.SaveChangesAsync();
                            }
                        }
                        else
                        {
                            // Save the raw file directly
                            CreateFile(isNew, fileName, inputSteam);
                        }

                        scope.Complete();
                    };
                }
            }
        }