private static void MissingImages() { var checkedImages = d.ProductsImages.Where(t => t.ImageChecked).ToList(); var internetAccess = new InternetAccess(); var siteURL = d.Settings.Where(t => t.SettingName == "URL").FirstOrDefault().SettingValue; var site = (from s in d.Sites where s.SiteURL == siteURL select s).FirstOrDefault(); var connection = internetAccess.FtpConnect(site.FTPServer, 21, site.FTPUsername, site.FTPPass, true); connection.SetDirectory(site.ImagesPath); var files = connection.GetDirectoryContents("*.jpeg", CachingFlags.None); var filesNames = files.Where(t => !t.Name.Contains("_med") && !t.Name.Contains("_sm")).Select(t => t.Name.Replace(".jpeg", "")).ToList(); if (checkedImages.Count != filesNames.Count) { foreach (var file in filesNames) { if (checkedImages.Where(t => t.ImageID.ToString() == file).FirstOrDefault() == null) { // удалить по ftp try { connection.DeleteFile(file + ".jpeg"); connection.DeleteFile(file + "_med.jpeg"); connection.DeleteFile(file + "_sm.jpeg"); } catch { } } } files = connection.GetDirectoryContents("*.jpeg", CachingFlags.None); filesNames = files.Select(t => t.Name.Replace(".jpeg", "")).ToList(); } if (checkedImages.Count != filesNames.Count) { foreach (var image in checkedImages) { if (!filesNames.Contains(image.ImageID.ToString())) { var originalImageName = homeFolder + image.ImageID.ToString();; var ftpPath = site.ImagesPath + "/" + image.ImageID.ToString(); try { connection.UploadFile(originalImageName + ".jpeg", ftpPath + ".jpeg", TransferType.Binary, CachingFlags.None); if (site.ResizeImages && site.SmallBigSize != null) { connection.UploadFile(originalImageName + "_sm.jpeg", ftpPath + "_sm.jpeg", TransferType.Binary, CachingFlags.None); } if (site.ResizeImages && site.MedBigSize != null) { connection.UploadFile(originalImageName + "_med.jpeg", ftpPath + "_med.jpeg", TransferType.Binary, CachingFlags.None); } } catch { } } } } connection.Dispose(); internetAccess.Dispose(); }
private static void ClearImages() { Console.WriteLine("Clear Images"); var di = new DirectoryInfo(homeFolder); var filesCount = di.GetFiles().Where(t => !t.Name.Contains("_med") && !t.Name.Contains("_sm")).Count(); var dbImagesCount = d.ProductsImages.Count(); if (dbImagesCount != filesCount) { var internetAccess = new InternetAccess(); var siteURL = d.Settings.Where(t => t.SettingName == "URL").FirstOrDefault().SettingValue; var site = (from s in d.Sites where s.SiteURL == siteURL select s).FirstOrDefault(); try { var connection = internetAccess.FtpConnect(site.FTPServer, 21, site.FTPUsername, site.FTPPass, true); connection.SetDirectory(site.ImagesPath); var productsImages = d.ProductsImages.Select(t => t.ImageID.ToString()).ToList(); var files = di.GetFiles().Where(t => !t.Name.Contains("_med") && !t.Name.Contains("_sm")); foreach (var file in files) { var imageId = file.Name.Replace(".jpeg", ""); if (!productsImages.Contains(imageId)) { var medfile = new FileInfo(file.Name.Replace(".jpeg", "_med.jpeg")); var smfile = new FileInfo(file.Name.Replace(".jpeg", "_sm.jpeg")); file.Delete(); medfile.Delete(); smfile.Delete(); // удалить по ftp try { connection.DeleteFile(imageId + ".jpeg"); connection.DeleteFile(imageId + "_med.jpeg"); connection.DeleteFile(imageId + "_sm.jpeg"); } catch { } } } foreach (var img in d.ProductsImages) { if (!new FileInfo(di.FullName + "\\" + img.ImageID + ".jpeg").Exists) { var product = d.Products.Where(t => t.ImageId == img.ImageID).FirstOrDefault(); if (product != null) { product.ImageId = null; } d.ProductsImages.DeleteOnSubmit(img); } } d.SubmitChanges(); } catch { } } }
private static void ClearImages() { Console.WriteLine("Clear Images"); var di = new DirectoryInfo(homeFolder); var filesCount = di.GetFiles().Where(t => !t.Name.Contains("_med") && !t.Name.Contains("_sm")).Count(); var dbImagesCount = d.ProductsImages.Count(); if (dbImagesCount != filesCount) { var internetAccess = new InternetAccess(); var siteURL = d.Settings.Where(t => t.SettingName == "URL").FirstOrDefault().SettingValue; var site = (from s in d.Sites where s.SiteURL == siteURL select s).FirstOrDefault(); try { var connection = internetAccess.FtpConnect(site.FTPServer, 21, site.FTPUsername, site.FTPPass, true); connection.SetDirectory(site.ImagesPath); var productsImages = d.ProductsImages.Select(t=>t.ImageID.ToString()).ToList(); var files = di.GetFiles().Where(t => !t.Name.Contains("_med") && !t.Name.Contains("_sm")); foreach (var file in files) { var imageId = file.Name.Replace(".jpeg", ""); if (!productsImages.Contains(imageId)) { var medfile = new FileInfo(file.Name.Replace(".jpeg", "_med.jpeg")); var smfile = new FileInfo(file.Name.Replace(".jpeg", "_sm.jpeg")); file.Delete(); medfile.Delete(); smfile.Delete(); // удалить по ftp try { connection.DeleteFile(imageId + ".jpeg"); connection.DeleteFile(imageId + "_med.jpeg"); connection.DeleteFile(imageId + "_sm.jpeg"); } catch { } } } foreach (var img in d.ProductsImages) { if (!new FileInfo(di.FullName + "\\" + img.ImageID + ".jpeg").Exists) { var product = d.Products.Where(t => t.ImageId == img.ImageID).FirstOrDefault(); if (product != null) product.ImageId = null; d.ProductsImages.DeleteOnSubmit(img); } } d.SubmitChanges(); } catch { } } }
private static void MissingImages() { var checkedImages = d.ProductsImages.Where(t => t.ImageChecked).ToList(); var internetAccess = new InternetAccess(); var siteURL = d.Settings.Where(t => t.SettingName == "URL").FirstOrDefault().SettingValue; var site = (from s in d.Sites where s.SiteURL == siteURL select s).FirstOrDefault(); var connection = internetAccess.FtpConnect(site.FTPServer, 21, site.FTPUsername, site.FTPPass, true); connection.SetDirectory(site.ImagesPath); var files = connection.GetDirectoryContents("*.jpeg", CachingFlags.None); var filesNames = files.Where(t => !t.Name.Contains("_med") && !t.Name.Contains("_sm")).Select(t => t.Name.Replace(".jpeg", "")).ToList(); if (checkedImages.Count != filesNames.Count) { foreach (var file in filesNames) { if (checkedImages.Where(t => t.ImageID.ToString() == file).FirstOrDefault() == null) { // удалить по ftp try { connection.DeleteFile(file + ".jpeg"); connection.DeleteFile(file + "_med.jpeg"); connection.DeleteFile(file + "_sm.jpeg"); } catch { } } } files = connection.GetDirectoryContents("*.jpeg", CachingFlags.None); filesNames = files.Select(t => t.Name.Replace(".jpeg", "")).ToList(); } if (checkedImages.Count != filesNames.Count) { foreach (var image in checkedImages) { if (!filesNames.Contains(image.ImageID.ToString())) { var originalImageName = homeFolder + image.ImageID.ToString(); ; var ftpPath = site.ImagesPath + "/" + image.ImageID.ToString(); try { connection.UploadFile(originalImageName + ".jpeg", ftpPath + ".jpeg", TransferType.Binary, CachingFlags.None); if (site.ResizeImages && site.SmallBigSize != null) { connection.UploadFile(originalImageName + "_sm.jpeg", ftpPath + "_sm.jpeg", TransferType.Binary, CachingFlags.None); } if (site.ResizeImages && site.MedBigSize != null) { connection.UploadFile(originalImageName + "_med.jpeg", ftpPath + "_med.jpeg", TransferType.Binary, CachingFlags.None); } } catch { } } } } connection.Dispose(); internetAccess.Dispose(); }