public async Task <IActionResult> PersonalDataFormAction(PersonalDataModel model) { int size = 200; ImageCheckResult imgCheck = _moneyImageParser.CheckImage(model.Photo, size * 1000); if (imgCheck == ImageCheckResult.MaxSizeError) { ViewData["ImageError"] = $"Photo wasn't changed. Maximum image size is {size}kb."; } if (imgCheck == ImageCheckResult.IsNotJpeg) { ViewData["ImageError"] = "Photo wasn't changed. Only jpeg image format supported."; } if (imgCheck == ImageCheckResult.Success) { await _moneyImageParser.SaveUserImage(model.Photo, User.Identity.Name); } UserInfo uInfo = new UserInfo { Address = model.Address, BirthYear = model.BirthYear, BirthDay = model.BirthDay, BirthMonth = model.BirthMonth, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Phone = model.Phone, Gender = model.Gender, Login = model.Login }; await _userInfoManager.SetUserInfoAsync(uInfo, User.Identity.Name); await _logManager.WriteAsync(uInfo.Login, $"User '{uInfo.Login}' edited his personal data."); if (uInfo.Login != User.Identity.Name) { string name = uInfo.Login; await _logManager.WriteAsync(name, $"User '{User.Identity.Name}' renamed himself into '{name}'."); await _authentication.SignOutAsync(); await _logManager.WriteAsync(name, $"User '{name}' signed out."); return(RedirectToAction(nameof(AccountController.Login), "Account")); } model.BirthMonthStr = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(model.BirthMonth); return(View(nameof(MainController.PersonalData), model)); }
/// <summary> /// Formats an error message for a ImageCheckResult code returned from a CheckStream operation. /// This should be called right after CheckStream, cause image checkers current constraint /// and results will be used to format the message.</summary> /// <param name="error"> /// Checking result.</param> /// <returns> /// An error message formatted according to constraints and validation result.</returns> public string FormatErrorMessage(ImageCheckResult error) { return (String.Format( ImageChecker.CheckErrorMessages[(int)error], DataSize, // 0 Width, // 1 Height, // 2 MaxDataSize, // 3 MinWidth, // 4 MinHeight, // 5 MaxWidth, // 6 MaxHeight)); // 7 }
public string FormatErrorMessage(ImageCheckResult result) { var format = LocalText.Get("Enums.ImageCheckResult." + Enum.GetName(typeof(ImageCheckResult), result)); switch (result) { case ImageCheckResult.GIFImage: case ImageCheckResult.PNGImage: case ImageCheckResult.JPEGImage: case ImageCheckResult.FlashMovie: case ImageCheckResult.InvalidImage: case ImageCheckResult.ImageIsEmpty: return(format); case ImageCheckResult.DataSizeTooHigh: return(String.Format(format, MaxDataSize, DataSize)); case ImageCheckResult.SizeMismatch: return(String.Format(format, MinWidth, MinHeight, Width, Height)); case ImageCheckResult.WidthMismatch: case ImageCheckResult.WidthTooHigh: return(String.Format(format, MaxWidth, Width)); case ImageCheckResult.WidthTooLow: return(String.Format(format, MinWidth, Width)); case ImageCheckResult.HeightMismatch: case ImageCheckResult.HeightTooHigh: return(String.Format(format, MaxHeight, MaxHeight)); case ImageCheckResult.HeightTooLow: return(String.Format(format, MinHeight, MaxHeight)); default: throw new ArgumentOutOfRangeException("ImageCheckResult"); } }
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 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 = "temporaryFile/" + 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 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(); } } } }
public string FormatErrorMessage(ImageCheckResult result) { var format = LocalText.Get("Enums.ImageCheckResult." + Enum.GetName(typeof(ImageCheckResult), result)); switch (result) { case ImageCheckResult.GIFImage: case ImageCheckResult.PNGImage: case ImageCheckResult.JPEGImage: case ImageCheckResult.FlashMovie: case ImageCheckResult.InvalidImage: case ImageCheckResult.ImageIsEmpty: return format; case ImageCheckResult.DataSizeTooHigh: return String.Format(format, MaxDataSize, DataSize); case ImageCheckResult.SizeMismatch: return String.Format(format, MinWidth, MinHeight, Width, Height); case ImageCheckResult.WidthMismatch: case ImageCheckResult.WidthTooHigh: return String.Format(format, MaxWidth, Width); case ImageCheckResult.WidthTooLow: return String.Format(format, MinWidth, Width); case ImageCheckResult.HeightMismatch: case ImageCheckResult.HeightTooHigh: return String.Format(format, MaxHeight, MaxHeight); case ImageCheckResult.HeightTooLow: return String.Format(format, MinHeight, MaxHeight); default: throw new ArgumentOutOfRangeException("ImageCheckResult"); } }