Exemplo n.º 1
0
        private void DeleteImageThumb(byte[] data)
        {
            MqThumbImageInfo thumbImageInfo = new MqThumbImageInfo();

            try
            {
                thumbImageInfo = MqThumbImageInfo.GetDataFromMessage(data);
            }
            catch (Exception ex)
            {
                Log.Error("ERROR convert byte -> MQThumbImageInfo.", ex);
            }
            string directory = string.Empty;

            try
            {
                //SP thường
                if (thumbImageInfo.TypeProduct == 1)
                {
                    directory = _folderImage + thumbImageInfo.FolderImage;
                }
                //SP gốc
                else if (thumbImageInfo.TypeProduct == 2)
                {
                    directory = _folderImageSPGoc + thumbImageInfo.FolderImage;
                }
                DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(directory);
                FileInfo[]    filesInDir = hdDirectoryInWhichToSearch.GetFiles(thumbImageInfo.ImageName + "*.*");
                //if (filesInDir.Count() > 0)
                //{
                //    Log.Info(string.Format("Delete {0} file with directory {1} filename {2}", filesInDir.Count(), directory, thumbImageInfo.ImageName));
                //}
                foreach (FileInfo foundFile in filesInDir)
                {
                    File.Delete(foundFile.FullName);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Could not find a part of the path") || ex.Message.Contains("The device is not ready"))
                {
                }
                else
                {
                    Log.Error(string.Format("Error delete file with path {0}", directory), ex);
                }
            }
        }
Exemplo n.º 2
0
        private void DownloadImageSPGoc(long companyId, bool reloadall = false)
        {
            rabbitMQServer = RabbitMQManager.GetRabbitMQServer(rabbitMQServerName);
            //Jobclient update solr and redis
            var updateProductJobClient = new JobClient(updateProductExchangeGroupName, GroupType.Topic, updateProductqueueJobName, true, rabbitMQServer);
            //JobClient download image fails
            //JobClient downloadImageProductJobClient = new JobClient(updateProductImageGroupName, GroupType.Topic, updateProductImageProductJobName, true, rabbitMQServer);
            //JobClient delete thumb
            var deleteThumbJobClient = new JobClient(updateProductImageGroupName, GroupType.Topic, deleteThumbImageProductJobName, true, rabbitMQServer);

            WSS.DownloadImageByHand.DBTableAdapters.ProductTableAdapter productAdapter = new WSS.DownloadImageByHand.DBTableAdapters.ProductTableAdapter();
            productAdapter.Connection.ConnectionString = connectionString;
            WSS.DownloadImageByHand.DB.ProductDataTable productTable = new WSS.DownloadImageByHand.DB.ProductDataTable();
            int demgetproduct = 0;

            while (true)
            {
                try
                {
                    if (reloadall)
                    {
                        productAdapter.FillBy_AllSPGoc(productTable, companyId);
                    }
                    else
                    {
                        //Lấy danh sách sản phẩm valid = 1
                        productAdapter.FillBy_SPGocAndValid(productTable, companyId);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    demgetproduct++;
                    if (demgetproduct == 1)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error 1", companyId), ex);
                    }
                    else if (demgetproduct == numbererror)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error {1}", companyId, numbererror), ex);
                        break;
                    }
                    Thread.Sleep(60000);
                }
            }
            try
            {
                if (productTable.Rows.Count > 0)
                {
                    this.Invoke(new Action(() =>
                    {
                        rbsuccess.AppendText(companyId + ".Get " + productTable.Rows.Count + " Product from SQL..." + System.Environment.NewLine);
                    }));
                    string path         = pathImageSPGOC;
                    int    countsuccess = 0;
                    int    countproduct = productTable.Rows.Count;
                    for (int i = 0; i < productTable.Rows.Count; i++)
                    {
                        long   productId   = Common.Obj2Int64(productTable.Rows[i]["ID"].ToString());
                        string nameProduct = productTable.Rows[i]["Name"].ToString();
                        string imageUrl    = productTable.Rows[i]["ImageUrls"].ToString();
                        if (string.IsNullOrEmpty(nameProduct) || string.IsNullOrEmpty(imageUrl))
                        {
                            Log.Error(string.Format("CompanyID = {0} ProductID = {1} Name or ImageUrl is null or empty.", companyId, productId));
                            continue;
                        }
                        string filename = Common.UnicodeToKoDauAndGach(nameProduct);
                        string tempf    = filename.Replace("-", "");
                        if (tempf.Length < 3)
                        {
                            continue;
                        }
                        string folder = tempf.Substring(0, 3);
                        if (folder == "bin")
                        {
                            folder = "bin1";
                        }
                        if (folder == "con")
                        {
                            folder = "con1";
                        }
                        if (folder == "aux")
                        {
                            folder = "aux1";
                        }
                        if (folder == "prn")
                        {
                            folder = "prn1";
                        }
                        if (folder == "nul")
                        {
                            folder = "nul1";
                        }
                        if (filename.Length > 100)
                        {
                            filename = filename.Substring(0, 99);
                        }
                        bool fileSaved = true;
                        try
                        {
                            //fileSaved = Common.SaveFileDownloadImage(imageUrl, path + folder, filename + ".jpg", productId, companyId);
                        }
                        catch (Exception)
                        {
                        }
                        if (fileSaved)
                        {
                            int    w = 0, h = 0;
                            string pathsave = "Store/images/" + folder + "/" + filename + ".jpg";
                            while (checkstop)
                            {
                                try
                                {
                                    productAdapter.UpdateQuery(pathsave, w, h, productId);
                                    Log.InfoFormat("{0}/{1}. CompanyID = {2} ProductID = {3} Download Image success.", i, countproduct, companyId, productId);
                                    countsuccess++;
                                    #region Send Message Update solr and redis
                                    Job job = new Job();
                                    job.Data = BitConverter.GetBytes(productId);
                                    job.Type = 2;
                                    updateProductJobClient.PublishJob(job, updateProductJobExpirationMS);
                                    //Log.InfoFormat("Send message To RabbitMq {0} with ID = {1}", rabbitMQServerName, productId);
                                    #endregion

                                    #region Xóa ảnh Thumb nếu có
                                    //push message lên service xóa ảnh thumb
                                    Job deletejob          = new Job();
                                    MqThumbImageInfo thumb = new MqThumbImageInfo();
                                    thumb.ProductId   = productId;
                                    thumb.FolderImage = folder;
                                    thumb.ImageName   = filename;
                                    thumb.TypeProduct = 2;
                                    deletejob.Data    = MqThumbImageInfo.GetMess(thumb);
                                    deleteThumbJobClient.PublishJob(deletejob);
                                    #endregion
                                    this.Invoke(new Action(() =>
                                    {
                                        rbsuccess.AppendText(string.Format("{0}...{1}: {2}.{3} sucess.", 1, companyId, i, productId) + System.Environment.NewLine);
                                    }));
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    this.Invoke(new Action(() =>
                                    {
                                        rbfail.AppendText(i + "." + productId + ": Update Solr + Push service Delete Thumb fails." + ex.ToString() + System.Environment.NewLine);
                                    }));
                                    break;
                                    //Thread.Sleep(10000);
                                    //dem++;
                                    //if (dem == 1)
                                    //    Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error 1", companyId, productId), ex);
                                    //else if (dem == numbererror)
                                    //{
                                    //    Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error {2}", companyId, productId, numbererror), ex);
                                    //    break;
                                    //}
                                }
                            }
                        }
                        else
                        {
                            this.Invoke(new Action(() =>
                            {
                                //lbFails.Text = countfail.ToString();
                                rbsuccess.AppendText(string.Format("{0}...{1}: {2}.{3} fails.", 1, companyId, i, productId) + System.Environment.NewLine);
                                rbfail.AppendText(i + "." + productId + ": Download fails." + imageUrl + " ...Name: " + nameProduct + System.Environment.NewLine);
                            }));
                        }
                    }
                    Log.InfoFormat("CompanyID = {0} Download Image Success {1}/{2}", companyId, countsuccess, productTable.Rows.Count);
                }
                else
                {
                    Log.InfoFormat("CompanyID = {0} 0 product (valid = 1 and ImagePath is Null or empty)", companyId);
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("CompanyID = {0} ERROR~~~", companyId), ex);
            }
        }
