public static void CheckUploadedImageAndCreateThumbs(ImageUploadEditorAttribute attr, ref string temporaryFile) { ImageCheckResult[] supportedFormats = null; if (!attr.AllowNonImage) { supportedFormats = new ImageCheckResult[] { ImageCheckResult.JPEGImage, ImageCheckResult.GIFImage, ImageCheckResult.PNGImage } } ; UploadHelper.CheckFileNameSecurity(temporaryFile); var checker = new ImageChecker(); checker.MinWidth = attr.MinWidth; checker.MaxWidth = attr.MaxWidth; checker.MinHeight = attr.MinHeight; checker.MaxHeight = attr.MaxHeight; checker.MaxDataSize = attr.MaxSize; Image image = null; try { var temporaryPath = UploadHelper.DbFilePath(temporaryFile); using (var fs = new FileStream(temporaryPath, FileMode.Open)) { if (attr.MinSize != 0 && fs.Length < attr.MinSize) { throw new ValidationError(String.Format(Texts.Controls.ImageUpload.UploadFileTooSmall, UploadHelper.FileSizeDisplay(attr.MinSize))); } if (attr.MaxSize != 0 && fs.Length > attr.MaxSize) { throw new ValidationError(String.Format(Texts.Controls.ImageUpload.UploadFileTooBig, UploadHelper.FileSizeDisplay(attr.MaxSize))); } ImageCheckResult result; if (Path.GetExtension(temporaryFile).ToLowerInvariant() == ".swf") { result = ImageCheckResult.FlashMovie; // validate swf file somehow! } else { result = checker.CheckStream(fs, true, out image); } if (result == ImageCheckResult.InvalidImage && attr.AllowNonImage) { return; } if (result > ImageCheckResult.UnsupportedFormat || (supportedFormats != null && Array.IndexOf(supportedFormats, result) < 0)) { string error = checker.FormatErrorMessage(result); throw new ValidationError(error); } if (result >= ImageCheckResult.FlashMovie) { return; } string basePath = UploadHelper.TemporaryPath; string baseFile = Path.GetFileNameWithoutExtension(Path.GetFileName(temporaryPath)); TemporaryFileHelper.PurgeDirectoryDefault(basePath); if ((attr.ScaleWidth > 0 || attr.ScaleHeight > 0) && ((attr.ScaleWidth > 0 && (attr.ScaleSmaller || checker.Width > attr.ScaleWidth)) || (attr.ScaleHeight > 0 && (attr.ScaleSmaller || checker.Height > attr.ScaleHeight)))) { using (Image scaledImage = ThumbnailGenerator.Generate( image, attr.ScaleWidth, attr.ScaleHeight, attr.ScaleMode, Color.Empty)) { temporaryFile = baseFile + ".jpg"; fs.Close(); scaledImage.Save(Path.Combine(basePath, temporaryFile), System.Drawing.Imaging.ImageFormat.Jpeg); temporaryFile = "temporary/" + temporaryFile; } } var thumbSizes = attr.ThumbSizes.TrimToNull(); if (thumbSizes == null) { return; } foreach (var sizeStr in thumbSizes.Replace(";", ",").Split(new char[] { ',' })) { var dims = sizeStr.ToLowerInvariant().Split(new char[] { 'x' }); int w, h; if (dims.Length != 2 || !Int32.TryParse(dims[0], out w) || !Int32.TryParse(dims[1], out h) || w < 0 || h < 0 || (w == 0 && h == 0)) { throw new ArgumentOutOfRangeException("thumbSizes"); } using (Image thumbImage = ThumbnailGenerator.Generate(image, w, h, attr.ThumbMode, Color.Empty)) { string thumbFile = Path.Combine(basePath, baseFile + "_t" + w.ToInvariant() + "x" + h.ToInvariant() + ".jpg"); thumbImage.Save(thumbFile); } } } } finally { if (image != null) { image.Dispose(); } } } }
public bool ProcessStream(Stream fileContent, string extension) { extension = extension.TrimToEmpty().ToLowerInvariant(); if (IsDangerousExtension(extension)) { ErrorMessage = "Unsupported file extension!"; return(false); } CheckResult = ImageCheckResult.InvalidImage; ErrorMessage = null; ImageWidth = 0; ImageHeight = 0; IsImage = false; var success = false; var temporaryPath = UploadHelper.TemporaryPath; Directory.CreateDirectory(temporaryPath); TemporaryFileHelper.PurgeDirectoryDefault(temporaryPath); string baseFileName = System.IO.Path.Combine(temporaryPath, Guid.NewGuid().ToString("N")); try { try { if (IsImageExtension(extension)) { IsImage = true; success = true; FilePath = baseFileName + extension; fileContent.Seek(0, SeekOrigin.Begin); using (FileStream fs = new FileStream(FilePath, FileMode.Create)) { fileContent.CopyTo(fs); FileSize = fs.Length; } success = ProcessImageStream(fileContent, extension); } else { FilePath = baseFileName + extension; fileContent.Seek(0, SeekOrigin.Begin); using (FileStream fs = new FileStream(FilePath, FileMode.Create)) { fileContent.CopyTo(fs); FileSize = fs.Length; } success = true; } } catch (Exception ex) { ErrorMessage = ex.Message; success = false; return(success); } } finally { if (!success) { if (!ThumbFile.IsNullOrEmpty()) { TemporaryFileHelper.TryDelete(ThumbFile); } if (!FilePath.IsNullOrEmpty()) { TemporaryFileHelper.TryDelete(FilePath); } } fileContent.Dispose(); } return(success); }
public override void PurgeTemporaryFiles() { TemporaryFileHelper.PurgeDirectoryDefault(RootPath); }
private bool ProcessImageStream(Stream fileContent, string extension) { Image image = null; var imageChecker = new ImageChecker(); CheckResult = imageChecker.CheckStream(fileContent, true, out image); try { FileSize = imageChecker.DataSize; ImageWidth = imageChecker.Width; ImageHeight = imageChecker.Height; if (CheckResult != ImageCheckResult.JPEGImage && CheckResult != ImageCheckResult.GIFImage && CheckResult != ImageCheckResult.PNGImage) { ErrorMessage = imageChecker.FormatErrorMessage(CheckResult); return(false); } else { IsImage = true; extension = (CheckResult == ImageCheckResult.PNGImage ? ".png" : (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg")); var temporaryPath = UploadHelper.TemporaryPath; Directory.CreateDirectory(temporaryPath); TemporaryFileHelper.PurgeDirectoryDefault(temporaryPath); string baseFileName = System.IO.Path.Combine(temporaryPath, Guid.NewGuid().ToString("N")); FilePath = baseFileName + extension; fileContent.Seek(0, SeekOrigin.Begin); using (FileStream fs = new FileStream(FilePath, FileMode.Create)) fileContent.CopyTo(fs); if (ThumbWidth > 0 || ThumbHeight > 0) { using (System.Drawing.Image thumbImage = ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor)) { ThumbFile = baseFileName + "_t.jpg"; if (ThumbQuality != 0) { var p = new System.Drawing.Imaging.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(ThumbFile, jpegCodec, p); } else { thumbImage.Save(ThumbFile, System.Drawing.Imaging.ImageFormat.Jpeg); } ThumbHeight = thumbImage.Width; ThumbWidth = thumbImage.Height; } } return(true); } } finally { if (image != null) { image.Dispose(); image = null; } } }
public void CheckUploadedImageAndCreateThumbs(ref string temporaryFile, params ImageCheckResult[] supportedFormats) { if (supportedFormats == null || supportedFormats.Length == 0) { supportedFormats = new ImageCheckResult[] { ImageCheckResult.JPEGImage, ImageCheckResult.GIFImage, ImageCheckResult.PNGImage } } ; UploadHelper.CheckFileNameSecurity(temporaryFile); var checker = new ImageChecker(); checker.MinWidth = this.MinWidth; checker.MaxWidth = this.MaxWidth; checker.MinHeight = this.MinHeight; checker.MaxHeight = this.MaxHeight; checker.MaxDataSize = this.MaxBytes; Image image = null; try { var temporaryPath = Path.Combine(UploadHelper.TemporaryPath, temporaryFile); using (var fs = new FileStream(temporaryPath, FileMode.Open)) { if (this.MinBytes != 0 && fs.Length < this.MinBytes) { throw new ValidationError(String.Format("Yükleyeceğiniz dosya en az {0} boyutunda olmalı!", UploadHelper.FileSizeDisplay(this.MinBytes))); } if (this.MaxBytes != 0 && fs.Length > this.MaxBytes) { throw new ValidationError(String.Format("Yükleyeceğiniz dosya en çok {0} boyutunda olabilir!", UploadHelper.FileSizeDisplay(this.MaxBytes))); } ImageCheckResult result; if (Path.GetExtension(temporaryFile).ToLowerInvariant() == ".swf") { result = ImageCheckResult.FlashMovie; // validate swf file somehow! } else { result = checker.CheckStream(fs, true, out image); } if (result > ImageCheckResult.FlashMovie || Array.IndexOf(supportedFormats, result) < 0) { string error = checker.FormatErrorMessage(result); throw new ValidationError(error); } if (result != ImageCheckResult.FlashMovie) { string basePath = UploadHelper.TemporaryPath; string baseFile = Path.GetFileNameWithoutExtension(temporaryFile); TemporaryFileHelper.PurgeDirectoryDefault(basePath); if ((this.ScaleWidth > 0 || this.ScaleHeight > 0) && ((this.ScaleWidth > 0 && (this.ScaleSmaller || checker.Width > this.ScaleWidth)) || (this.ScaleHeight > 0 && (this.ScaleSmaller || checker.Height > this.ScaleHeight)))) { using (Image scaledImage = ThumbnailGenerator.Generate( image, this.ScaleWidth, this.ScaleHeight, this.ScaleMode, Color.Empty)) { temporaryFile = baseFile + ".jpg"; fs.Close(); scaledImage.Save(Path.Combine(basePath, temporaryFile), System.Drawing.Imaging.ImageFormat.Jpeg); } } var thumbSizes = this.ThumbSizes.TrimToNull(); if (thumbSizes != null) { foreach (var sizeStr in thumbSizes.Replace(";", ",").Split(new char[] { ',' })) { var dims = sizeStr.ToLowerInvariant().Split(new char[] { 'x' }); int w, h; if (dims.Length != 2 || !Int32.TryParse(dims[0], out w) || !Int32.TryParse(dims[1], out h) || w < 0 || h < 0 || (w == 0 && h == 0)) { throw new ArgumentOutOfRangeException("thumbSizes"); } using (Image thumbImage = ThumbnailGenerator.Generate(image, w, h, this.ThumbMode, Color.Empty)) { string thumbFile = Path.Combine(basePath, baseFile + "t_" + w.ToInvariant() + "x" + h.ToInvariant() + ".jpg"); if (this.ThumbQuality != 0) { var p = new System.Drawing.Imaging.EncoderParameters(1); p.Param[0] = new EncoderParameter(Encoder.Quality, 80L); 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(thumbFile, jpegCodec, p); } else { thumbImage.Save(thumbFile, System.Drawing.Imaging.ImageFormat.Jpeg); } } } } } } } finally { if (image != null) { image.Dispose(); } } } }