예제 #1
0
        protected override void Process()
        {
            using (var unit = GetUnitOfWork())
            {
                var thumbs    = unit.Scope.Repository <ThumbnailGenerator>().GetAll().ToList();
                var tumbTypes = GetConfiguration().AppSettings.Settings["ThumbTypeIDs"].Value.Split(',').Select(x => int.Parse(x));
                var mediaPath = ConfigurationManager.AppSettings["FTPMediaDirectory"];
                var thumbPath = Path.Combine(mediaPath, ConfigurationManager.AppSettings["FTPMediaProductThumbDirectory"]);

                var productImages = unit.Scope.Repository <ProductMedia>().GetAll(x => tumbTypes.Contains(x.TypeID) && x.MediaPath != null).ToList();

                thumbs.ForEach(thumb =>
                {
                    var destinationPath = Path.Combine(thumbPath, thumb.ThumbnailGeneratorID.ToString());

                    if (!Directory.Exists(Path.Combine(destinationPath, "Products")))
                    {
                        Directory.CreateDirectory(Path.Combine(destinationPath, "Products"));
                    }

                    int width  = thumb.Width;
                    int heigth = thumb.Height;

                    int couterProduct = 0;
                    int logCount      = 0;
                    int totalProducts = productImages.Count;
                    log.DebugFormat("Found {0} media items to process for thumb generator {1}", totalProducts, thumb.Description);

                    productImages.ForEach(media =>
                    {
                        couterProduct++;
                        logCount++;
                        if (logCount == 50)
                        {
                            log.DebugFormat("Media Processed : {0}/{1}", couterProduct, totalProducts);
                            logCount = 0;
                        }

                        try
                        {
                            var pathToLook = Path.Combine(mediaPath, media.MediaPath);

                            var ext                 = Path.GetExtension(pathToLook).ToLower();
                            var isTiff              = (ext == ".tiff" || ext == ".tif");
                            FileInfo inf            = new FileInfo(pathToLook);
                            var destinationFilePath = Path.Combine(destinationPath, inf.Name);

                            ProductMediaTumbnail productThumb = unit.Scope.Repository <ProductMediaTumbnail>().GetSingle(x => x.MediaID == media.MediaID && x.ThumbnailGeneratorID == thumb.ThumbnailGeneratorID);

                            if (System.IO.File.Exists(pathToLook) && (productThumb == null || !File.Exists(destinationFilePath) || System.IO.File.GetLastWriteTime(pathToLook) > System.IO.File.GetLastWriteTime(destinationFilePath)))
                            {
                                if (!File.Exists(destinationFilePath))
                                {
                                    using (Image img = Image.FromFile(pathToLook))
                                    {
                                        if (isTiff)
                                        {
                                            TiffConverter converter = new TiffConverter(Path.Combine(pathToLook));
                                            converter.WriteTo(destinationFilePath.Replace(ext, ".png"), width, heigth);
                                        }
                                        else
                                        {
                                            var image = ImageUtility.GetFixedSizeImage(img, width > 0 ? width : img.Width, heigth > 0 ? heigth : img.Height, true, Color.White);
                                            image.Save(destinationFilePath.Replace(ext, ".png"), System.Drawing.Imaging.ImageFormat.Png);
                                        }
                                    }
                                }

                                if (productThumb == null)
                                {
                                    productThumb = new ProductMediaTumbnail()
                                    {
                                        MediaID = media.MediaID,
                                        ThumbnailGeneratorID = thumb.ThumbnailGeneratorID,
                                        Path = Path.Combine(thumb.ThumbnailGeneratorID.ToString(), inf.Name.Replace(ext, ".png"))
                                    };

                                    unit.Scope.Repository <ProductMediaTumbnail>().Add(productThumb);
                                    unit.Save();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Error("Fail to Generate thumb for " + media.MediaPath, ex);
                        }
                    });
                });
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/png";

            bool   networkDrive = false;
            string drive        = ConfigurationManager.AppSettings["FTPMediaDirectory"].ToString();

            //string drive = @"\\SOL\Company_Shares\Database Backup";
            bool.TryParse(ConfigurationManager.AppSettings["IsNetworkDrive"], out networkDrive);

            string destinationPath = drive;

            if (networkDrive)
            {
                NetworkDrive oNetDrive = new NetworkDrive();
                try
                {
                    destinationPath      = @"H:\";
                    destinationPath      = Path.Combine(destinationPath, "Concentrator");
                    oNetDrive.LocalDrive = "H:";
                    oNetDrive.ShareName  = drive;
                    oNetDrive.MapDrive("Diract", "Concentrator01");
                    //oNetDrive.MapDrive();
                }
                catch (Exception err)
                {
                    //log.Error("Invalid network drive", err);
                }
                oNetDrive = null;
            }

            var mediaPath = context.Request.Params["MediaPath"];
            //var dir = ConfigurationManager.AppSettings["FTPMediaDirectory"].ToString();

            int width = -1;

            if (context.Request.Params["width"] != null)
            {
                int.TryParse(context.Request.Params["width"], out width);
            }

            int heigth = -1;

            if (context.Request.Params["height"] != null)
            {
                int.TryParse(context.Request.Params["height"], out heigth);
            }

            var pathToLook = Path.Combine(destinationPath, mediaPath);

            var ext    = Path.GetExtension(pathToLook).ToLower();
            var isTiff = (ext == ".tiff" || ext == ".tif");

            using (Image img = Image.FromFile(pathToLook))
            {
                if (isTiff)
                {
                    TiffConverter converter = new TiffConverter(Path.Combine(destinationPath, mediaPath));
                    converter.WriteTo(context.Response.OutputStream, width, heigth);
                }
                else
                {
                    var image = ImageUtility.GetFixedSizeImage(img, width > 0 ? width : img.Width, heigth > 0 ? heigth : img.Height, true);
                    image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }