/// <summary>See IProductService.</summary>
        public void DeleteImage(ProductImage toDelete)
        {
            Contract.Assert(toDelete != null, "Tried to remove a product image that does not exist.");

            DeleteProductImageFiles(toDelete);
            saver.MarkForDeletion(toDelete);
        }
Exemple #2
0
        /// <summary>Get a URL for a product image. Returns text placeholder if there is no primary image.</summary>
        public static string GetProductImageThumbnailURL(HtmlHelper helper, ProductImage image, bool linkToFullsize)
        {
            if (image == null) return "[no image]";

            string imageTag = helper.Image(image.GetUrl(ProductImage.SizeOption.SMALL), image.Description, new { border = "0" });
            return (linkToFullsize)
                ? String.Format("<a href='{0}' target='_blank'>{1}</a>", image.GetUrl(ProductImage.SizeOption.LARGE), imageTag)
                : imageTag;
        }
 private int GetWidthPixels(ProductImage.SizeOption size)
 {
     switch (size)
     {
         case ProductImage.SizeOption.SMALL: return SMALL_WIDTH;
         case ProductImage.SizeOption.LARGE: return LARGE_WIDTH;
         default: throw new Exception("Unknown size option " + size.ToString());
     }
 }
        public Bitmap GetResized(Image original, ProductImage.SizeOption size)
        {
            Rectangle scaledRectangle = GetScaledRectangle(original.Width, original.Height, GetWidthPixels(size));

            Bitmap newImage = new Bitmap(scaledRectangle.Width, scaledRectangle.Height, original.PixelFormat);
            Graphics toDrawOn = Graphics.FromImage(newImage);
            toDrawOn.CompositingQuality = CompositingQuality.HighQuality;
            toDrawOn.SmoothingMode = SmoothingMode.HighQuality;
            toDrawOn.InterpolationMode = InterpolationMode.HighQualityBicubic;
            toDrawOn.DrawImage(original, scaledRectangle);
            return newImage;
        }
 /// <summary>Resize to size and save file to file system.</summary>
 /// <param name="pi"></param><param name="image"></param>
 private void SaveImageFiles(ProductImage pi, Image image)
 {
     SaveImageFile(pi, _imageResizeService.GetResized(image, ProductImage.SizeOption.SMALL), ProductImage.SizeOption.SMALL);
     SaveImageFile(pi, _imageResizeService.GetResized(image, ProductImage.SizeOption.LARGE), ProductImage.SizeOption.LARGE);
 }
        /// <summary>If this ProductImage has an Image then save the Image to the file system.</summary>
        private void SaveImageFile(ProductImage pi, System.Drawing.Image resized, ProductImage.SizeOption size)
        {
            if (!Directory.Exists(pi.Product.MetaProduct.Agency.ContentDirectory))
                Directory.CreateDirectory(pi.Product.MetaProduct.Agency.ContentDirectory);

            Contract.Assert(resized != null, "Image file save failed because Image is null.");
            resized.Save(pi.GetFullyQualifiedFileName(size), System.Drawing.Imaging.ImageFormat.Jpeg);
            resized.Dispose();
        }
 private void DeleteProductImageFiles(ProductImage image)
 {
     if (File.Exists(image.GetFullyQualifiedFileName(ProductImage.SizeOption.SMALL)))
         File.Delete(image.GetFullyQualifiedFileName(ProductImage.SizeOption.SMALL));
     if (File.Exists(image.GetFullyQualifiedFileName(ProductImage.SizeOption.LARGE)))
         File.Delete(image.GetFullyQualifiedFileName(ProductImage.SizeOption.LARGE));
 }
 public ProductImageBuilder WithPrimaryOrSecondary(ProductImage.PrimaryOrSecondaryOption primaryOrSecondary)
 {
     defaultPrimaryOrSecondary = primaryOrSecondary;
     return this;
 }
		private void detach_ProductImages(ProductImage entity)
		{
			this.SendPropertyChanging();
			entity.Product = null;
		}
 public UploaderData(Product product, ProductImage.PrimaryOrSecondaryOption primaryOrSecondary)
 {
     _product = product;
     _primaryOrSecondary = primaryOrSecondary;
 }
 public ActionResult Uploader(int id, ProductImage.PrimaryOrSecondaryOption primaryOrSecondary)
 {
     return View("ImageUpload", new UploaderData(_productRepo.Find(id), primaryOrSecondary));
 }
        public ActionResult Upload(int productId, ProductImage.PrimaryOrSecondaryOption primaryOrSecondary)
        {
            HttpPostedFileBase file = Request.Files["imageData"];
            if (file == null || file.ContentLength == 0)
            {
                SetErrorMessage("No file uploaded.");
                return RedirectToRoute(new RouteValueDictionary(new { action = "Edit", id = productId }));
            }

            if (!file.IsFileType(new string[] {"jpg", "jpeg"}))
            {
                SetErrorMessage("Incorrect file type. Valid extensions are .jpg and .jpeg.");
                return RedirectToRoute(new RouteValueDictionary(new { action = "Edit", id = productId }));
            }

            _productService.SaveImage(productId, file, primaryOrSecondary, Server.HtmlEncode(Request.Form["description"]));
            saver.SaveAll();
            SetSuccessMessage("File uploaded successfully.");
            return RedirectToRoute(new RouteValueDictionary(new { action = "Edit", id = productId }));
        }
 partial void DeleteProductImage(ProductImage instance);
 partial void UpdateProductImage(ProductImage instance);
 partial void InsertProductImage(ProductImage instance);
Exemple #16
0
        /// <summary>
        /// See IProductService.
        /// </summary>
        /// <param name="productId"></param><param name="file"></param><param name="primaryOrSecondary"></param><returns></returns>
        public void SaveImage(int productId, HttpPostedFileBase file, ProductImage.PrimaryOrSecondaryOption primaryOrSecondary, string imageDescription)
        {
            Product hasImages = _productRepository.Find(productId); // will throw exception if product is not found.
            Contract.Assert(file != null
                            && file.ContentLength > 0
                            && (file.FileName.EndsWith("jpg", StringComparison.CurrentCultureIgnoreCase) || file.FileName.EndsWith("jpeg", StringComparison.CurrentCultureIgnoreCase)),
                "File is not valid for saving.");
            Contract.Assert(imageDescription != null, "Image descrpition cannot be null.");

            if (hasImages.PrimaryImage != null && primaryOrSecondary == ProductImage.PrimaryOrSecondaryOption.PRIMARY)
                DeleteImage(hasImages.PrimaryImage);

            ProductImage pi = _productRepository.CreateImage(imageDescription, primaryOrSecondary);
            hasImages.ProductImages.Add(pi);
            saver.SaveAll();   // violates rule about saving in controls. Done because SaveImageFiles needs the
                                            // ProductImage id to generate the correct file name.

            using (System.Drawing.Image uploadedImage = System.Drawing.Image.FromStream(file.InputStream))
                SaveImageFiles(pi, uploadedImage);
        }
		private void attach_ProductImages(ProductImage entity)
		{
			this.SendPropertyChanging();
			entity.Product = this;
		}