Exemplo n.º 3
0
        private void DownloadImageProduct(MqChangeImage mqProduct, JobClient jobclientUpdateSolr, JobClient jobclientDownloadImageProduct, JobClient jobclientDeleteThumb)
        {
            WSS.DownloadImageProductService.DBTableAdapters.ProductTableAdapter productAdapter = new WSS.DownloadImageProductService.DBTableAdapters.ProductTableAdapter();
            WSS.DownloadImageProductService.DBTableAdapters.History_DownloadImageProductTableAdapter historyDownloadAdapter = new DBTableAdapters.History_DownloadImageProductTableAdapter();
            WSS.DownloadImageProductService.DB.ProductDataTable productTable = new WSS.DownloadImageProductService.DB.ProductDataTable();
            productAdapter.Connection.ConnectionString         = connectionString;
            historyDownloadAdapter.Connection.ConnectionString = connectionString;
            try
            {
                productAdapter.FillBy_ID(productTable, mqProduct.ProductId);
                if (productTable.Rows.Count > 0)
                {
                    String path     = pathImage;
                    string filename = Common.UnicodeToKoDauAndGach(productTable.Rows[0]["Name"].ToString());
                    if (filename.Length > 100)
                    {
                        filename = filename.Substring(0, 99);
                    }
                    filename += "_" + mqProduct.ProductId;

                    var url      = productTable.Rows[0]["DetailUrl"].ToString();
                    var folder   = Common.GetFolderSaveImageProduct(filename, url);
                    var imageurl = productTable.Rows[0]["ImageUrls"].ToString();
                    if (string.IsNullOrEmpty(imageurl))
                    {
                        historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "DetailUrl: " + url + " Null or Empty!", false);
                        Log.Error(string.Format("ProductID = {0} ImageUrl is null or empty.", mqProduct.ProductId));
                        return;
                    }
                    bool fileSaved = Common.SaveFileDownloadImage(imageurl, path + folder, filename + ".jpg", mqProduct.ProductId, 0);
                    if (fileSaved)
                    {
                        string pathsave = Common.GetImagePath(folder, filename);
                        int    dem      = 1;
                        while (checkstop)
                        {
                            try
                            {
                                productAdapter.UpdateImagePathAndValid(pathsave, 0, 0, true, mqProduct.ProductId);
                                bool isnews = false;
                                isnews = Common.Obj2Bool(productTable.Rows[0]["IsNews"].ToString());

                                Log.InfoFormat("ProductID = {0} Download Image success.", mqProduct.ProductId); #region Send Message Update solr and redis
                                Job job  = new Job();
                                job.Data = BitConverter.GetBytes(mqProduct.ProductId);
                                job.Type = 2;
                                jobclientUpdateSolr.PublishJob(job, updateProductJobExpirationMS);

                                #endregion
                                #region Send message services delete image thumb
                                Job deletejob          = new Job();
                                MqThumbImageInfo thumb = new MqThumbImageInfo
                                {
                                    ProductId   = mqProduct.ProductId,
                                    FolderImage = folder,
                                    ImageName   = filename,
                                    TypeProduct = 1
                                };
                                deletejob.Data = MqThumbImageInfo.GetMess(thumb);
                                jobclientDeleteThumb.PublishJob(deletejob);
                                #endregion
                                #region Log Finished
                                historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, true, "", isnews);
                                #endregion
                                break;
                            }
                            catch (Exception ex)
                            {
                                Thread.Sleep(10000);
                                if (dem == 1)
                                {
                                    Log.Error(string.Format("ProductID = {0}, Update ImagePath Error lan 1", mqProduct.ProductId), ex);
                                }
                                else if (dem == numbererror)
                                {
                                    #region Log error in here
                                    historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "UpdateImagePath OR Update Solr or Redis OR Send Message Delete Thumb Error: " + ex.Message, false);
                                    #endregion
                                    Log.Error(string.Format("ProductID = {0}, Update ImagePath Error lan {1}", mqProduct.ProductId, numbererror), ex);
                                    break;
                                }
                                else
                                {
                                    dem++;
                                }
                            }
                        }
                    }
                    else
                    {
                        historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, imageurl + " . Download Fails.", false);
                        //CheckErrorDownloadImage(mqProduct, jobclientDownloadImageProduct);
                    }
                }
                else
                {
                    historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Product not in Database.", false);
                    Log.ErrorFormat("ProductID = {0} Khong ton tai trong Database.", mqProduct.ProductId);
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("ProductID = {0} Get Info Product in SQL Error.", mqProduct.ProductId), ex);
                historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Error: " + ex.Message, false);
                //CheckErrorDownloadImage(mqProduct, jobclientDownloadImageProduct);
            }
        }
