protected override void Process() { using (var unit = GetUnitOfWork()) { log.InfoFormat("Start Cleanup image directory"); string drive = GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value; string mediaDirectory = drive; #region Cleanup var _mediaRepo = unit.Scope.Repository <ProductMedia>(); //var imagelist = _mediaRepo.GetAll(i => i.MediaPath != null).OrderBy(c => c.ProductID).ToList(); //try //{ // int couterProduct = 0; // int logCount = 0; // string imageDirectory = drive;//GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value; // imageDirectory = Path.Combine(imageDirectory, "Concentrator"); // var files = Directory.GetFiles(Path.Combine(imageDirectory, "Products")); // int totalProducts = files.Count(); // log.DebugFormat("Found {0} images to check", totalProducts); // if (totalProducts != imagelist.Count) // { // foreach (var file in files) // { // couterProduct++; // logCount++; // if (logCount == 50) // { // log.DebugFormat("Media files Processed : {0}/{1}", couterProduct, totalProducts); // logCount = 0; // } // if (!imagelist.Any(x => Path.Combine(imageDirectory, x.MediaPath) == file)) // { // try // { // log.DebugFormat("Try to delete {0}", file); // File.Delete(file); // } // catch (Exception ex) // { // log.ErrorFormat("Delete file {0} failed: {1}", file, ex.InnerException); // } // } // } // } // log.Debug("Finish download image process"); //} //catch (Exception ex) //{ // log.Error("Error processing images", ex); //} #endregion log.Info("Finish cleanup images"); var mediaList = _mediaRepo.GetAll(i => i.MediaPath == null && i.MediaUrl != "").OrderBy(c => c.ProductID).ToList(); int couterProduct = 0; int logCount = 0; int totalProducts = mediaList.Count; log.DebugFormat("Found {0} media items to process", totalProducts); try { foreach (var mediaItem in mediaList) { couterProduct++; logCount++; if (logCount == 50) { log.DebugFormat("Media Processed : {0}/{1}", couterProduct, totalProducts); logCount = 0; } string fileName = string.Format("{0}_{1}_{2}_{3}", mediaItem.MediaID, mediaItem.ProductID, mediaItem.Sequence, mediaItem.VendorID); string imagePath = string.Empty; try { imagePath = GetImage(mediaItem.MediaUrl, fileName, "Products", mediaDirectory); } catch { imagePath = GetFile(mediaItem.MediaUrl, fileName, "Products", mediaDirectory); } if (!string.IsNullOrEmpty(imagePath)) { mediaItem.MediaPath = imagePath; mediaItem.Resolution = MediaUtility.getRes(Path.Combine(mediaDirectory, imagePath)); mediaItem.Size = MediaUtility.getSize(Path.Combine(mediaDirectory, imagePath)).HasValue ? MediaUtility.getSize(Path.Combine(mediaDirectory, imagePath)) : null; unit.Save(); } } log.Debug("Finish download image process"); } catch (Exception ex) { log.Error("Error processing images", ex); } log.Debug("Start set media information"); mediaList = _mediaRepo.GetAll(i => i.MediaPath != null && (i.Resolution == null || i.Size == null)).OrderBy(c => c.ProductID).ToList(); couterProduct = 0; logCount = 0; totalProducts = mediaList.Count; log.DebugFormat("Found {0} media items to set information", totalProducts); mediaList.ForEach(media => { couterProduct++; logCount++; if (logCount == 50) { log.DebugFormat("Media information Processed : {0}/{1}", couterProduct, totalProducts); logCount = 0; } media.Resolution = MediaUtility.getRes(Path.Combine(mediaDirectory, media.MediaPath)); media.Size = MediaUtility.getSize(Path.Combine(mediaDirectory, media.MediaPath)).HasValue ? MediaUtility.getSize(Path.Combine(mediaDirectory, media.MediaPath)) : null; unit.Save(); }); log.Debug("Finish set media information"); } }
protected override void Process() { string FTPMediaDirectory = ConfigurationManager.AppSettings["FTPMediaDirectory"]; string ImageWatchDirectory = ConfigurationManager.AppSettings["ImageWatchDirectory"]; string[] files = Directory.GetFiles(ImageWatchDirectory); using (var unit = GetUnitOfWork()) { int TypeID = unit.Scope.Repository <MediaType>().GetSingle(x => x.Type == "Image").TypeID; int counter = 0; int total = files.Count(); int totalNumberOfImagesToProcess = total; log.InfoFormat("Start: adding {0} images", total); foreach (var image in files) { var _productmediaRepo = unit.Scope.Repository <ProductMedia>(); #region Imageinfo //Retrieve all info; look in watchedfolder and look up corresponding info in database string filename = Path.GetFileName(image); string filewithoutextension = Path.GetFileNameWithoutExtension(image); string[] filearray = filewithoutextension.Split('_'); string CustomItemNumber = filearray[0]; string ColorCode = filearray[1]; string FrontOrBack = filearray[2]; string Extension = Path.GetExtension(image); string Resolution = MediaUtility.getRes(Path.Combine(ImageWatchDirectory, filename)); int? Size = MediaUtility.getSize(Path.Combine(ImageWatchDirectory, filename)).HasValue ? MediaUtility.getSize(Path.Combine(ImageWatchDirectory, filename)) : null; string Description = filearray[1] + filearray[2]; string Sequence = FrontOrBack.Equals("f") ? "0" : "1"; var vendorAssortment = (from a in unit.Scope.Repository <VendorAssortment>().GetAll(x => x.CustomItemNumber.Trim() == CustomItemNumber || x.CustomItemNumber.Trim().StartsWith(CustomItemNumber + ColorCode)).ToList() select new ProductMedia { ProductID = a.ProductID, Sequence = int.Parse(Sequence), VendorID = a.VendorID, TypeID = TypeID, MediaUrl = null, MediaPath = Path.Combine("Products", filename), FileName = null, MediaID = 0, Description = Description, Resolution = Resolution, Size = Size }); #endregion #region Progresscount //Progresscounter log.InfoFormat("Still need to process {0} of {1}; {2} done;", totalNumberOfImagesToProcess, total, total - totalNumberOfImagesToProcess); totalNumberOfImagesToProcess--; counter++; #endregion //Add to database _productmediaRepo.Add(vendorAssortment); //Move image to FTPMediaDirectory System.IO.File.Move(Path.Combine(ImageWatchDirectory, filename), Path.Combine(FTPMediaDirectory + filename)); } unit.Save(); } }