Esempio n. 1
0
        public static void ImportImage(string folderPath)
        {
            var imageFolder = new DirectoryInfo(folderPath);

            #region imageFiles
            var images = imageFolder.GetFiles();
            #endregion

            #region imageFolder
            //var images = new List<FileInfo>();
            //var dics = imageFolder.GetDirectories();
            //foreach (var dic in dics)
            //{
            //    var fies = dic.GetFiles();
            //    images.AddRange(fies.ToList());
            //}
            #endregion

            if (images.Any())
            {
                foreach (var img in images)
                {
                    var imageName = img.Name;
                    var imageExtension = Path.GetExtension(imageName);

                    var imageNameWithoutExtension = Path.GetFileNameWithoutExtension(imageName).Replace("-", "");

                    if (!imageNameAllowLengh.Contains(imageNameWithoutExtension.Length))
                    {
                        continue;
                    }

                    var barcode = imageNameWithoutExtension.Substring(0, imageNameWithoutExtension.Length - 2);
                    var product = _repositoryManager.ProductRepository.Table.FirstOrDefault(m => m.BarCode == barcode);
                    if (product != null)
                    {
                        var imageFileFolder = GetImageFolderByBarcode(barcode);
                        var imageCatePath = $@"{barcode}";

                        var productId = product.ProductId;
                        var imageIndex = imageNameWithoutExtension.Substring(imageNameWithoutExtension.Length - 2, 2);

                        string filename = string.Format(@"{0}{1}", imageIndex, imageExtension);
                        string filePath = string.Format(@"{0}/{1}", imageFileFolder, filename);

                        var storeFileName = $@"{imageCatePath}/{filename}";
                        if (!_repositoryManager.ProductImageRepository.Table.ToList().Any(m => m.ProductId == product.ProductId && Path.GetFileNameWithoutExtension(m.FileName) == Path.GetFileNameWithoutExtension(storeFileName)))
                        {
                            FileStream fileStream = new FileStream(img.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                            // 读取文件的 byte[]   
                            byte[] bytes = new byte[fileStream.Length];
                            fileStream.Read(bytes, 0, bytes.Length);
                            fileStream.Close();
                            File.WriteAllBytes(filePath, bytes);
                            //Todo ProductImageTypeId is temp
                            var newProductImage = new ProductImage()
                            {
                                ProductId = productId,
                                //FileName = filePath,
                                FileName = storeFileName,
                                ProductImageTypeId = 1
                            };
                            _repositoryManager.ProductImageRepository.Create(newProductImage);
                            _repositoryManager.ProductImageRepository.Save();
                        }
                        else
                        {
                            Console.WriteLine("ProductId:" + productId + "'s Image:" + storeFileName + "Existed!");
                        }

                    }
                    else
                    {
                        //Todo error
                    }


                }
            }


            //var imageFileFolder = GetImageFolderByCategory(categoryCodeLevel1, categoryCodeLevel2, categoryCodeLevel3);
            //var imageCatePath = $@"{categoryCodeLevel1}/{categoryCodeLevel2}/{categoryCodeLevel3}";
            //int imageIndex = 1;
            //var success = 0;
            //var fail = 0;
            //foreach (ImagePart i in wsPart.DrawingsPart.ImageParts)
            //{
            //    try
            //    {
            //        using (Stream stream = i.GetStream())
            //        {
            //            long length = stream.Length;
            //            byte[] byteStream = new byte[length];
            //            stream.Read(byteStream, 0, (int)length);

            //            var imageValidated = ImageHelper.ValidateImageQualityByPixel(stream);
            //            if (imageValidated)
            //            {

            //                var imageExtension = Path.GetExtension(i.Uri.ToString());
            //                string filename = string.Format(@"{0}_{1}{2}", productId, imageIndex, imageExtension);
            //                string filePath = string.Format(@"{0}/{1}", imageFileFolder, filename);

            //                File.WriteAllBytes(filePath, byteStream);
            //                //Todo ProductImageTypeId is temp
            //                var newProductImage = new ProductImage()
            //                {
            //                    ProductId = productId,
            //                    //FileName = filePath,
            //                    FileName = $@"{imageCatePath}/{filename}",
            //                    ProductImageTypeId = 1
            //                };
            //                _repositoryManager.ProductImageRepository.Create(newProductImage);
            //                _repositoryManager.ProductImageRepository.Save();
            //            }
            //            else
            //            {
            //                Console.WriteLine("Pass Image:productId:" + productId);
            //            }
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        Logger.Log(LogLevel.Error, string.Format("Save Image Error:productId {0},ex:", productId));
            //        fail++;
            //    }
            //    imageIndex++;
            //}
        }
Esempio n. 2
0
        static void ImportImage(WorksheetPart wsPart, int productId, string barcode, string categoryCodeLevel1, string categoryCodeLevel2, string categoryCodeLevel3)
        {
            //var imageFileFolder = GetImageFolderByCategory(categoryCodeLevel1, categoryCodeLevel2, categoryCodeLevel3);
            //var imageCatePath = $@"{categoryCodeLevel1}/{categoryCodeLevel2}/{categoryCodeLevel3}";

            var imageFileFolder = GetImageFolderByBarcode(barcode);
            var imageCatePath = $@"{barcode}";

            string imageIndex = "01";
            var success = 0;
            var fail = 0;
            //foreach (ImagePart i in wsPart.DrawingsPart.ImageParts)
            //{
            var i = wsPart.DrawingsPart.ImageParts.First();
            try
            {
                using (Stream stream = i.GetStream())
                {
                    long length = stream.Length;
                    byte[] byteStream = new byte[length];
                    stream.Read(byteStream, 0, (int)length);

                    var imageValidated = ImageHelper.ValidateImageQualityByPixel(stream);
                    if (imageValidated)
                    {

                        var imageExtension = Path.GetExtension(i.Uri.ToString());

                        string filename = string.Format(@"{0}{1}", imageIndex, imageExtension);
                        string filePath = string.Format(@"{0}/{1}", imageFileFolder, filename);

                        File.WriteAllBytes(filePath, byteStream);
                        //Todo ProductImageTypeId is temp
                        var newProductImage = new ProductImage()
                        {
                            ProductId = productId,
                            //FileName = filePath,
                            FileName = $@"{imageCatePath}/{filename}",
                            ProductImageTypeId = 1
                        };
                        _repositoryManager.ProductImageRepository.Create(newProductImage);
                        _repositoryManager.ProductImageRepository.Save();
                    }
                    else
                    {
                        Console.WriteLine("Pass Image:productId:" + productId);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, string.Format("Save Image Error:productId {0},ex:", productId));
                fail++;
            }
            //imageIndex++;
            //}
        }