public ImageShopify getImageFromFtp(ProductTempImage image, string imageName, int i) { try { Web web = _context.Web.Find(1); FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"{web.SMTPURL}/{image.name}{image.extension}"); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(web.SMTPUser, web.SMTPPassword); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); byte[] bytes; using (var memoryStream = new MemoryStream()) { responseStream.CopyTo(memoryStream); bytes = memoryStream.ToArray(); } string img = Convert.ToBase64String(bytes); ImageShopify imgS = new ImageShopify(); imgS.attachment = img; imgS.filename = $"{imageName.ToUpper()}_{i}.jpg"; imgS.alt = $"{imageName.ToUpper()}_{i}.jpg"; return(imgS); } catch (Exception e) { if (e.Message == "Unable to connect to the remote server") { return(getImageFromFtp(image, imageName, i)); } _logger.LogError(e, "Error in ftp"); return(null); } }
public IActionResult Edit(EditProduct lstProduct) { if (ModelState.IsValid) { try { ProductShopify ps = new ProductShopify(); ps.vendor = lstProduct.parent.Vendor; ps.product_type = lstProduct.parent.ProductType; ps.body_html = lstProduct.parent.Description; ps.tags = lstProduct.parent.Tags; ps.handle = lstProduct.parent.Handle; ps.id = lstProduct.parent.Id; ps.published_scope = "global"; ps.title = lstProduct.parent.Title; ps.metafields_global_description_tag = lstProduct.parent.SEODescription; ps.metafields_global_title_tag = lstProduct.parent.SEOTitle; lstProduct.parent.UpdateDate = DateTime.Now; List <Variant> lsVariant = new List <Variant>(); int stock = 0; foreach (Product child in lstProduct.childs) { Variant variant = new Variant(); variant.id = child.Id; variant.sku = child.SKU; variant.price = child.Price; variant.option1 = child.Size; variant.inventory_quantity = child.Stock; variant.inventory_management = "shopify"; variant.compare_at_price = child.CompareAtPrice; variant.grams = child.Peso; variant.weight = child.Peso.ToString(); variant.weight_unit = "g"; stock += child.Stock; lsVariant.Add(variant); child.UpdateDate = DateTime.Now; } List <ProductImage> productImages = _context.ProductImage.Where(i => i.product_id == lstProduct.parent.Id).ToList(); ps.variants = lsVariant; string status = ""; if (stock <= 0 || productImages == null) { status = "draft"; } else { status = "active"; } ps.status = status; lstProduct.parent.Status = status; List <ImageShopify> imageShopifies = new List <ImageShopify>(); ps.images = new List <ImageShopify>(); if (lstProduct.imgtoShow != null && lstProduct.imgtoShow.Count > 0) { int i = 1; foreach (string file in lstProduct.imgtoShow) { if (!Regex.IsMatch(file, @"^(http|https|ftp|)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?([a-zA-Z0-9\-\?\,\'\/\+&%\$#_]+)")) { ImageShopify imgS = new ImageShopify(); imgS.attachment = file; imgS.filename = $"{ps.metafields_global_title_tag.ToUpper().Replace("| ", "").Replace(" ", "_")}_{i}.jpg"; imgS.alt = imgS.filename; imageShopifies.Add(imgS); } i++; } } List <Option> lsOpt = new List <Option>(); Option option = new Option(); option.name = "Talla"; option.position = 1; lsOpt.Add(option); ps.options = lsOpt; if (ps.id == null) { ps.images = imageShopifies; } dynamic oJson = new { product = ps }; if (ps.id != null) { IRestResponse response = CallShopify("products/" + ps.id + ".json", Method.PUT, oJson); if (response.StatusCode.ToString().Equals("OK")) { //if (imageShopifies.Count > 0) //{ // int position = 1; // foreach(ImageShopify imgS in imageShopifies) // { // if (imageShopifies.Count == 3) // imgS.position = position; // dynamic oJ = new // { // image = imgS // }; // IRestResponse response1 = CallShopify($"products/{ps.id}/images.json", Method.POST, oJ); // if(response1.StatusCode.ToString().Equals("OK")) // { // MasterImage mi = JsonConvert.DeserializeObject<MasterImage>(response1.Content); // ProductImage pi = new ProductImage(); // pi.id = mi.image.id; // pi.product_id = lstProduct.parent.Id; // pi.position = mi.image.position; // pi.src = mi.image.src; // pi.alt = mi.image.alt; // position++; // _context.ProductImage.Add(pi); // } // else // { // _logger.LogError("Error uploading product: " + response1.ErrorMessage); // return NotFound(response1.ErrorMessage); // } // } //} _context.Product.Update(lstProduct.parent); _context.Product.UpdateRange(lstProduct.childs); _context.SaveChanges(); _logger.LogInformation("Product uploaded"); } else { _logger.LogError("Error uploading product: " + response.ErrorMessage); return(NotFound(response.ErrorMessage)); } } else { IRestResponse response = CallShopify("products.json", Method.POST, oJson); if (response.StatusCode.ToString().Equals("Created")) { MasterProduct mp = JsonConvert.DeserializeObject <MasterProduct>(response.Content); if (mp.product != null) { lstProduct.parent.Id = mp.product.id; foreach (Variant variant in mp.product.variants) { Product child = lstProduct.childs.Where(c => c.SKU == variant.sku).FirstOrDefault(); child.Id = variant.id; child.InventoryItemId = variant.inventory_item_id; child.ParentId = mp.product.id; } } _context.Product.Add(lstProduct.parent); _context.Product.AddRange(lstProduct.childs); //foreach (ImageShopify img in mp.product.images) //{ // ProductImage pi = new ProductImage(); // pi.id = img.id; // pi.product_id = lstProduct.parent.Id; // pi.src = img.src; // pi.alt = img.alt; // _context.ProductImage.Add(pi); //} _context.SaveChanges(); _logger.LogInformation("Product created"); } else { _logger.LogError("Error uploading product: " + response.ErrorMessage); return(NotFound(response.ErrorMessage)); } } } catch (Exception e) { _logger.LogError("Error updating product", e); return(NotFound(e.Message)); } return(Ok()); } return(Ok()); }