private ImageItemValidator ProcessFile(FileUpload file) { var imageMediaFormats = Ioc.GetInstance<IImageMediaFormats>(); var photoRepo = Ioc.GetInstance<IPhotoRepository>(); log.WriteLine("Submitted a picture with name " + file.FileName); var fileExt = Path.GetExtension(file.FileName.ToLower()); var userID = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()); var userName = Membership.GetUser(User.Identity.Name).UserName; var ticks = DateTime.Now.Ticks; var thumbImageId = Guid.NewGuid(); var fullImageId = Guid.NewGuid(); if (string.IsNullOrEmpty(fileExt) || !imageMediaFormats.HasExtension(fileExt)) { return new ImageItemValidator(false, file.FileName + " does not have a valid extension. "); } var newFileName = userName + "-" + ticks + fileExt; log.WriteLine("thumb image New file name: " + newFileName); if (file.FileBytes.Length > 0) { int intContentLength; if (!int.TryParse(file.FileBytes.Length.ToString(), out intContentLength)) { return new ImageItemValidator(false, file.FileName + " is too large. "); } IPhoto fullImage = null; Guid? showId = new Guid(hdnShowId.Value); var notes = Request.Form["txtNotes"].ToString(); log.WriteLine("About to create full image"); fullImage = new Photo { PhotoId = fullImageId, CreatedDate = DateTime.Now, UserId = userID, FileName = newFileName, ContentType = fileExt, Type = short.Parse(hdnPhotoType.Value), NickName = txtNickName.Text.Trim(), Notes = notes, Quality = short.Parse(ddlQuality.SelectedValue), Thumbnail = false, ShowId = showId }; string destDir = Path.Combine(Request.PhysicalApplicationPath, "images\\Shows"); string destPath = Path.Combine(destDir, newFileName); log.WriteLine("Saving image to the path = " + destPath); file.SaveAs(destPath); //file.MoveTo(destPath, MoveToOptions.Overwrite); log.WriteLine("Image saved to the path and now determining what type of photo it is"); DetermineTypeOfPhoto(fullImage, showId); //imgDisplayFull.ImageUrl = LinkBuilder.GetImageLinkByFileName(newFileName); return new ImageItemValidator(true, "You have successfully saved " + file.FileName); } return new ImageItemValidator(false, "There was an error processing " + file.FileName + ". "); }
public void Brih(Guid showId) { IImageMediaFormats imageMediaFormats = Ioc.GetInstance<IImageMediaFormats>(); IPhotoRepository photoRepo = Ioc.GetInstance<IPhotoRepository>(); ImageResizerService imageResizerService = new ImageResizerService(); PhotoService photoService = new PhotoService(photoRepo); bool compiledSuccess = true; var posted = fuPicture.PostedFile; if (posted == null) { return; //ERROR MESSAGE //return new FileUploadJsonResult { Data = new { Src = "", ErrorMessage = "File was not uploaded." } }; } var fileExt = Path.GetExtension(posted.FileName.ToLower()); if (string.IsNullOrEmpty(fileExt)) { //ERROR MESSAGE //return new FileUploadJsonResult { Data = new { Src = "", ErrorMessage = "File was not uploaded." } }; } else { //check type if (imageMediaFormats.HasExtension(fileExt)) { var mediaFormat = imageMediaFormats.GetSpecByExtension(fileExt); Guid userID = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()); string userName = Membership.GetUser(User.Identity.Name).UserName; var ticks = DateTime.Now.Ticks; Guid thumbImageId = Guid.NewGuid(); Guid fullImageId = Guid.NewGuid(); //save file var newFileName = posted.FileName; if (posted.ContentLength > 0) { //try to resize the image var tmpResizeBuffer = new byte[posted.ContentLength]; posted.InputStream.Read(tmpResizeBuffer, 0, posted.ContentLength); var thumbResizedBuffer = imageResizerService.ResizeImage(tmpResizeBuffer, new ThumbnailSize(), mediaFormat); var fullResizedBuffer = imageResizerService.ResizeImage(tmpResizeBuffer, new FullImageSize(), mediaFormat); IPhoto thumbImage = null; IPhoto fullImage = null; //TODO: save fullImage using (var unitOfWork = TheCore.Infrastructure.UnitOfWork.Begin()) { /* * thumbnail */ if (thumbImage == null) { thumbImage = new Photo { PhotoId = thumbImageId, CreatedDate = DateTime.Now, UserId = userID, FileName = "thumb" + newFileName, ContentType = mediaFormat.ContentType, ContentLength = thumbResizedBuffer.Length, //Image = new byte[thumbResizedBuffer.Length], Type = (short)PhotoType.TicketStub, Thumbnail = true, ShowId = showId }; } else { //update entity thumbImage.ContentType = mediaFormat.ContentType; thumbImage.ContentLength = thumbResizedBuffer.Length; } bool success = false; photoService.Save(thumbImage, mediaFormat, out success); compiledSuccess = compiledSuccess && success; /* * fullsize */ if (fullImage == null) { //newFileName = userName + "-" + DateTime.Now.Ticks + fileExt; fullImage = new Photo { PhotoId = fullImageId, CreatedDate = DateTime.Now, UserId = userID, FileName = newFileName, ContentType = mediaFormat.ContentType, ContentLength = fullResizedBuffer.Length, //Image = new byte[fullResizedBuffer.Length], Type = (short)PhotoType.TicketStub, Thumbnail = false, ShowId = showId }; } else { //update entity fullImage.ContentType = mediaFormat.ContentType; fullImage.ContentLength = fullResizedBuffer.Length; } photoService.Save(fullImage, mediaFormat, out success); compiledSuccess = compiledSuccess && success; if (!compiledSuccess) { //log.Error("The following validation conditions cased the offering image upload to fail during a call to CreateOfferingImageUpload: {0}".FormatWith(validationState.GetMessages())); //ERROR MESSAGE NEEDED //return new FileUploadJsonResult { Data = new { Src = "", ErrorMessage = "An error occurred while trying to save you upload. Please try again." } }; } else { unitOfWork.Commit(); var thumbImageTemp = photoService.GetPhoto(thumbImageId); thumbImageTemp.Image = thumbResizedBuffer; string path = string.Format("{0}{1}", DefaultTicketStubImageLocation, thumbImageTemp.FileName); bool valid = photoService.SaveAs(thumbImageTemp, HttpContext.Current.Server.MapPath(path)); if (valid) { var fullImageTemp = photoService.GetPhoto(fullImageId); fullImageTemp.Image = fullResizedBuffer; path = string.Format("{0}{1}", DefaultTicketStubImageLocation, fullImageTemp.FileName); valid = photoService.SaveAs(fullImageTemp, HttpContext.Current.Server.MapPath(path)); if (valid) { imgTheImage.ImageUrl = LinkBuilder.GetTicketStubLink(fullImage.FileName); thumbImageTemp.Image = null; fullImageTemp.Image = null; CreateTicketStubNow(fullImage, showId); } else { //FREAK OUT!! } } } } } //ERROR MESSAGE NEEDED //return new FileUploadJsonResult { Data = new { Src = Url.TempImages(tempId), ErrorMessage = "" } }; } else { //ERROR MESSAGE NEEDED //return new FileUploadJsonResult { Data = new { Src = "", ErrorMessage = "The image you are trying to upload is listed as an invalid image type. Please use the approved types:{0}".FormatWith(_imageMediaFormats.ImageFormatSpecs.ToDebugString(",")) } }; } } //ERROR MESSAGE NEEDED //return new FileUploadJsonResult { Data = new { Src = "", ErrorMessage = "No file uploaded." } }; }