private void ExecImage(string FTP, string User, string Password, Model.Local.Article Article, Model.Local.ArticleImage ArticleImage, Model.Local.ArticleImageRepository ArticleImageRepository) { Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository(); Model.Prestashop.PsImage PsImage = null; Model.Prestashop.PsImageLangRepository PsImageLangRepository = new Model.Prestashop.PsImageLangRepository(); Model.Prestashop.PsImageLang PsImageLang; Boolean isImageLang = false; Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository(); List <Model.Prestashop.PsImageType> ListPsImageType = PsImageTypeRepository.ListProduct(1); try { if (ArticleImage.Pre_Id == null) { PsImage = new Model.Prestashop.PsImage(); PsImage.IDProduct = Convert.ToUInt32(Article.Pre_Id); PsImage.Position = (ushort)ExecPosition((uint)Article.Pre_Id.Value, (uint)ArticleImage.ImaArt_Position, PsImageRepository, PsImage); PsImage.Cover = ExecCover((uint)Article.Pre_Id.Value, ArticleImage.ImaArt_Default, PsImageRepository, PsImage); PsImageRepository.Add(PsImage, Global.CurrentShop.IDShop); #region lang isImageLang = false; PsImageLang = new Model.Prestashop.PsImageLang(); if (PsImageLangRepository.ExistImageLang(PsImage.IDImage, Core.Global.Lang)) { PsImageLang = PsImageLangRepository.ReadImageLang(PsImage.IDImage, Core.Global.Lang); isImageLang = true; } PsImageLang.Legend = ArticleImage.ImaArt_Name; if (isImageLang == true) { PsImageLangRepository.Save(); } else { PsImageLang.IDImage = PsImage.IDImage; PsImageLang.IDLang = Core.Global.Lang; PsImageLangRepository.Add(PsImageLang); } // <JG> 26/12/2012 ajout insertion autres langues actives si non renseignées try { Model.Prestashop.PsLangRepository PsLangRepository = new Model.Prestashop.PsLangRepository(); foreach (Model.Prestashop.PsLang PsLang in PsLangRepository.ListActive(1, Global.CurrentShop.IDShop)) { if (!PsImageLangRepository.ExistImageLang(PsImage.IDImage, PsLang.IDLang)) { PsImageLang = new Model.Prestashop.PsImageLang() { IDImage = PsImage.IDImage, IDLang = PsLang.IDLang, Legend = ArticleImage.ImaArt_Name }; PsImageLangRepository.Add(PsImageLang); } } } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } #endregion try { // <JG> 10/04/2013 gestion système d'images string ftpPath = "/img/p/"; switch (Core.Global.GetConfig().ConfigImageStorageMode) { case Core.Parametres.ImageStorageMode.old_system: #region old_system // no action on path break; #endregion case Core.Parametres.ImageStorageMode.new_system: default: #region new_system //System.Net.FtpWebRequest ftp_folder = null; foreach (char directory in PsImage.IDImage.ToString()) { ftpPath += directory + "/"; #region MyRegion try { FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTP + ftpPath); request.Credentials = new NetworkCredential(User, Password); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; request.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; request.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse makeDirectoryResponse = (FtpWebResponse)request.GetResponse(); } catch //Exception ex { //System.Windows.MessageBox.Show(ex.ToString()); } #endregion //try //{ // ftp_folder = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(FTP + ftpPath); // ftp_folder.Credentials = new System.Net.NetworkCredential(User, Password); // ftp_folder.UseBinary = true; // ftp_folder.UsePassive = true; // ftp_folder.KeepAlive = false; // ftp_folder.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory; // System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp_folder.GetResponse(); // System.IO.Stream ftpStream = response.GetResponseStream(); // ftpStream.Close(); // response.Close(); //} //catch(Exception ex) //{ // System.Windows.MessageBox.Show(ex.ToString()); //} } break; #endregion } #region Upload des images String extension = ArticleImage.GetExtension; if (System.IO.File.Exists(ArticleImage.TempFileName)) { string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + ".jpg" : FTP + ftpPath + PsImage.IDImage + ".jpg"; System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new System.Net.NetworkCredential(User, Password); //userid and password for the ftp server to given ftp.UseBinary = true; ftp.UsePassive = true; ftp.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; System.IO.FileStream fs = System.IO.File.OpenRead(ArticleImage.TempFileName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); System.IO.Stream ftpstream = ftp.GetRequestStream(); ftpstream.Write(buffer, 0, buffer.Length); ftpstream.Close(); ftp.Abort(); } foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType) { String PathImg = ArticleImage.FileName(PsImageType.Name); if (System.IO.File.Exists(PathImg)) { string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + "-" + PsImageType.Name + ".jpg" : FTP + ftpPath + PsImage.IDImage + "-" + PsImageType.Name + ".jpg"; System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new System.Net.NetworkCredential(User, Password); //userid and password for the ftp server to given ftp.UseBinary = true; ftp.UsePassive = true; ftp.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; System.IO.FileStream fs = System.IO.File.OpenRead(PathImg); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); System.IO.Stream ftpstream = ftp.GetRequestStream(); ftpstream.Write(buffer, 0, buffer.Length); ftpstream.Close(); ftp.Abort(); } } #endregion ArticleImage.Pre_Id = Convert.ToInt32(PsImage.IDImage); ArticleImageRepository.Save(); } catch (Exception ex) { Core.Error.SendMailError("[UPLOAD FTP IMAGE ARTICLE]<br />" + ex.ToString()); PsImageLangRepository.DeleteAll(PsImageLangRepository.ListImage(PsImage.IDImage)); PsImageRepository.Delete(PsImage); } } // option car si PAS GADA écrase infos PS else if (Core.Global.GetConfig().ConfigImageSynchroPositionLegende) { PsImage = new Model.Prestashop.PsImage(); if (PsImageRepository.ExistImage((uint)ArticleImage.Pre_Id)) { PsImage = PsImageRepository.ReadImage((uint)ArticleImage.Pre_Id); if (PsImage.Position != (UInt16)ArticleImage.ImaArt_Position || PsImage.Cover != #if (PRESTASHOP_VERSION_172 || PRESTASHOP_VERSION_161) ((ArticleImage.ImaArt_Default) ? Convert.ToByte(ArticleImage.ImaArt_Default) : (byte?)null)) #else Convert.ToByte(ArticleImage.ImaArt_Default)) #endif { PsImage.Position = (ushort)ExecPosition((uint)Article.Pre_Id, (uint)ArticleImage.ImaArt_Position, PsImageRepository, PsImage); PsImage.Cover = ExecCover((uint)Article.Pre_Id, ArticleImage.ImaArt_Default, PsImageRepository, PsImage); PsImageRepository.Save(); Model.Prestashop.PsImageShopRepository PsImageShopRepository = new Model.Prestashop.PsImageShopRepository(); Model.Prestashop.PsImageShop PsImageShop = PsImageShopRepository.ReadImage(PsImage.IDImage); if (PsImageShop != null && PsImageShop.Cover != #if (PRESTASHOP_VERSION_160) (sbyte)PsImage.Cover #else PsImage.Cover #endif ) { #if (PRESTASHOP_VERSION_160) PsImageShop.Cover = (sbyte)PsImage.Cover; #else PsImageShop.Cover = PsImage.Cover; #endif PsImageShopRepository.Save(); } } #region lang PsImageLang = new Model.Prestashop.PsImageLang(); isImageLang = false; if (PsImageLangRepository.ExistImageLang(PsImage.IDImage, Core.Global.Lang)) { PsImageLang = PsImageLangRepository.ReadImageLang(PsImage.IDImage, Core.Global.Lang); isImageLang = true; } PsImageLang.Legend = ArticleImage.ImaArt_Name; if (isImageLang == true) { PsImageLangRepository.Save(); } else { PsImageLang.IDImage = PsImage.IDImage; PsImageLang.IDLang = Core.Global.Lang; PsImageLangRepository.Add(PsImageLang); } // <JG> 26/12/2012 ajout insertion autres langues actives si non renseignées try { Model.Prestashop.PsLangRepository PsLangRepository = new Model.Prestashop.PsLangRepository(); foreach (Model.Prestashop.PsLang PsLang in PsLangRepository.ListActive(1, Global.CurrentShop.IDShop)) { if (!PsImageLangRepository.ExistImageLang(PsImage.IDImage, PsLang.IDLang)) { PsImageLang = new Model.Prestashop.PsImageLang() { IDImage = PsImage.IDImage, IDLang = PsLang.IDLang, Legend = ArticleImage.ImaArt_Name }; PsImageLangRepository.Add(PsImageLang); } } } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } #endregion } } } catch (Exception ex) { Core.Error.SendMailError("[SYNCHRO IMAGE ARTICLE]<br />" + ex.ToString()); if (PsImage != null) { PsImageLangRepository.DeleteAll(PsImageLangRepository.ListImage(PsImage.IDImage)); PsImageRepository.Delete(PsImage); } } }
public Boolean Exec(String PathImg, Int32 ArticleSend, int position, int Declination) { Boolean result = false; Int32 IdArticleImage = 0; try { String extension = Path.GetExtension(PathImg); String FileName = Path.GetFileName(PathImg); Model.Local.ArticleImageRepository ArticleImageRepository = new Model.Local.ArticleImageRepository(); if (!ArticleImageRepository.ExistArticleFile(ArticleSend, FileName)) { Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository(); Model.Local.Article Article = ArticleRepository.ReadArticle(ArticleSend); Model.Local.ArticleImage ArticleImage = new Model.Local.ArticleImage() { Art_Id = Article.Art_Id, ImaArt_Name = Article.Art_Name, ImaArt_Image = "", ImaArt_DateAdd = DateTime.Now, ImaArt_SourceFile = FileName }; ArticleImage.ImaArt_Position = this.ReadNextPosition(Article, position); ArticleImage.ImaArt_Default = !(new Model.Local.ArticleImageRepository().ExistArticleDefault(Article.Art_Id, true)); ArticleImageRepository.Add(ArticleImage); ArticleImage.ImaArt_Image = String.Format("{0}" + extension, ArticleImage.ImaArt_Id); ArticleImageRepository.Save(); IdArticleImage = ArticleImage.ImaArt_Id; string uri = PathImg.Replace("File:///", "").Replace("file:///", "").Replace("File://", "\\\\").Replace("file://", "\\\\").Replace("/", "\\"); System.IO.File.Copy(uri, ArticleImage.TempFileName); Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository(); List <Model.Prestashop.PsImageType> ListPsImageType = PsImageTypeRepository.ListProduct(1); System.Drawing.Image img = System.Drawing.Image.FromFile(ArticleImage.TempFileName); foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType) { Core.Img.resizeImage(img, Convert.ToInt32(PsImageType.Width), Convert.ToInt32(PsImageType.Height), ArticleImage.FileName(PsImageType.Name)); } Core.Img.resizeImage(img, Core.Global.GetConfig().ConfigImageMiniatureWidth, Core.Global.GetConfig().ConfigImageMiniatureHeight, ArticleImage.SmallFileName); img.Dispose(); // <JG> 28/10/2015 ajout attribution gamme/images if (Declination != 0) { if (ArticleImage.Article.TypeArticle == Model.Local.Article.enum_TypeArticle.ArticleMonoGamme || ArticleImage.Article.TypeArticle == Model.Local.Article.enum_TypeArticle.ArticleMultiGammes) { Model.Local.AttributeArticleRepository AttributeArticleRepository = new Model.Local.AttributeArticleRepository(); if (AttributeArticleRepository.Exist(Declination)) { Model.Local.AttributeArticleImageRepository AttributeArticleImageRepository = new Model.Local.AttributeArticleImageRepository(); if (!AttributeArticleImageRepository.ExistAttributeArticleImage(Declination, ArticleImage.ImaArt_Id)) { AttributeArticleImageRepository.Add(new Model.Local.AttributeArticleImage() { AttArt_Id = Declination, ImaArt_Id = ArticleImage.ImaArt_Id, }); } } } else if (ArticleImage.Article.TypeArticle == Model.Local.Article.enum_TypeArticle.ArticleComposition) { Model.Local.CompositionArticleRepository CompositionArticleRepository = new Model.Local.CompositionArticleRepository(); if (CompositionArticleRepository.Exist(Declination)) { Model.Local.CompositionArticleImageRepository CompositionArticleImageRepository = new Model.Local.CompositionArticleImageRepository(); if (!CompositionArticleImageRepository.ExistCompositionArticleImage(Declination, ArticleImage.ImaArt_Id)) { CompositionArticleImageRepository.Add(new Model.Local.CompositionArticleImage() { ComArt_Id = Declination, ImaArt_Id = ArticleImage.ImaArt_Id, }); } } } } result = true; } else if (Core.Global.GetConfig().ImportImageReplaceFiles) { FileInfo importfile = new FileInfo(PathImg); Model.Local.ArticleImage ArticleImage = ArticleImageRepository.ReadArticleFile(ArticleSend, FileName); FileInfo existfile = new FileInfo(ArticleImage.TempFileName); if ((ArticleImage.ImaArt_DateAdd == null || importfile.LastWriteTime > ArticleImage.ImaArt_DateAdd) || importfile.Length != existfile.Length) { try { // import nouveau fichier string uri = PathImg.Replace("File:///", "").Replace("file:///", "").Replace("File://", "\\\\").Replace("file://", "\\\\").Replace("/", "\\"); System.IO.File.Copy(uri, ArticleImage.TempFileName, true); Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository(); List <Model.Prestashop.PsImageType> ListPsImageType = PsImageTypeRepository.ListProduct(1); System.Drawing.Image img = System.Drawing.Image.FromFile(ArticleImage.TempFileName); foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType) { Core.Img.resizeImage(img, Convert.ToInt32(PsImageType.Width), Convert.ToInt32(PsImageType.Height), ArticleImage.FileName(PsImageType.Name)); } Core.Img.resizeImage(img, Core.Global.GetConfig().ConfigImageMiniatureWidth, Core.Global.GetConfig().ConfigImageMiniatureHeight, ArticleImage.SmallFileName); Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository(); if (ArticleImage.Pre_Id != null && PsImageRepository.ExistImage((uint)ArticleImage.Pre_Id)) { String FTP = Core.Global.GetConfig().ConfigFTPIP; String User = Core.Global.GetConfig().ConfigFTPUser; String Password = Core.Global.GetConfig().ConfigFTPPassword; Model.Prestashop.PsImage PsImage = PsImageRepository.ReadImage((uint)ArticleImage.Pre_Id); string ftpPath = "/img/p/"; switch (Core.Global.GetConfig().ConfigImageStorageMode) { case Core.Parametres.ImageStorageMode.old_system: #region old_system // no action on path break; #endregion case Core.Parametres.ImageStorageMode.new_system: default: #region new_system //System.Net.FtpWebRequest ftp_folder = null; foreach (char directory in PsImage.IDImage.ToString()) { ftpPath += directory + "/"; #region MyRegion try { System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(FTP + ftpPath); request.Credentials = new System.Net.NetworkCredential(User, Password); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; request.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory; System.Net.FtpWebResponse makeDirectoryResponse = (System.Net.FtpWebResponse)request.GetResponse(); } catch //Exception ex { //System.Windows.MessageBox.Show(ex.ToString()); } #endregion } break; #endregion } #region Upload des images extension = ArticleImage.GetExtension; if (System.IO.File.Exists(ArticleImage.TempFileName)) { string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + ".jpg" : FTP + ftpPath + PsImage.IDImage + ".jpg"; System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new System.Net.NetworkCredential(User, Password); //userid and password for the ftp server to given ftp.UseBinary = true; ftp.UsePassive = true; ftp.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; System.IO.FileStream fs = System.IO.File.OpenRead(ArticleImage.TempFileName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); System.IO.Stream ftpstream = ftp.GetRequestStream(); ftpstream.Write(buffer, 0, buffer.Length); ftpstream.Close(); ftp.Abort(); } foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType) { String localfile = ArticleImage.FileName(PsImageType.Name); if (System.IO.File.Exists(localfile)) { string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + "-" + PsImageType.Name + ".jpg" : FTP + ftpPath + PsImage.IDImage + "-" + PsImageType.Name + ".jpg"; System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new System.Net.NetworkCredential(User, Password); //userid and password for the ftp server to given ftp.UseBinary = true; ftp.UsePassive = true; ftp.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; System.IO.FileStream fs = System.IO.File.OpenRead(localfile); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); System.IO.Stream ftpstream = ftp.GetRequestStream(); ftpstream.Write(buffer, 0, buffer.Length); ftpstream.Close(); ftp.Abort(); } } #endregion } ArticleImage.ImaArt_DateAdd = DateTime.Now; ArticleImageRepository.Save(); // <JG> 28/10/2015 ajout attribution gamme/images if (Declination != 0) { if (ArticleImage.Article.TypeArticle == Model.Local.Article.enum_TypeArticle.ArticleMonoGamme || ArticleImage.Article.TypeArticle == Model.Local.Article.enum_TypeArticle.ArticleMultiGammes) { Model.Local.AttributeArticleRepository AttributeArticleRepository = new Model.Local.AttributeArticleRepository(); if (AttributeArticleRepository.Exist(Declination)) { Model.Local.AttributeArticleImageRepository AttributeArticleImageRepository = new Model.Local.AttributeArticleImageRepository(); if (!AttributeArticleImageRepository.ExistAttributeArticleImage(Declination, ArticleImage.ImaArt_Id)) { AttributeArticleImageRepository.Add(new Model.Local.AttributeArticleImage() { AttArt_Id = Declination, ImaArt_Id = ArticleImage.ImaArt_Id, }); } // réaffectation côté PrestaShop Model.Local.AttributeArticle AttributeArticle = AttributeArticleRepository.Read(Declination); if (AttributeArticle.Pre_Id != null && AttributeArticle.Pre_Id != 0) { Model.Prestashop.PsProductAttributeImageRepository PsProductAttributeImageRepository = new Model.Prestashop.PsProductAttributeImageRepository(); if (PsProductAttributeImageRepository.ExistProductAttributeImage((UInt32)AttributeArticle.Pre_Id, (UInt32)ArticleImage.Pre_Id) == false) { PsProductAttributeImageRepository.Add(new Model.Prestashop.PsProductAttributeImage() { IDImage = (UInt32)ArticleImage.Pre_Id, IDProductAttribute = (UInt32)AttributeArticle.Pre_Id, }); } } } } else if (ArticleImage.Article.TypeArticle == Model.Local.Article.enum_TypeArticle.ArticleComposition) { Model.Local.CompositionArticleRepository CompositionArticleRepository = new Model.Local.CompositionArticleRepository(); if (CompositionArticleRepository.Exist(Declination)) { Model.Local.CompositionArticleImageRepository CompositionArticleImageRepository = new Model.Local.CompositionArticleImageRepository(); if (!CompositionArticleImageRepository.ExistCompositionArticleImage(Declination, ArticleImage.ImaArt_Id)) { CompositionArticleImageRepository.Add(new Model.Local.CompositionArticleImage() { ComArt_Id = Declination, ImaArt_Id = ArticleImage.ImaArt_Id, }); } // réaffectation côté PrestaShop Model.Local.CompositionArticle CompositionArticle = CompositionArticleRepository.Read(Declination); if (CompositionArticle.Pre_Id != null && CompositionArticle.Pre_Id != 0) { Model.Prestashop.PsProductAttributeImageRepository PsProductAttributeImageRepository = new Model.Prestashop.PsProductAttributeImageRepository(); if (PsProductAttributeImageRepository.ExistProductAttributeImage((UInt32)CompositionArticle.Pre_Id, (UInt32)ArticleImage.Pre_Id) == false) { PsProductAttributeImageRepository.Add(new Model.Prestashop.PsProductAttributeImage() { IDImage = (UInt32)ArticleImage.Pre_Id, IDProductAttribute = (UInt32)CompositionArticle.Pre_Id, }); } } } } } result = true; logs.Add("II30- Remplacement de l'image " + ArticleImage.ImaArt_SourceFile + " en position " + ArticleImage.ImaArt_Position + " pour l'article " + ArticleImage.Article.Art_Ref); } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); logs.Add("II39- Erreur lors du remplacement de l'image " + ArticleImage.ImaArt_SourceFile + " en position " + ArticleImage.ImaArt_Position + " pour l'article " + ArticleImage.Article.Art_Ref); logs.Add(ex.ToString()); } } } } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); if (ex.ToString().Contains("System.UnauthorizedAccessException") && IdArticleImage != 0) { Model.Local.ArticleImageRepository ArticleImageRepository = new Model.Local.ArticleImageRepository(); ArticleImageRepository.Delete(ArticleImageRepository.ReadArticleImage(IdArticleImage)); } } return(result); }
public void Exec(Int32 ImageSend) { try { Model.Local.ArticleImageRepository ArticleImageRepository = new Model.Local.ArticleImageRepository(); if (ArticleImageRepository.ExistPre_Id(ImageSend) == false) { Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository(); Model.Prestashop.PsImage PsImage = PsImageRepository.ReadImage(Convert.ToUInt32(ImageSend)); Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository(); if (ArticleRepository.ExistPre_Id(Convert.ToInt32(PsImage.IDProduct))) { Model.Local.Article Article = ArticleRepository.ReadPre_Id(Convert.ToInt32(PsImage.IDProduct)); Model.Prestashop.PsImageLangRepository PsImageLangRepository = new Model.Prestashop.PsImageLangRepository(); if (PsImageLangRepository.ExistImageLang(Convert.ToUInt32(ImageSend), Core.Global.Lang)) { string extension = Core.Img.jpgExtension; String FTP = Core.Global.GetConfig().ConfigFTPIP; String User = Core.Global.GetConfig().ConfigFTPUser; String Password = Core.Global.GetConfig().ConfigFTPPassword; Model.Prestashop.PsImageLang PsImageLang = PsImageLangRepository.ReadImageLang(Convert.ToUInt32(ImageSend), Core.Global.Lang); Model.Local.ArticleImage ArticleImage = new Model.Local.ArticleImage() { ImaArt_Name = (!String.IsNullOrEmpty(PsImageLang.Legend)) ? PsImageLang.Legend : Article.Art_Ref, ImaArt_Position = PsImage.Position, ImaArt_Default = Convert.ToBoolean(PsImage.Cover), Pre_Id = Convert.ToInt32(PsImage.IDImage), Art_Id = Article.Art_Id, ImaArt_Image = string.Empty, ImaArt_SourceFile = SearchFreeNameFile(Article.Art_Id, Article.Art_Ref, extension), ImaArt_DateAdd = DateTime.Now }; ArticleImageRepository.Add(ArticleImage); Boolean import_img = false; try { // <JG> 10/04/2013 gestion système d'images string ftpPath = "/img/p/"; switch (Core.Global.GetConfig().ConfigImageStorageMode) { case Core.Parametres.ImageStorageMode.old_system: #region old_system // no action on path break; #endregion case Core.Parametres.ImageStorageMode.new_system: default: #region new_system foreach (char directory in PsImage.IDImage.ToString()) { ftpPath += directory + "/"; } break; #endregion } // <JG> 21/05/2013 import image originale Boolean import_img_tmp = false; try { string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + Core.Img.jpgExtension : FTP + ftpPath + PsImage.IDImage + Core.Img.jpgExtension; bool existfile = Core.Ftp.ExistFile(ftpfullpath, User, Password); if (existfile) { System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new System.Net.NetworkCredential(User, Password); ftp.UseBinary = true; ftp.UsePassive = true; ftp.KeepAlive = false; ftp.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse(); Stream reader = response.GetResponseStream(); MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[1024]; byte[] downloadedData = new byte[0]; while (true) { int bytesRead = reader.Read(buffer, 0, buffer.Length); if (bytesRead != 0) { memStream.Write(buffer, 0, bytesRead); } else { break; } downloadedData = memStream.ToArray(); } string target_folder = (Core.Global.GetConfig().ConfigLocalStorageMode == Parametres.LocalStorageMode.simple_system) ? Global.GetConfig().Folders.TempArticle : ArticleImage.advanced_folder; if (downloadedData != null && downloadedData.Length != 0) { FileStream newFile = new FileStream( System.IO.Path.Combine(target_folder, String.Format("{0}" + extension, ArticleImage.ImaArt_Id)), FileMode.Create); newFile.Write(downloadedData, 0, downloadedData.Length); newFile.Close(); newFile.Dispose(); memStream.Dispose(); downloadedData = new byte[0]; } string local_file_tmp = System.IO.Path.Combine(target_folder, String.Format("{0}" + extension, ArticleImage.ImaArt_Id)); // <JG> 30/09/2013 détection encodage PNG lors de l'import Boolean rename_to_png = false; System.Drawing.Image img = System.Drawing.Image.FromFile(local_file_tmp); var imgguid = img.RawFormat.Guid; img.Dispose(); System.Drawing.Imaging.ImageCodecInfo search; foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders()) { if (codec.FormatID == imgguid) { search = codec; if (search.FormatDescription == "PNG") { rename_to_png = true; } break; } } if (rename_to_png) { if (System.IO.File.Exists(local_file_tmp)) { extension = Core.Img.pngExtension; System.IO.File.Move(local_file_tmp, System.IO.Path.Combine(target_folder, String.Format("{0}" + extension, ArticleImage.ImaArt_Id))); } ArticleImage.ImaArt_SourceFile = SearchFreeNameFile(Article.Art_Id, Article.Art_Ref, extension); } ArticleImage.ImaArt_Image = String.Format("{0}" + extension, ArticleImage.ImaArt_Id); ArticleImageRepository.Save(); Core.Img.resizeImage(new System.Drawing.Bitmap(ArticleImage.TempFileName), Core.Global.GetConfig().ConfigImageMiniatureWidth, Core.Global.GetConfig().ConfigImageMiniatureHeight, ArticleImage.SmallFileName); import_img_tmp = true; } } catch (Exception) { // Not implemented } if (import_img_tmp) { Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository(); List <Model.Prestashop.PsImageType> ListPsImageType = PsImageTypeRepository.ListProduct(1); foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType) { string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + "-" + PsImageType.Name + Core.Img.jpgExtension : FTP + ftpPath + PsImage.IDImage + "-" + PsImageType.Name + Core.Img.jpgExtension; System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new System.Net.NetworkCredential(User, Password); ftp.UseBinary = true; ftp.UsePassive = true; ftp.KeepAlive = false; ftp.EnableSsl = Core.Global.GetConfig().ConfigFTPSSL; System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse(); Stream reader = response.GetResponseStream(); MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[1024]; byte[] downloadedData = new byte[0]; while (true) { int bytesRead = reader.Read(buffer, 0, buffer.Length); if (bytesRead != 0) { memStream.Write(buffer, 0, bytesRead); } else { break; } downloadedData = memStream.ToArray(); } if (downloadedData != null && downloadedData.Length != 0) { FileStream newFile = new FileStream(ArticleImage.FileName(PsImageType.Name), FileMode.Create); newFile.Write(downloadedData, 0, downloadedData.Length); newFile.Close(); } } import_img = true; // gestion image par défaut if (ArticleImage.ImaArt_Default) { List <Model.Local.ArticleImage> ListArticleImage = ArticleImageRepository.ListArticle(ArticleImage.Art_Id.Value); if (ListArticleImage.Count(i => i.ImaArt_Default == true && i.ImaArt_Id != ArticleImage.ImaArt_Id) > 0) { foreach (Model.Local.ArticleImage ArticleImageDefault in ListArticleImage.Where(i => i.ImaArt_Default == true && i.ImaArt_Id != ArticleImage.ImaArt_Id)) { ArticleImageDefault.ImaArt_Default = false; ArticleImageRepository.Save(); } } } // liens images déclinaisons ExecAttributeImage(PsImage, ArticleImage); } } catch (Exception ex) { Core.Error.SendMailError("[DOWNLOAD FTP IMAGE ARTICLE]<br />" + ex.ToString()); ArticleImageRepository.Delete(ArticleImage); } if (!import_img) { ArticleImageRepository.Delete(ArticleImage); } } } } else if (ArticleImageRepository.ExistPrestaShop(ImageSend)) { // import des affectations aux déclinaisons Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository(); Model.Prestashop.PsImage PsImage = PsImageRepository.ReadImage(Convert.ToUInt32(ImageSend)); Model.Local.ArticleImage ArticleImage = ArticleImageRepository.ReadPrestaShop(ImageSend); ExecAttributeImage(PsImage, ArticleImage); } } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } }
public void Exec(Model.Local.ArticleImage ArticleImage, Model.Local.Article Article, out List <string> log_out) { log_out = new List <string>(); try { // Suppression de l'affectation de l'image à la gamme en local Model.Local.AttributeArticleImageRepository AttributeArticleImageRepository = new Model.Local.AttributeArticleImageRepository(); AttributeArticleImageRepository.DeleteAll(AttributeArticleImageRepository.ListImageArticle(ArticleImage.ImaArt_Id)); // 07/04/2016 ajout suppression liens compositions Model.Local.CompositionArticleImageRepository CompositionArticleImageRepository = new Model.Local.CompositionArticleImageRepository(); CompositionArticleImageRepository.DeleteAll(CompositionArticleImageRepository.ListImageArticle(ArticleImage.ImaArt_Id)); // Suppression des fichiers images en local if (File.Exists(ArticleImage.SmallFileName)) { try { File.Delete(ArticleImage.SmallFileName); } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } } if (File.Exists(ArticleImage.TempFileName)) { try { File.Delete(ArticleImage.TempFileName); } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } } string folder = (Core.Global.GetConfig().ConfigLocalStorageMode == Core.Parametres.LocalStorageMode.advanced_system) ? ArticleImage.advanced_folder : Core.Global.GetConfig().Folders.RootArticle; foreach (var fileName in Directory.GetFiles(folder, String.Format("{0}-*" + ArticleImage.GetExtension, ArticleImage.ImaArt_Id))) { try { File.Delete(fileName); } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } } if (ArticleImage.Pre_Id != null && ArticleImage.Pre_Id > 0) { Model.Prestashop.PsProductAttributeImageRepository PsProductAttributeImageRepository = new Model.Prestashop.PsProductAttributeImageRepository(); PsProductAttributeImageRepository.DeleteAll(PsProductAttributeImageRepository.ListImage((uint)ArticleImage.Pre_Id)); Model.Prestashop.PsImageLangRepository PsImageLangRepository = new Model.Prestashop.PsImageLangRepository(); PsImageLangRepository.DeleteAll(PsImageLangRepository.ListImage((uint)ArticleImage.Pre_Id)); // Suppression de l'occurence de l'image sur prestashop Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository(); Model.Prestashop.PsImage PsImage = PsImageRepository.ReadImage((uint)ArticleImage.Pre_Id); if (PsImage != null) { PsImageRepository.Delete(PsImage); try { if (PsImage.Cover == 1 && !PsImageRepository.ExistProductCover(PsImage.IDProduct, 1) && PsImageRepository.ExistProduct(PsImage.IDProduct)) { List <Model.Prestashop.PsImage> Others = PsImageRepository.ListProduct(PsImage.IDProduct); Model.Prestashop.PsImage NewCover = Others.OrderBy(i => i.Position).FirstOrDefault(); if (NewCover != null) { NewCover.Cover = 1; PsImageRepository.Save(); Core.Transfert.TransfertArticleImage syncimage = new Core.Transfert.TransfertArticleImage(); syncimage.ExecCover(PsImage.IDProduct, true, PsImageRepository, NewCover); Model.Prestashop.PsImageShopRepository PsImageShopRepository = new Model.Prestashop.PsImageShopRepository(); foreach (Model.Prestashop.PsImageShop PsImageShop in PsImageShopRepository.List(NewCover.IDImage)) { PsImageShop.Cover = 1; } PsImageShopRepository.Save(); } } } catch { log_out.Add("II47- Erreur durant la réaffectation de l'image de couverture dans PrestaShop pour l'article " + Article.Art_Ref); } try { foreach (Model.Prestashop.PsImage Image in PsImageRepository.ListProduct(PsImage.IDProduct).Where(i => i.Position > PsImage.Position)) { Image.Position--; } PsImageRepository.Save(); } catch { log_out.Add("II48- Erreur durant le recalcul des positions dans PrestaShop pour les images de l'article " + Article.Art_Ref); } // Suppression des fichiers images sur prestashop if (Core.Global.GetConfig().ConfigFTPActive) { String FTP = Core.Global.GetConfig().ConfigFTPIP; String User = Core.Global.GetConfig().ConfigFTPUser; String Password = Core.Global.GetConfig().ConfigFTPPassword; // <JG> 10/04/2013 gestion système d'images string ftpPath = "/img/p/"; switch (Core.Global.GetConfig().ConfigImageStorageMode) { case Core.Parametres.ImageStorageMode.old_system: #region old_system // no action on path break; #endregion case Core.Parametres.ImageStorageMode.new_system: default: #region new_system foreach (char directory in ArticleImage.Pre_Id.ToString()) { ftpPath += directory + "/"; } break; #endregion } // <JG> 21/05/2013 correct for 1.5 storage mode string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + Article.Pre_Id + "-" + ArticleImage.Pre_Id + ".jpg" : FTP + ftpPath + ArticleImage.Pre_Id + ".jpg"; FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); request.Credentials = new System.Net.NetworkCredential(User, Password); request.Method = WebRequestMethods.Ftp.DeleteFile; request.UseBinary = true; request.UsePassive = true; request.KeepAlive = false; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository(); List <Model.Prestashop.PsImageType> ListPsImageType = PsImageTypeRepository.ListProduct(1); foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType) { try { // <JG> 21/05/2013 correct for 1.5 storage mode ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system) ? FTP + ftpPath + Article.Pre_Id + "-" + ArticleImage.Pre_Id + "-" + PsImageType.Name + ".jpg" : FTP + ftpPath + ArticleImage.Pre_Id + "-" + PsImageType.Name + ".jpg"; request = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath); request.Credentials = new System.Net.NetworkCredential(User, Password); request.Method = WebRequestMethods.Ftp.DeleteFile; request.UseBinary = true; request.UsePassive = true; request.KeepAlive = false; response = (FtpWebResponse)request.GetResponse(); response.Close(); } catch {} } } } } // Suppression de l'occurence de l'image en local Model.Local.ArticleImageRepository ArticleImageRepository = new Model.Local.ArticleImageRepository(); Model.Local.ArticleImage articleImage = ArticleImageRepository.ReadArticleImage(ArticleImage.ImaArt_Id); if (articleImage != null) { ArticleImageRepository.Delete(articleImage); try { if (articleImage.ImaArt_Default && !ArticleImageRepository.ExistArticleDefault(Article.Art_Id, true) && ArticleImageRepository.ExistArticle(Article.Art_Id)) { Model.Local.ArticleImage NewCover = ArticleImageRepository.ListArticle(Article.Art_Id).OrderBy(i => i.ImaArt_Position).FirstOrDefault(); if (NewCover != null) { NewCover.ImaArt_Default = true; ArticleImageRepository.Save(); } } } catch { log_out.Add("II43- Erreur durant la réaffectation de l'image de couverture dans PrestaConnect pour l'article " + Article.Art_Ref); } try { foreach (Model.Local.ArticleImage Image in ArticleImageRepository.ListArticle(Article.Art_Id).Where(i => i.ImaArt_Position > articleImage.ImaArt_Position)) { Image.ImaArt_Position--; } ArticleImageRepository.Save(); } catch { log_out.Add("II44- Erreur durant le recalcul des positions dans PrestaConnect pour les images de l'article " + Article.Art_Ref); } } log_out.Add("II40- Suppression de l'image " + ArticleImage.ImaArt_SourceFile + " en position " + ArticleImage.ImaArt_Position + " pour l'article " + Article.Art_Ref); } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } }