Ejemplo n.º 1
0
        public static CopyTemporaryFileResult CopyTemporaryFile(this IUploadStorage uploadStorage,
                                                                CopyTemporaryFileOptions options)
        {
            if (uploadStorage is null)
            {
                throw new ArgumentNullException(nameof(uploadStorage));
            }

            long   size = uploadStorage.GetFileSize(options.TemporaryFile);
            string path = PathHelper.ToUrl(UploadFormatting.FormatFilename(options));

            path = uploadStorage.CopyFrom(uploadStorage, options.TemporaryFile, path, autoRename: true);
            bool hasThumbnail = uploadStorage.FileExists(UploadPathHelper.GetThumbnailName(options.TemporaryFile));

            var result = new CopyTemporaryFileResult()
            {
                FileSize     = size,
                Path         = path,
                OriginalName = options.OriginalName,
                HasThumbnail = hasThumbnail
            };

            options.FilesToDelete?.RegisterNewFile(path);
            options.FilesToDelete?.RegisterOldFile(options.TemporaryFile);
            return(result);
        }
Ejemplo n.º 2
0
        public static string GetThumbnailUrl(this IUploadStorage uploadStorage, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            var thumb = UploadPathHelper.GetThumbnailName(path);

            return(uploadStorage.GetFileUrl(thumb));
        }
Ejemplo n.º 3
0
        private bool ProcessImageStream(Stream fileContent, ITextLocalizer localizer)
        {
            var imageChecker = new ImageChecker();

            CheckResult = imageChecker.CheckStream(fileContent, true, out Image image, Logger);
            try
            {
                FileSize    = imageChecker.DataSize;
                ImageWidth  = imageChecker.Width;
                ImageHeight = imageChecker.Height;

                if (CheckResult != ImageCheckResult.JPEGImage &&
                    CheckResult != ImageCheckResult.GIFImage &&
                    CheckResult != ImageCheckResult.PNGImage)
                {
                    ErrorMessage = imageChecker.FormatErrorMessage(CheckResult, localizer);
                    return(false);
                }
                else
                {
                    IsImage = true;

                    var extension = CheckResult == ImageCheckResult.PNGImage ? ".png" :
                                    (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg");

                    storage.PurgeTemporaryFiles();

                    if (ThumbWidth > 0 || ThumbHeight > 0)
                    {
                        using Image thumbImage =
                                  ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor);
                        var thumbFile = UploadPathHelper.GetThumbnailName(TemporaryFile);

                        using (var ms = new MemoryStream())
                        {
                            if (ThumbQuality != 0)
                            {
                                var p = new EncoderParameters(1);
                                p.Param[0] = new EncoderParameter(Encoder.Quality, ThumbQuality);

                                ImageCodecInfo   jpegCodec = null;
                                ImageCodecInfo[] codecs    = ImageCodecInfo.GetImageEncoders();
                                // Find the correct image codec
                                for (int i = 0; i < codecs.Length; i++)
                                {
                                    if (codecs[i].MimeType == "image/jpeg")
                                    {
                                        jpegCodec = codecs[i];
                                    }
                                }

                                thumbImage.Save(ms, jpegCodec, p);
                            }
                            else
                            {
                                thumbImage.Save(ms, ImageFormat.Jpeg);
                            }
                            ms.Seek(0, SeekOrigin.Begin);
                            ThumbFile = storage.WriteFile(thumbFile, ms, autoRename: false);
                        }
                        ThumbHeight = thumbImage.Width;
                        ThumbWidth  = thumbImage.Height;
                    }

                    return(true);
                }
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        private bool ProcessImageStream(Stream fileContent, ITextLocalizer localizer)
        {
            var imageChecker = new ImageChecker();

            CheckResult = imageChecker.CheckStream(fileContent, true, out Image image, Logger);
            try
            {
                FileSize    = imageChecker.DataSize;
                ImageWidth  = imageChecker.Width;
                ImageHeight = imageChecker.Height;

                if (CheckResult != ImageCheckResult.JPEGImage &&
                    CheckResult != ImageCheckResult.GIFImage &&
                    CheckResult != ImageCheckResult.PNGImage)
                {
                    ErrorMessage = imageChecker.FormatErrorMessage(CheckResult, localizer);
                    return(false);
                }
                else
                {
                    IsImage = true;

                    var extension = CheckResult == ImageCheckResult.PNGImage ? ".png" :
                                    (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg");

                    storage.PurgeTemporaryFiles();

                    if (ThumbWidth > 0 || ThumbHeight > 0)
                    {
                        ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor,
                                                    inplace: true);
                        var thumbFile = UploadPathHelper.GetThumbnailName(TemporaryFile);

                        using (var ms = new MemoryStream())
                        {
                            if (ThumbQuality != 0)
                            {
                                image.Save(ms, new JpegEncoder {
                                    Quality = ThumbQuality
                                });
                            }
                            else
                            {
                                image.Save(ms, new JpegEncoder());
                            }
                            ms.Seek(0, SeekOrigin.Begin);
                            ThumbFile = storage.WriteFile(thumbFile, ms, autoRename: false);
                        }
                        ThumbHeight = image.Width;
                        ThumbWidth  = image.Height;
                    }

                    return(true);
                }
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }