Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        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();
        }