public void Exec(Model.Prestashop.PsProductAttachment PsProductAttachment)
        {
            try
            {
                //<YH> 21/08/2012
                string DirAttachment = Global.GetConfig().Folders.RootAttachment;

                Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository();
                if (ArticleRepository.ExistPre_Id(Convert.ToInt32(PsProductAttachment.IDProduct)))
                {
                    Model.Local.Article Article = ArticleRepository.ReadPre_Id(Convert.ToInt32(PsProductAttachment.IDProduct));
                    Model.Local.AttachmentRepository AttachmentRepository = new Model.Local.AttachmentRepository();
                    if (AttachmentRepository.ExistPre_IdArt_Id(Convert.ToInt32(PsProductAttachment.IDAttachment), Article.Art_Id) == false)
                    {
                        Model.Prestashop.PsAttachmentRepository     PsAttachmentRepository     = new Model.Prestashop.PsAttachmentRepository();
                        Model.Prestashop.PsAttachment               PsAttachment               = PsAttachmentRepository.ReadAttachment(PsProductAttachment.IDAttachment);
                        Model.Prestashop.PsAttachmentLangRepository PsAttachmentLangRepository = new Model.Prestashop.PsAttachmentLangRepository();
                        if (PsAttachmentLangRepository.ExistAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang))
                        {
                            Model.Prestashop.PsAttachmentLang PsAttachmentLang = PsAttachmentLangRepository.ReadAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang);
                            Model.Local.Attachment            Attachment       = new Model.Local.Attachment()
                            {
                                Att_FileName    = PsAttachment.FileName,
                                Att_Description = PsAttachmentLang.Description,
                                Att_Mime        = PsAttachment.Mime,
                                Att_Name        = PsAttachmentLang.Name,
                                Att_File        = this.ReadFile(DirAttachment, PsAttachment.File),
                                Pre_Id          = Convert.ToInt32(PsAttachment.IDAttachment),
                                Art_Id          = Article.Art_Id
                            };
                            AttachmentRepository.Add(Attachment);

                            String FTP      = Core.Global.GetConfig().ConfigFTPIP;
                            String User     = Core.Global.GetConfig().ConfigFTPUser;
                            String Password = Core.Global.GetConfig().ConfigFTPPassword;

                            // <JG> 21/05/2013 correction recherche fichier sur le ftp
                            string ftpfullpath           = FTP + "/download/" + PsAttachment.File;
                            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(DirAttachment + Attachment.Att_File, FileMode.Create);
                                newFile.Write(downloadedData, 0, downloadedData.Length);
                                newFile.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }
Esempio n. 2
0
        //<YH> 23/08/2012
        private void ExecLocalToDistant(Model.Local.Article Article)
        {
            try
            {
                Model.Prestashop.PsAttachmentRepository PsAttachmentRepository = new Model.Prestashop.PsAttachmentRepository();
                Model.Prestashop.PsAttachment           PsAttachment;

                Model.Prestashop.PsAttachmentLangRepository PsAttachmentLangRepository = new Model.Prestashop.PsAttachmentLangRepository();
                Model.Prestashop.PsAttachmentLang           PsAttachmentLang;

                Model.Prestashop.PsProductAttachmentRepository PsProductAttachmentRepository = new Model.Prestashop.PsProductAttachmentRepository();
                Model.Prestashop.PsProductAttachment           PsProductAttachment;

                Boolean isAttachmentLang = false;

                String FTP      = Core.Global.GetConfig().ConfigFTPIP;
                String User     = Core.Global.GetConfig().ConfigFTPUser;
                String Password = Core.Global.GetConfig().ConfigFTPPassword;

                Model.Local.AttachmentRepository AttachmentRepository = new Model.Local.AttachmentRepository();
                List <Model.Local.Attachment>    ListAttachment       = AttachmentRepository.ListArticle(Article.Art_Id);
                foreach (Model.Local.Attachment Attachment in ListAttachment)
                {
                    try
                    {
                        if (Attachment.Pre_Id == null)
                        {
                            String PathAttachment = Path.Combine(Global.GetConfig().Folders.RootAttachment, Attachment.Att_File);
                            if (System.IO.File.Exists(PathAttachment))
                            {
                                #region infos document joint
                                PsAttachment          = new Model.Prestashop.PsAttachment();
                                PsAttachment.File     = Attachment.Att_File;
                                PsAttachment.FileName = Attachment.Att_FileName;
                                PsAttachment.Mime     = Attachment.Att_Mime;
                                PsAttachmentRepository.Add(PsAttachment);

                                isAttachmentLang = false;
                                PsAttachmentLang = new Model.Prestashop.PsAttachmentLang();
                                if (PsAttachmentLangRepository.ExistAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang))
                                {
                                    PsAttachmentLang = PsAttachmentLangRepository.ReadAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang);
                                    isAttachmentLang = true;
                                }
                                PsAttachmentLang.Name        = Attachment.Att_Name;
                                PsAttachmentLang.Description = Attachment.Att_Description;
                                if (isAttachmentLang == true)
                                {
                                    PsAttachmentLangRepository.Save();
                                }
                                else
                                {
                                    PsAttachmentLang.IDAttachment = PsAttachment.IDAttachment;
                                    PsAttachmentLang.IDLang       = Core.Global.Lang;
                                    PsAttachmentLangRepository.Add(PsAttachmentLang);
                                }
                                #endregion

                                // <JG> 24/05/2013 ajout insertion autres langues actives si non renseignées
                                #region Multi-langues
                                try
                                {
                                    Model.Prestashop.PsLangRepository PsLangRepository = new Model.Prestashop.PsLangRepository();
                                    foreach (Model.Prestashop.PsLang PsLang in PsLangRepository.ListActive(1, Global.CurrentShop.IDShop))
                                    {
                                        if (!PsAttachmentLangRepository.ExistAttachmentLang(PsAttachment.IDAttachment, PsLang.IDLang))
                                        {
                                            PsAttachmentLang = new Model.Prestashop.PsAttachmentLang()
                                            {
                                                IDAttachment = PsAttachment.IDAttachment,
                                                IDLang       = PsLang.IDLang,
                                                Name         = Attachment.Att_Name,
                                                Description  = Attachment.Att_Description
                                            };
                                            PsAttachmentLangRepository.Add(PsAttachmentLang);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Core.Error.SendMailError(ex.ToString());
                                }
                                #endregion

                                // affectation au produit
                                PsProductAttachment = new Model.Prestashop.PsProductAttachment();
                                if (PsProductAttachmentRepository.ExistProductAttachment(Convert.ToUInt32(Article.Pre_Id), PsAttachment.IDAttachment) == false)
                                {
                                    PsProductAttachment.IDProduct    = Convert.ToUInt32(Article.Pre_Id);
                                    PsProductAttachment.IDAttachment = PsAttachment.IDAttachment;
                                    PsProductAttachmentRepository.Add(PsProductAttachment);
                                }

                                try
                                {
                                    #region upload

                                    string ftpfullpath           = FTP + "/download/" + Attachment.Att_File;
                                    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.Method    = System.Net.WebRequestMethods.Ftp.UploadFile;
                                    System.IO.FileStream fs = System.IO.File.OpenRead(PathAttachment);
                                    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

                                    #region update Product field cache_as_attachements

                                    Model.Prestashop.PsProductRepository PsProductRepository = new Model.Prestashop.PsProductRepository();
                                    Model.Prestashop.PsProduct           PsProduct           = PsProductRepository.ReadId(PsProductAttachment.IDProduct);
                                    if (PsProduct.CacheHasAttachments == 0)
                                    {
                                        PsProduct.CacheHasAttachments = (sbyte)1;
                                        PsProductRepository.Save();
                                    }

                                    #endregion

                                    Attachment.Pre_Id = Convert.ToInt32(PsAttachment.IDAttachment);
                                    AttachmentRepository.Save();
                                }
                                catch (Exception ex)
                                {
                                    Core.Error.SendMailError("[UPLOAD FTP DOCUMENT]<br />" + ex.ToString());
                                    PsProductAttachmentRepository.Delete(PsProductAttachmentRepository.ListAttachment(PsAttachment.IDAttachment));
                                    PsAttachmentLangRepository.Delete(PsAttachmentLangRepository.ListAttachment(PsAttachment.IDAttachment));
                                    PsAttachmentRepository.Delete(PsAttachment);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Core.Error.SendMailError("[SYNCHRO DOCUMENT ARTICLE]<br />" + ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }
Esempio n. 3
0
        public void Sync(int IdArticle)
        {
            this.Semaphore.WaitOne();

            try
            {
                Model.Sage.F_ARTICLEMEDIARepository F_ARTICLEMEDIARepository = new Model.Sage.F_ARTICLEMEDIARepository();
                Model.Local.ArticleRepository       ArticleRepository        = new Model.Local.ArticleRepository();
                Model.Local.Article Article = new Model.Local.Article();
                if (ArticleRepository.ExistArticle(IdArticle))
                {
                    Article = ArticleRepository.ReadArticle(IdArticle);

                    // <JG> 24/03/2015 ajout option suppression auto
                    if (Core.Global.GetConfig().ImportMediaAutoDeleteAttachment)
                    {
                        Model.Local.AttachmentRepository AttachmentRepository = new Model.Local.AttachmentRepository();
                        if (AttachmentRepository.ExistArticle(IdArticle))
                        {
                            List <Model.Local.Attachment> ListArticle = AttachmentRepository.ListArticle(IdArticle);
                            ListArticle = ListArticle.Where(at => at.Sag_Id != null).ToList();

                            foreach (Model.Local.Attachment Attachment in ListArticle)
                            {
                                if (!F_ARTICLEMEDIARepository.Exist(Attachment.Sag_Id.Value))
                                {
                                    if (System.IO.File.Exists(System.IO.Path.Combine(Core.Global.GetConfig().Folders.RootAttachment, Attachment.Att_File)))
                                    {
                                        File.Delete(System.IO.Path.Combine(Core.Global.GetConfig().Folders.RootAttachment, Attachment.Att_File));
                                    }

                                    if (Attachment.Pre_Id != null && Attachment.Pre_Id > 0)
                                    {
                                        // Suppression de l'occurence du document sur prestashop
                                        Model.Prestashop.PsAttachmentRepository        psAttachmentRepository        = new Model.Prestashop.PsAttachmentRepository();
                                        Model.Prestashop.PsAttachmentLangRepository    psAttachmentLangRepository    = new Model.Prestashop.PsAttachmentLangRepository();
                                        Model.Prestashop.PsProductAttachmentRepository psProductAttachmentRepository = new Model.Prestashop.PsProductAttachmentRepository();

                                        Model.Prestashop.PsAttachment psAttachment = psAttachmentRepository.ReadAttachment(Convert.ToUInt32(Attachment.Pre_Id.Value));

                                        string distant_file = string.Empty;
                                        if (psAttachment != null)
                                        {
                                            distant_file = psAttachment.File;
                                            psProductAttachmentRepository.Delete(psProductAttachmentRepository.ListAttachment(psAttachment.IDAttachment));
                                            psAttachmentLangRepository.Delete(psAttachmentLangRepository.ListAttachment(psAttachment.IDAttachment));
                                            psAttachmentRepository.Delete(psAttachment);
                                        }

                                        if (Core.Global.GetConfig().ConfigFTPActive)
                                        {
                                            String FTP      = Core.Global.GetConfig().ConfigFTPIP;
                                            String User     = Core.Global.GetConfig().ConfigFTPUser;
                                            String Password = Core.Global.GetConfig().ConfigFTPPassword;

                                            string ftpfullpath = FTP + "/download/" + distant_file;

                                            if (Core.Ftp.ExistFile(ftpfullpath, User, Password))
                                            {
                                                try
                                                {
                                                    System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath);
                                                    request.Credentials = new System.Net.NetworkCredential(User, Password);
                                                    request.Method      = System.Net.WebRequestMethods.Ftp.DeleteFile;
                                                    request.UseBinary   = true;
                                                    request.UsePassive  = true;
                                                    request.KeepAlive   = false;

                                                    System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();
                                                    response.Close();
                                                }
                                                catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); }
                                            }
                                        }
                                    }

                                    AttachmentRepository.Delete(Attachment);
                                }
                            }
                        }
                    }

                    if (F_ARTICLEMEDIARepository.ExistReference(Article.Art_Ref))
                    {
                        foreach (Model.Sage.F_ARTICLEMEDIA F_ARTICLEMEDIA in F_ARTICLEMEDIARepository.ListReference(Article.Art_Ref))
                        {
                            String File = (System.IO.File.Exists(F_ARTICLEMEDIA.ME_Fichier))
                                        ? F_ARTICLEMEDIA.ME_Fichier
                                        : Path.Combine(DirDoc, F_ARTICLEMEDIA.ME_Fichier.Substring(2));
                            if (System.IO.File.Exists(File))
                            {
                                string extension = Path.GetExtension(File).ToLower();
                                string filename  = Path.GetFileNameWithoutExtension(File);
                                Model.Local.MediaAssignmentRuleRepository MediaAssignmentRuleRepository = new Model.Local.MediaAssignmentRuleRepository();
                                List <Model.Local.MediaAssignmentRule>    list = MediaAssignmentRuleRepository.List();
                                if (list.Count(r => filename.EndsWith(r.SuffixText)) > 0)
                                {
                                    foreach (Model.Local.MediaAssignmentRule mediarule in list.Where(r => filename.EndsWith(r.SuffixText)))
                                    {
                                        if (filename.EndsWith(mediarule.SuffixText))
                                        {
                                            switch (mediarule.Rule)
                                            {
                                            case (short)Core.Parametres.MediaRule.AsAttachment:
                                                Core.ImportSage.ImportArticleDocument Sync = new Core.ImportSage.ImportArticleDocument();
                                                Sync.Exec(File, Article.Art_Id, (!string.IsNullOrEmpty(mediarule.AssignName) ? mediarule.AssignName : F_ARTICLEMEDIA.ME_Commentaire), null, F_ARTICLEMEDIA.cbMarq);
                                                break;

                                            case (short)Core.Parametres.MediaRule.AsPicture:
                                                if (Core.Img.imageExtensions.Contains(extension))
                                                {
                                                    int position, AttributeArticle;
                                                    Core.Global.SearchReference(filename, out position, out AttributeArticle);
                                                    Core.ImportSage.ImportArticleImage ImportImage = new Core.ImportSage.ImportArticleImage();
                                                    ImportImage.Exec(File, Article.Art_Id, position, AttributeArticle);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (Core.Img.imageExtensions.Contains(extension))
                                    {
                                        if (Core.Global.GetConfig().ImportMediaIncludePictures)
                                        {
                                            int position, AttributeArticle;
                                            Core.Global.SearchReference(filename, out position, out AttributeArticle);
                                            Core.ImportSage.ImportArticleImage ImportImage = new Core.ImportSage.ImportArticleImage();
                                            ImportImage.Exec(File, Article.Art_Id, position, AttributeArticle);
                                        }
                                    }
                                    else
                                    {
                                        Core.ImportSage.ImportArticleDocument Sync = new Core.ImportSage.ImportArticleDocument();
                                        Sync.Exec(File, Article.Art_Id, F_ARTICLEMEDIA.ME_Commentaire, null, F_ARTICLEMEDIA.cbMarq);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError("[IM] " + ex.ToString());
            }
            lock (this)
            {
                this.CurrentCount += 1;
            }
            this.ReportProgress(this.CurrentCount * 100 / this.ListCount);
            this.Semaphore.Release();
        }