Exemplo n.º 4
0
        private void DownloadImageProductSpGoc(MqChangeImage mqProduct, JobClient updateProductJobClient, JobClient downloadImageProductJobClient, JobClient deleteThumbJobClient)
        {
            string path                   = pathImageSPGOC;
            var    productAdapter         = new DBTableAdapters.ProductTableAdapter();
            var    historyDownloadAdapter = new DBTableAdapters.History_DownloadImageProductTableAdapter();
            var    productTable           = new DB.ProductDataTable();

            productAdapter.Connection.ConnectionString         = connectionString;
            historyDownloadAdapter.Connection.ConnectionString = connectionString;
            //bien dem lay du lieu loi tu sql = 3 thì dừng
            int dem = 0;

            //lấy dữ liệu từ sql
            while (checkstop)
            {
                try
                {
                    productAdapter.FillBy_ID(productTable, mqProduct.ProductId);
                    break;
                }
                catch (Exception ex)
                {
                    if (dem == 3)
                    {
                        historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Get product from Database error." + ex, false);
                        Log.ErrorFormat("SP Gốc : ProductID = {0} Get product from Database error. \r\n {1}", mqProduct.ProductId, ex);
                        return;
                    }
                    dem++;
                    Thread.Sleep(30000);
                }
            }
            if (productTable.Rows.Count > 0)
            {
                string nameProduct = productTable.Rows[0]["Name"].ToString();
                string imageUrl    = productTable.Rows[0]["ImageUrls"].ToString();
                if (string.IsNullOrEmpty(nameProduct) || string.IsNullOrEmpty(imageUrl))
                {
                    Log.Error(string.Format("SP Gốc : ProductID = {0} Name or ImageUrl is null or empty.", mqProduct.ProductId));
                    return;
                }
                else
                {
                    string filename = Common.UnicodeToKoDauAndGach(nameProduct);
                    if (filename.Length > 100)
                    {
                        filename = filename.Substring(0, 99);
                    }
                    string folder    = Common.GetFolderSaveImageRootProduct(filename);
                    bool   fileSaved = Common.SaveFileDownloadImage(imageUrl, path + folder, filename + ".jpg", mqProduct.ProductId, 6619858476258121218);
                    if (fileSaved)
                    {
                        string pathsave  = Common.GetImagePath(folder, filename);
                        int    demupdate = 0;
                        while (checkstop)
                        {
                            try
                            {
                                productAdapter.UpdateImagePathAndValid(pathsave, 0, 0, true, mqProduct.ProductId);

                                #region Send Message Update solr and redis
                                Job job = new Job();
                                job.Data = BitConverter.GetBytes(mqProduct.ProductId);
                                job.Type = 2;
                                updateProductJobClient.PublishJob(job, updateProductJobExpirationMS);
                                //Log.InfoFormat("Send message To RabbitMq {0} with ID = {1}", rabbitMQServerName, productId);
                                #endregion

                                #region Xóa ảnh Thumb nếu có
                                //push message lên service xóa ảnh thumb
                                Job deletejob = new Job();
                                var thumb     = new MqThumbImageInfo
                                {
                                    ProductId   = mqProduct.ProductId,
                                    FolderImage = folder,
                                    ImageName   = filename,
                                    TypeProduct = 2
                                };
                                deletejob.Data = MqThumbImageInfo.GetMess(thumb);
                                deleteThumbJobClient.PublishJob(deletejob);
                                #endregion
                                #region Log Finished
                                historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, true, "SP Gốc", false);
                                #endregion
                                break;
                            }
                            catch (Exception ex)
                            {
                                Thread.Sleep(10000);
                                demupdate++;
                                if (demupdate == 1)
                                {
                                    Log.Error(string.Format("SP Gốc : CompanyID = {0} Product = {1}, Update ImagePath Error 1", 6619858476258121218, mqProduct.ProductId), ex);
                                }
                                else if (demupdate == numbererror)
                                {
                                    historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Update sql fail or push message fail.", false);
                                    Log.Error(string.Format("SP Gốc : CompanyID = {0} Product = {1}, Update ImagePath Error {2}", 6619858476258121218, mqProduct.ProductId, numbererror), ex);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                historyDownloadAdapter.Insert(mqProduct.ProductId, DateTime.Now, false, "Product not in Database.", false);
                Log.ErrorFormat("SP Gốc : ProductID = {0} Khong ton tai trong Database.", mqProduct.ProductId);
            }
        }
Exemplo n.º 5
0
        private void DownloadImageSPGoc(long companyId, JobClient jobclientUpdateSolr, JobClient jobclientDownloadImageProduct, JobClient jobclientDeleteThumb, bool reloadall = false)
        {
            var productAdapter = new WSS.DownloadImageCompanyService.DBTableAdapters.ProductTableAdapter();

            productAdapter.Connection.ConnectionString = connectionString;
            var productTable  = new WSS.DownloadImageCompanyService.DB.ProductDataTable();
            int demgetproduct = 0;

            while (true)
            {
                try
                {
                    if (reloadall)
                    {
                        productAdapter.FillBy_AllSPGoc(productTable, companyId);
                    }
                    else
                    {
                        //Lấy danh sách sản phẩm valid = 1 mà chưa có imagepath hoặc sản phẩm có stt =11
                        productAdapter.FillBy_SPGocCanDownloadImage(productTable, companyId);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    demgetproduct++;
                    if (demgetproduct == 1)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error 1", companyId), ex);
                    }
                    else if (demgetproduct == numbererror)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error {1}", companyId, numbererror), ex);
                        break;
                    }
                    Thread.Sleep(60000);
                }
            }
            try
            {
                if (productTable.Rows.Count > 0)
                {
                    string path         = pathImageSPGOC;
                    int    countsuccess = 0;
                    int    countproduct = productTable.Rows.Count;
                    for (int i = 0; i < productTable.Rows.Count; i++)
                    {
                        long   productId   = Common.Obj2Int64(productTable.Rows[i]["ID"].ToString());
                        string nameProduct = productTable.Rows[i]["Name"].ToString();
                        string imageUrl    = productTable.Rows[i]["ImageUrls"].ToString();
                        if (string.IsNullOrEmpty(nameProduct) || string.IsNullOrEmpty(imageUrl))
                        {
                            Log.Error(string.Format("CompanyID = {0} ProductID = {1} Name or ImageUrl is null or empty.", companyId, productId));
                            continue;
                        }
                        string filename = Common.UnicodeToKoDauAndGach(nameProduct);
                        string tempf    = filename.Replace("-", "");
                        if (tempf.Length < 3)
                        {
                            continue;
                        }
                        string folder = tempf.Substring(0, 3);
                        if (folder == "bin")
                        {
                            folder = "bin1";
                        }
                        if (folder == "con")
                        {
                            folder = "con1";
                        }
                        if (folder == "aux")
                        {
                            folder = "aux1";
                        }
                        if (folder == "prn")
                        {
                            folder = "prn1";
                        }
                        if (folder == "nul")
                        {
                            folder = "nul1";
                        }
                        if (filename.Length > 100)
                        {
                            filename = filename.Substring(0, 99);
                        }
                        bool fileSaved = Common.SaveFileDownloadImage(imageUrl, path + folder, filename + ".jpg", productId, companyId);
                        if (fileSaved)
                        {
                            int    w = 0, h = 0;
                            string pathsave = "Store/images/" + folder + "/" + filename + ".jpg";
                            int    dem      = 0;
                            while (checkstop)
                            {
                                try
                                {
                                    productAdapter.UpdateQuery(pathsave, w, h, true, productId);
                                    Log.InfoFormat("{0}/{1}. CompanyID = {2} ProductID = {3} Download Image success.", i, countproduct, companyId, productId);
                                    countsuccess++;
                                    #region Send Message Update solr and redis

                                    var job = new Job {
                                        Data = BitConverter.GetBytes(productId), Type = 2
                                    };
                                    jobclientUpdateSolr.PublishJob(job, updateProductJobExpirationMS);
                                    //Log.InfoFormat("Send message To RabbitMq {0} with ID = {1}", rabbitMQServerName, productId);
                                    #endregion

                                    #region Xóa ảnh Thumb nếu có
                                    //push message lên service xóa ảnh thumb
                                    var deletejob = new Job();
                                    var thumb     = new MqThumbImageInfo
                                    {
                                        ProductId   = productId,
                                        FolderImage = folder,
                                        ImageName   = filename,
                                        TypeProduct = 2
                                    };
                                    deletejob.Data = MqThumbImageInfo.GetMess(thumb);
                                    jobclientDeleteThumb.PublishJob(deletejob);
                                    #endregion
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    Thread.Sleep(10000);
                                    dem++;
                                    if (dem == 1)
                                    {
                                        Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error 1", companyId, productId), ex);
                                    }
                                    else if (dem == numbererror)
                                    {
                                        Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error {2}", companyId, productId, numbererror), ex);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    Log.InfoFormat("CompanyID = {0} Download Image Success {1}/{2}", companyId, countsuccess, productTable.Rows.Count);
                }
                else
                {
                    Log.InfoFormat("CompanyID = {0} 0 product (valid = 1 and ImagePath is Null or empty)", companyId);
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("CompanyID = {0} ERROR~~~", companyId), ex);
            }
        }
Exemplo n.º 6
0
        private void DownloadImageCompany(long companyId, JobClient jobclientUpdateSolr, JobClient jobclientDownloadImageProduct, JobClient jobclientDeleteThumb, bool reloadall = false)
        {
            DateTime startDownload = DateTime.Now;

            WSS.DownloadImageCompanyService.DBTableAdapters.ProductTableAdapter productAdapter = new WSS.DownloadImageCompanyService.DBTableAdapters.ProductTableAdapter();
            WSS.DownloadImageCompanyService.DBTableAdapters.History_DownloadImageCompanyTableAdapter historyAdapter = new DBTableAdapters.History_DownloadImageCompanyTableAdapter();
            productAdapter.Connection.ConnectionString = connectionString;
            historyAdapter.Connection.ConnectionString = connectionString;
            WSS.DownloadImageCompanyService.DB.ProductDataTable productTable = new WSS.DownloadImageCompanyService.DB.ProductDataTable();
            int demgetproduct = 0;

            while (true)
            {
                try
                {
                    if (reloadall)
                    {
                        productAdapter.FillBy_AllValid(productTable, companyId);
                    }
                    else
                    {
                        //Lấy danh sách sản phẩm valid = 1
                        productAdapter.FillBy_CompanyIDValid1AndImagePathNull(productTable, companyId);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    demgetproduct++;
                    if (demgetproduct == 1)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error 1", companyId), ex);
                    }
                    else if (demgetproduct == numbererror)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error {1}", companyId, numbererror), ex);
                        //Ghi log
                        historyAdapter.Insert(companyId, 0, 0, 0, startDownload, DateTime.Now, "Lấy dữ liệu " + numbererror + " lần từ SQL fails. " + ex.ToString());
                        return;
                    }
                    Thread.Sleep(60000);
                }
            }

            if (productTable.Rows.Count > 0)
            {
                String      path         = pathImage;
                int         countsuccess = 0;
                int         fail         = 0;
                List <long> listIDFail   = new List <long>();
                string      exception    = "";
                try
                {
                    for (int i = 0; i < productTable.Rows.Count; i++)
                    {
                        long   productId = Common.Obj2Int64(productTable.Rows[i]["ID"].ToString());
                        string url       = productTable.Rows[i]["DetailUrl"].ToString();
                        if (string.IsNullOrEmpty(url))
                        {
                            fail++;
                            listIDFail.Add(productId);
                            continue;
                        }

                        Uri    root     = new Uri(url);
                        string domain   = root.DnsSafeHost.Replace("www.", "");
                        string char1    = domain.Substring(0, 1);
                        String namepath = domain.Substring(0, 1) + "\\" + root.DnsSafeHost.Replace("www.", "").Replace('.', '_');
                        string direct   = namepath + "\\";
                        string filename = Common.UnicodeToKoDauAndGach(productTable.Rows[i]["Name"].ToString());
                        string tempf    = filename.Replace("-", "");
                        if (tempf.Length < 3)
                        {
                            fail++;
                            listIDFail.Add(productId);
                            continue;
                        }
                        string folder = tempf.Substring(0, 3);
                        if (folder == "bin")
                        {
                            folder = "bin1";
                        }
                        if (folder == "con")
                        {
                            folder = "con1";
                        }
                        if (folder == "aux")
                        {
                            folder = "aux1";
                        }
                        if (folder == "prn")
                        {
                            folder = "prn1";
                        }
                        if (folder == "nul")
                        {
                            folder = "nul1";
                        }
                        direct += folder + "\\";
                        if (filename.Length > 100)
                        {
                            filename = filename.Substring(0, 99);
                        }
                        filename += "_" + productId;
                        String fullfile = path + direct + filename + ".jpg";
                        int    w = 0, h = 0;
                        string imageurl = string.Empty;
                        imageurl = productTable.Rows[i]["ImageUrls"].ToString();
                        if (string.IsNullOrEmpty(imageurl))
                        {
                            fail++;
                            Log.Error(string.Format("CompanyID = {0} ProductID = {1} ImageUrl is null or empty.", companyId, productId));
                            continue;
                        }
                        bool fileSaved = false;
                        try
                        {
                            fileSaved = Common.SaveFileDownloadImage(imageurl, path + direct, filename + ".jpg", productId, companyId);
                        }
                        catch (Exception ex)
                        {
                            exception += ex.Message + "|";
                        }

                        if (fileSaved)
                        {
                            string pathsave = direct + filename.Replace('\\', '/') + ".jpg";
                            pathsave = "Store/" + pathsave.Replace('\\', '/');
                            int dem = 0;
                            while (checkstop)
                            {
                                try
                                {
                                    productAdapter.UpdateQuery(pathsave, w, h, true, productId);
                                    Log.InfoFormat("CompanyID = {0} ProductID = {1} Download Image success.", companyId, productId);
                                    #region Send Message Update solr and redis

                                    var job = new Job {
                                        Data = BitConverter.GetBytes(productId), Type = 2
                                    };
                                    jobclientUpdateSolr.PublishJob(job, updateProductJobExpirationMS);
                                    //Log.InfoFormat("Send message To RabbitMq {0} with ID = {1}", rabbitMQServerName, productId);
                                    #endregion

                                    #region Xóa ảnh Thumb nếu có
                                    //push message lên service xóa ảnh thumb
                                    var deletejob = new Job();
                                    var thumb     = new MqThumbImageInfo
                                    {
                                        ProductId   = productId,
                                        FolderImage = direct,
                                        ImageName   = filename,
                                        TypeProduct = 1
                                    };
                                    deletejob.Data = MqThumbImageInfo.GetMess(thumb);
                                    jobclientDeleteThumb.PublishJob(deletejob);
                                    #endregion
                                    countsuccess++;
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    Thread.Sleep(10000);
                                    dem++;
                                    if (dem == 1)
                                    {
                                        Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error 1", companyId, productId), ex);
                                    }
                                    else if (dem == numbererror)
                                    {
                                        fail++;
                                        listIDFail.Add(productId);
                                        Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error {2}", companyId, productId, numbererror), ex);
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            fail++;
                            listIDFail.Add(productId);
                            //Check số lần download error trên Redis
                            //int errordowbload = errordowbload = RedisErrorDownloadImageProductAdapter.GetErrorDownloadImage(productId);
                            ////Nếu số lần < numbererror thì push lại message lên để lúc khác download
                            //if (errordowbload <= numbererror)
                            //{
                            //    errordowbload++;
                            //    RedisErrorDownloadImageProductAdapter.SetErrorDownloadImage(productId, errordowbload);
                            //    Job job = new Job();
                            //    MQChangeImage mq = new MQChangeImage();
                            //    mq.ProductID = productId;
                            //    mq.Type = 1;
                            //    job.Data = MQChangeImage.GetMess(mq);
                            //    try
                            //    {
                            //        jobclientDownloadImageProduct.PublishJob(job);
                            //    }
                            //    catch (Exception ex)
                            //    {
                            //        Log.Error(string.Format("CompanyID = {0} ProductID = {1} Send message Redownload image.", companyId, productId), ex);
                            //    }
                            //}
                        }
                    }
                    Log.InfoFormat("CompanyID = {0} Download Image Success {1}/{2}", companyId, countsuccess, productTable.Rows.Count);
                    //Ghi log
                    historyAdapter.Insert(companyId, productTable.Rows.Count, countsuccess, fail, startDownload, DateTime.Now, "Finised! " + ConvertListToString(listIDFail) + " . MessageError: " + exception);
                }
                catch (Exception ex)
                {
                    //Ghi log
                    historyAdapter.Insert(companyId, productTable.Rows.Count, countsuccess, fail, startDownload, DateTime.Now, "Error: " + ex.Message + " " + ConvertListToString(listIDFail));
                    Log.Error(string.Format("CompanyID = {0} ERROR~~~", companyId), ex);
                }
            }
            else
            {
                Log.InfoFormat("CompanyID = {0} 0 product (valid = 1 and ImagePath is Null or empty)", companyId);
                //Ghi log
                historyAdapter.Insert(companyId, 0, 0, 0, startDownload, DateTime.Now, "0 Product Valid = 1 and ImagePath is Null or empty");
            }
        }
Exemplo n.º 7
0
        private void DownLoad(int index, long companyId, bool reloadall)
        {
            rabbitMQServer = RabbitMQManager.GetRabbitMQServer(rabbitMQServerName);
            //Jobclient update solr and redis
            var updateProductJobClient = new JobClient(updateProductExchangeGroupName, GroupType.Direct, updateProductqueueJobName, true, rabbitMQServer);
            //JobClient download image fails
            //JobClient downloadImageProductJobClient = new JobClient(updateProductImageGroupName, GroupType.Topic, updateProductImageProductJobName, true, rabbitMQServer);
            //JobClient delete thumb
            var deleteThumbJobClient = new JobClient(updateProductImageGroupName, GroupType.Topic, deleteThumbImageProductJobName, true, rabbitMQServer);

            WSS.DownloadImageByHand.DBTableAdapters.ProductTableAdapter productAdapter = new WSS.DownloadImageByHand.DBTableAdapters.ProductTableAdapter();
            productAdapter.Connection.ConnectionString = connectionString;
            WSS.DownloadImageByHand.DB.ProductDataTable productTable = new WSS.DownloadImageByHand.DB.ProductDataTable();
            int demgetproduct = 0;

            this.Invoke(new Action(() =>
            {
                rbIndex.AppendText("Get list Product from SQL..." + companyId + System.Environment.NewLine);
            }));
            while (true)
            {
                try
                {
                    if (reloadall)
                    {
                        productAdapter.FillBy_AllValid(productTable, companyId);
                    }
                    else
                    {
                        //Lấy danh sách sản phẩm valid = 1
                        productAdapter.FillBy_CompanyIDValid1AndImagePathNull(productTable, companyId);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    demgetproduct++;
                    if (demgetproduct == 1)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error 1", companyId), ex);
                        this.Invoke(new Action(() =>
                        {
                            rbIndex.AppendText("1.CompanyID = " + companyId + ". Get Product of Company in SQL Error" + ex.ToString() + System.Environment.NewLine);
                        }));
                    }
                    else if (demgetproduct == numbererror)
                    {
                        Log.Error(string.Format("CompanyID = {0} Get Product of Company in SQL Error {1}", companyId, numbererror), ex);
                        this.Invoke(new Action(() =>
                        {
                            rbIndex.AppendText(numbererror + ".CompanyID = " + companyId + ". Get Product of Company in SQL Error." + ex.ToString() + System.Environment.NewLine);
                        }));
                        break;
                    }
                    Thread.Sleep(60000);
                }
            }

            try
            {
                if (productTable.Rows.Count > 0)
                {
                    int total = productTable.Rows.Count;
                    this.Invoke(new Action(() =>
                    {
                        rbIndex.AppendText(companyId + ".Get " + total + " Product from SQL..." + System.Environment.NewLine);
                    }));
                    String path         = pathImage;
                    int    countsuccess = 0;
                    int    countfail    = 0;
                    for (int i = 0; i < productTable.Rows.Count; i++)
                    {
                        long   productId = Common.Obj2Int64(productTable.Rows[i]["ID"].ToString());
                        string url       = productTable.Rows[i]["DetailUrl"].ToString();
                        if (string.IsNullOrEmpty(url))
                        {
                            continue;
                        }
                        Uri    root     = new Uri(url);
                        string domain   = root.DnsSafeHost.Replace("www.", "");
                        string char1    = domain.Substring(0, 1);
                        String namepath = domain.Substring(0, 1) + "\\" + root.DnsSafeHost.Replace("www.", "").Replace('.', '_');
                        string direct   = namepath + "\\";
                        string filename = Common.UnicodeToKoDauAndGach(productTable.Rows[i]["Name"].ToString());
                        string tempf    = filename.Replace("-", "");
                        if (tempf.Length < 3)
                        {
                            continue;
                        }
                        string folder = tempf.Substring(0, 3);
                        if (folder == "bin")
                        {
                            folder = "bin1";
                        }
                        if (folder == "con")
                        {
                            folder = "con1";
                        }
                        if (folder == "aux")
                        {
                            folder = "aux1";
                        }
                        if (folder == "prn")
                        {
                            folder = "prn1";
                        }
                        if (folder == "nul")
                        {
                            folder = "nul1";
                        }
                        direct += folder + "\\";
                        if (filename.Length > 100)
                        {
                            filename = filename.Substring(0, 99);
                        }
                        filename += "_" + productId;
                        String fullfile = path + direct + filename + ".jpg";
                        int    w = 0, h = 0;
                        string imageurl = string.Empty;
                        imageurl = productTable.Rows[i]["ImageUrls"].ToString();
                        if (string.IsNullOrEmpty(imageurl))
                        {
                            Log.Error(string.Format("CompanyID = {0} ProductID = {1} ImageUrl is null or empty.", companyId, productId));
                            this.Invoke(new Action(() =>
                            {
                                rbListFails.AppendText(i + "." + productId + ": ImageUrl Null or Empty." + System.Environment.NewLine);
                            }));
                            continue;
                        }
                        bool fileSaved = false;
                        //Common.SaveFileDownloadImage(imageurl, path + direct, filename + ".jpg", productId, companyId);
                        if (fileSaved)
                        {
                            this.Invoke(new Action(() =>
                            {
                                rbIndex.AppendText(string.Format("{0}...{1}: {2}/{3} . {4} sucess.", index, companyId, i, total, productId) + System.Environment.NewLine);
                            }));
                            string pathsave = direct + filename.Replace('\\', '/') + ".jpg";
                            pathsave = "Store/" + pathsave.Replace('\\', '/');
                            try
                            {
                                productAdapter.UpdateQuery(pathsave, w, h, productId);
                                Log.InfoFormat("CompanyID = {0} ProductID = {1} Download Image success.", companyId, productId);
                                countsuccess++;
                                this.Invoke(new Action(() =>
                                {
                                    lbSuccess.Text = countsuccess.ToString();
                                }));
                            }
                            catch (Exception ex)
                            {
                                //Thread.Sleep(10000);
                                //dem++;
                                //if (dem == 1)
                                Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error", companyId, productId), ex);
                                this.Invoke(new Action(() =>
                                {
                                    rbListFails.AppendText(i + "." + productId + ": Update SQL fails." + ex.ToString() + System.Environment.NewLine);
                                }));
                                //else if (dem == numbererror)
                                //{
                                //    Log.Error(string.Format("CompanyID = {0} Product = {1}, Update ImagePath Error {2}", companyId, productId, numbererror), ex);
                                //    break;
                                //}
                            }
                            try
                            {
                                #region Send Message Update solr and redis
                                Job job = new Job();
                                job.Data = BitConverter.GetBytes(productId);
                                job.Type = 2;
                                updateProductJobClient.PublishJob(job, updateProductJobExpirationMS);
                                //Log.InfoFormat("Send message To RabbitMq {0} with ID = {1}", rabbitMQServerName, productId);
                                #endregion

                                #region Xóa ảnh Thumb nếu có
                                //push message lên service xóa ảnh thumb
                                Job deletejob          = new Job();
                                MqThumbImageInfo thumb = new MqThumbImageInfo();
                                thumb.ProductId   = productId;
                                thumb.FolderImage = direct;
                                thumb.ImageName   = filename;
                                thumb.TypeProduct = 1;
                                deletejob.Data    = MqThumbImageInfo.GetMess(thumb);
                                deleteThumbJobClient.PublishJob(deletejob);
                                #endregion
                            }
                            catch (Exception ex)
                            {
                                this.Invoke(new Action(() =>
                                {
                                    rbListFails.AppendText(i + "." + productId + ": Update Solr + Push service Delete Thumb fails." + ex.ToString() + System.Environment.NewLine);
                                }));
                            }
                        }
                        else
                        {
                            countfail++;
                            //Show thông tin không download được ảnh ra
                            this.Invoke(new Action(() =>
                            {
                                lbFails.Text = countfail.ToString();
                                rbIndex.AppendText(string.Format("{0}...{1}: {2}/{3} . {4} fails.", index, companyId, i, total, productId) + System.Environment.NewLine);
                                rbListLinksFails.AppendText(i + "." + productId + ": Download fails." + imageurl + "DetailUrl: " + url + System.Environment.NewLine);
                            }));

                            //Check số lần download error trên Redis
                            //int errordowbload = RedisErrorDownloadImageProductAdapter.GetErrorDownloadImage(productId);
                            ////Nếu số lần < numbererror thì push lại message lên để lúc khác download
                            //if (errordowbload <= numbererror)
                            //{
                            //    errordowbload++;
                            //    RedisErrorDownloadImageProductAdapter.SetErrorDownloadImage(productId, errordowbload);
                            //    Job job = new Job();
                            //    MQChangeImage mq = new MQChangeImage();
                            //    mq.ProductID = productId;
                            //    mq.Type = 1;
                            //    job.Data = MQChangeImage.GetMess(mq);
                            //    try
                            //    {
                            //        jobclientDownloadImageProduct.PublishJob(job);
                            //    }
                            //    catch (Exception ex)
                            //    {
                            //        Log.Error(string.Format("CompanyID = {0} ProductID = {1} Send message Redownload image.", companyId, productId), ex);
                            //    }
                            //}
                        }
                    }
                    Log.InfoFormat("CompanyID = {0} Download Image Success {1}/{2}", companyId, countsuccess, productTable.Rows.Count);
                    this.Invoke(new Action(() =>
                    {
                        rbIndex.AppendText(companyId + " FINISHED...Success: " + countsuccess + "- Fails: " + countfail + System.Environment.NewLine);
                    }));
                }
                else
                {
                    Log.InfoFormat("CompanyID = {0} 0 product (valid = 1 and ImagePath is Null or empty)", companyId);
                    this.Invoke(new Action(() =>
                    {
                        rbIndex.AppendText(companyId + "Get 0 Product from SQL..." + System.Environment.NewLine);
                    }));
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("CompanyID = {0} ERROR~~~", companyId), ex);
                this.Invoke(new Action(() =>
                {
                    rbListFails.AppendText(companyId + " FAILS..." + ex.ToString() + System.Environment.NewLine);
                }));
            }
        }