public void SaveImageData(ProductImageDomainModel modelDM) { ProductImage img; if (modelDM.ImageID > 0) { img = productImageRepository.SingleOrDefault(x => x.ImageID == modelDM.ImageID); img.ImageName = modelDM.ImageName; img.ImageByte = modelDM.ImageByte; img.ImagePath = modelDM.ImagePath; img.IsDeleted = modelDM.IsDeleted; img.ImageExt = modelDM.ImageExt; img.ImageSize = modelDM.ImageSize; img.ImageHeight = modelDM.ImageHeight; img.ImageWidth = modelDM.ImageWidth; productImageRepository.Update(img); } else { img = new ProductImage(); img.ImageName = modelDM.ImageName; img.ImageByte = modelDM.ImageByte; img.ImagePath = modelDM.ImagePath; img.IsDeleted = modelDM.IsDeleted; img.ImageExt = modelDM.ImageExt; img.ImageSize = modelDM.ImageSize; img.ImageHeight = modelDM.ImageHeight; img.ImageWidth = modelDM.ImageWidth; productImageRepository.Insert(img); } }
public void UpdateProduct(ProductViewModel productVM) { if (productVM.ImageFile != null) { string filePath = string.Empty; string fileContentType = string.Empty; byte[] uploadedFile = new byte[productVM.ImageFile.InputStream.Length]; productVM.ImageFile.InputStream.Read(uploadedFile, 0, uploadedFile.Length); fileContentType = productVM.ImageFile.ContentType; string folderPath = "/ProductsImage/"; string fileName = DateTime.Now.ToString("yyyyMMddHHmmss-") + productVM.ImageFile.FileName; this.WriteBytesToFile(this.Server.MapPath(folderPath), uploadedFile, fileName); filePath = folderPath + fileName; string fullFilePath = "C:/Users/Jairus Macatangay/source/repos/CaffeineFix/CaffeineFix" + filePath; FileInfo fi = new FileInfo(fullFilePath); string imgSize = (fi.Length / 1024) + " KB"; Bitmap bitmap = new Bitmap(fullFilePath); int imgHeight = bitmap.Height; int imgWidth = bitmap.Width; ProductImageDomainModel img = new ProductImageDomainModel(); img.ImageName = fileName; img.ImageByte = uploadedFile; img.ImageID = (int)productVM.ImageID; img.ImagePath = filePath; img.ImageExt = fileContentType; img.IsDeleted = false; img.ImageSize = imgSize; img.ImageHeight = imgHeight; img.ImageWidth = imgWidth; productsBusiness.SaveImageData(img); } ProductDomainModel productDM = new ProductDomainModel(); AutoMapper.Mapper.Map(productVM, productDM); productsBusiness.UpdateProduct(productDM); }
public void AddProduct(ProductViewModel productVM) { int imageID = 0; if (productVM.ImageFile != null) { string filePath = string.Empty; string fileContentType = string.Empty; byte[] uploadedFile = new byte[productVM.ImageFile.InputStream.Length]; productVM.ImageFile.InputStream.Read(uploadedFile, 0, uploadedFile.Length); fileContentType = productVM.ImageFile.ContentType; string folderPath = "/ProductsImage/"; string fileName = DateTime.Now.ToString("yyyyMMddHHmmss-") + productVM.ImageFile.FileName; this.WriteBytesToFile(this.Server.MapPath(folderPath), uploadedFile, fileName); filePath = folderPath + fileName; string fullFilePath = "C:/Users/Jairus Macatangay/source/repos/CaffeineFix/CaffeineFix" + filePath; FileInfo fi = new FileInfo(fullFilePath); string imgSize = (fi.Length / 1024) + " KB"; int fiAttempts = 0; int fiTimes = 3; TimeSpan fiDelay = TimeSpan.FromSeconds(5.0); int imgHeight = 0; int imgWidth = 0; do { try { fiAttempts++; using (Bitmap bitmap = new Bitmap(fullFilePath)) { imgHeight = bitmap.Height; imgWidth = bitmap.Width; } break; } catch (Exception ex) { if (fiAttempts == fiTimes) { throw ex; } Task.Delay(fiDelay).Wait(); } } while (true); ProductImageDomainModel img = new ProductImageDomainModel(); img.ImageName = fileName; img.ImageByte = uploadedFile; img.ImagePath = filePath; img.ImageExt = fileContentType; img.IsDeleted = false; img.ImageSize = imgSize; img.ImageHeight = imgHeight; img.ImageWidth = imgWidth; productsBusiness.SaveImageData(img); imageID = productsBusiness.GetRecentImageID(); } productVM.ImageID = imageID; ProductDomainModel productDM = new ProductDomainModel(); AutoMapper.Mapper.Map(productVM, productDM); productsBusiness.AddProduct(productDM); }