public void UploadImageinformationTest() { var vehicle = new ImageInformationDto(); var large = "https://cvis.iaai.com/thumbnail?imageKeys=18693774~SID~I1"; vehicle.StockNumber = "18693774"; vehicle.FileName = "18693774_1"; vehicle.ImageByteArray = new WebClient().DownloadData(large); var request = new ImageUploadRequest() { UserRequestInfo = User, ImageInformation = vehicle }; var uploadStockImage = ImageClient.UploadImageInformation(request); Assert.IsTrue(uploadStockImage.WasSuccessful); }
public void ProcessImage() { var connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; DataSet imageDS = new DataSet("root"); DataTable imageDT = new DataTable("stock"); imageDT.Columns.Add("StockNumber", typeof(string)); imageDT.Columns.Add("ImageIndex", typeof(int)); imageDT.Columns.Add("ImageUrl", typeof(string)); imageDS.Tables.Add(imageDT); Dictionary <string, int> push = new Dictionary <string, int>(); try { clsLog.LogInfo("ProcessImage - Getting stock numbers for downloading"); var ds = clsDB.funcExecuteSQLDS("SP_GETSTOCKNUMBER '" + Environment.MachineName + "'", connection, 300); DataTable dt = ds.Tables[0]; clsLog.LogInfo("ProcessImage - Got stock numbers for downloading"); dt.Rows.OfType <DataRow>().ToList().ForEach(p => { var imageUrls = GetImage(p["StockNumber"].ToString()); clsLog.LogInfo("ProcessImage - Got Image URLs for Stock " + p["StockNumber"].ToString()); //if (imageUrls[0] == "Specialty" || imageUrls[0] == "Offsite") // clsDB.funcExecuteSQL(string.Format("SP_UpdateStockImages @xml='{0}',@stockstatus=3", imageDS.GetXml()), connection); //else if (string.IsNullOrEmpty(imageUrls[0])) // clsDB.funcExecuteSQL(string.Format("SP_UpdateStockImages @xml='{0}',@stockstatus=2", imageDS.GetXml()), connection); //else //{ // imageDT.Rows.Clear(); for (int i = 0; i < 10; i++) { imageDT.Rows.Add(imageDT.NewRow()); imageDT.Rows[imageDT.Rows.Count - 1]["StockNumber"] = p["StockNumber"].ToString(); imageDT.Rows[imageDT.Rows.Count - 1]["ImageIndex"] = i; imageDT.Rows[imageDT.Rows.Count - 1]["ImageUrl"] = imageUrls[i]; } //clsDB.funcExecuteSQL(string.Format("SP_UpdateStockImages @xml='{0}'", imageDS.GetXml()), connection); //} clsLog.LogInfo("ProcessImage - HighRes Image URLs saved"); clsLog.LogInfo("ProcessImage - Sending Stock Image URLs " + p["StockNumber"].ToString()); var pushedSuccessful = true; if (imageUrls[0] == "Specialty" || imageUrls[0] == "Offsite") { push.Add(p["StockNumber"].ToString(), 3); } else if (string.IsNullOrEmpty(imageUrls[0])) { push.Add(p["StockNumber"].ToString(), 2); } else { for (int i = 0; i < 10; i++) { var imageinfo = new ImageInformationDto() { StockNumber = p["StockNumber"].ToString(), FileName = p["StockNumber"].ToString() + "_" + i, ImageByteArray = GetImageBytes(imageUrls[i]) }; clsLog.LogInfo("ProcessImage - Sending Stock Image URLs " + p["StockNumber"].ToString() + " Index " + i); var request = new ImageUploadRequest() { UserRequestInfo = User, ImageInformation = imageinfo }; var response = ImageClient.UploadImageInformation(request); if (response.WasSuccessful) { clsLog.LogInfo("ProcessImage - Sent Stock Image URLs " + p["StockNumber"] + "_" + i); File.Delete(imageUrls[i]); } else { pushedSuccessful = false; clsLog.LogError("ProcessImage - Unable to send Stock Image URLs " + p["StockNumber"] + "_" + i); } } if (!push.Keys.ToList().Exists(e => p["StockNumber"].ToString() == e)) { push.Add(p["StockNumber"].ToString(), pushedSuccessful ? 1 : 5); } } //clsDB.funcExecuteSQL(string.Format("SP_UpdatePushedStatus @stocknumbers='{0}', @status='{1}'", p["StockNumber"].ToString(), pushedSuccessful ? 1 : 5), connection); }); //------------------------------------- clsDB.funcExecuteSQL(string.Format("SP_UpdateStockImages @xml='{0}'", imageDS.GetXml()), connection); clsDB.funcExecuteSQL(string.Format("SP_UpdatePushedStatus @stocknumbers='{0}', @status='{1}'", string.Join(",", push.Keys), string.Join(",", push.Values)), connection); } catch (Exception ex) { clsLog.LogError("ProcessImage - Error while executing. " + ex.Message); } }
//run every 10 minutes public bool PushImageData() { bool successWhilePushing = true; try { clsLog.LogInfo("PushImageData - Getting StockImage URLs to push"); var connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(connection)) using (SqlCommand cmd = new SqlCommand("dyndata.dbo.SP_PUSHIMAGEDATA", con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { cmd.CommandType = CommandType.StoredProcedure; da.Fill(dt); } } var stocks = dt.Rows.OfType <DataRow>().ToList().ConvertAll(c => new Stock() { ImageIndex = Convert.ToInt16(c["imageindex"]), ImageURL = c["ImageUrl"].ToString().Trim(), StockNumber = c["stocknumber"].ToString().Trim() }); var stack = new ConcurrentStack <Stock>(stocks); clsLog.LogInfo("PushImageData - Got StockImage URLs to push"); Parallel.ForEach(stack, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, p => //stocks.ForEach(p => { clsLog.LogInfo("PushImageData - Sending Stock Image URLs " + p.StockNumber); var imageinfo = new ImageInformationDto() { StockNumber = p.StockNumber, FileName = p.StockNumber + "_" + p.ImageIndex, ImageByteArray = GetImageBytes(p.ImageURL) }; try { var request = new ImageUploadRequest() { UserRequestInfo = User, ImageInformation = imageinfo }; var response = ImageClient.UploadImageInformation(request); if (response.WasSuccessful) { clsLog.LogInfo("PushImageData - Sent Stock Image URLs " + p.StockNumber + "_" + p.ImageIndex); p.Pushed = true; File.Delete(p.ImageURL); } else { clsLog.LogInfo("PushImageData - Unable to send Stock Image URLs " + p.StockNumber + "_" + p.ImageIndex); } } catch (Exception ex1) { clsLog.LogError("PushImageData - Error while sending HighRes stock Image urls. Stock" + p.StockNumber + "_" + p.ImageIndex + " Error " + ex1.Message); successWhilePushing = false; } }); using (SqlConnection con = new SqlConnection(connection)) { con.Open(); using (SqlCommand cmd = new SqlCommand("dyndata.dbo.SP_UpdatePushedStatus", con)) using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@stocknumbers", string.Join(",", stack.Where(w => w.Pushed).Select(s => s.StockNumber).ToArray())); cmd.ExecuteNonQuery(); } con.Close(); } } catch (Exception ex) { clsLog.LogError("PushImageData - Error while sending HighRes stock Image urls. Error " + ex.Message); } return(successWhilePushing); }