Beispiel #1
0
        //private void GetEligibleWorkingURLs(List<ImageDetails> imgs)
        //{

        //    try
        //    {
        //        Parallel.ForEach(imgs, new ParallelOptions { MaxDegreeOfParallelism = 1 }, (img) =>
        //        {
        //            try
        //            {
        //                var request = (HttpWebRequest)WebRequest.Create(img.ImageUrl);
        //                request.Timeout = 500;
        //                request.Method = "HEAD";

        //                using (var response = (HttpWebResponse)request.GetResponse())
        //                {
        //                    if (response.StatusCode == HttpStatusCode.OK && response.ContentType.Contains("image"))
        //                    {
        //                        img.isValid = true;

        //                    }
        //                }
        //            }
        //            catch (WebException we)
        //            {
        //                Debug.Print(we.StackTrace);
        //            }
        //        });
        //    }
        //    catch (Exception eeeee)
        //    {
        //        Debug.Print(eeeee.StackTrace);
        //        throw;
        //    }


        //}

        private Task DownloadFile(ImageDetails img)
        {
            //using (WebClient wc = new WebClient())
            using (DwnldWebClient wc = new DwnldWebClient())
            {
                Uri uri = new Uri(img.ImageUrl);

                //string formatedName = string.Format("{0}_{1:yyyy_MM_dd_hh_mm_ss_fff}", img.ID, DateTime.Now);
                string formatedName = string.Format("{0}", img.ID);

                string downloadToDirectory = @"C:\Users\Abhi\Documents\hashes\" + formatedName;

                img.pathOnDisk = downloadToDirectory;

                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Timeout   = 2000;
                request.Method    = "HEAD";
                request.UserAgent = userAgent;

                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.OK && response.ContentType.Contains("image"))
                        {
                            return(wc.DownloadFileTaskAsync(uri, downloadToDirectory));
                        }
                        else
                        {
                            img.error = INVALID_TYPE_ERROR;
                            rejects++;
                            img.hash = EMPTY_MD5_HASH;
                            return(null);
                        }
                    }
                }
                catch (System.Net.WebException ex)
                {
                    img.error = ex.Message;
                    img.hash  = EMPTY_MD5_HASH;
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);
                    rejects++;
                    //return null;
                }
                catch (Exception ex)
                {
                    img.error = ex.Message;
                    img.hash  = EMPTY_MD5_HASH;
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);
                    rejects++;
                    //return null;
                }
                return(null);
                //return wc.DownloadFileTaskAsync(uri, downloadToDirectory);
            }
        }
        private Task DownloadFile(BuzzBuzz_Developments_UserImage img)
        {
            //using (WebClient wc = new WebClient())
            using (DwnldWebClient wc = new DwnldWebClient())
            {
                string fullImagePath = string.Concat(DEV_IMG_S3_PATH + "/" + img.ImageFile);
                Uri    uri           = new Uri(fullImagePath);

                img.FormattedName = img.ImageFile.Length < 150 ? string.Format("{0}_{1}", img.ImageID, img.ImageFile)
                    : string.Format("{0}_{1:yyyy_MM_dd_hh_mm_ss_fff}", img.ImageID.ToString(), DateTime.Now);

                //img.FormattedName = img.ImageFile.Length < 150 ? string.Format("{0}_{1:yyyy_MM_dd_hh_mm_ss_fff}_{2}", img.ImageID, DateTime.Now, img.ImageFile) : img.ImageID.ToString();


                string downloadToDirectory = string.Concat(DownloadDirectory + "/" + img.FormattedName);

                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Timeout   = 2000;
                request.Method    = "HEAD";
                request.UserAgent = userAgent;

                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        //if (response.StatusCode == HttpStatusCode.OK && response.ContentType.Contains("image") || img.ImageFile.EndsWith(".ashx"))
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            return(wc.DownloadFileTaskAsync(uri, downloadToDirectory));
                        }
                        else
                        {
                            img.Error   = INVALID_TYPE_ERROR;
                            img.MD5Hash = INVALID_TYPE_ERROR;
                            rejects++;
                            //img.hash = EMPTY_MD5_HASH;
                            return(null);
                        }
                    }
                }
                catch (System.Net.WebException ex)
                {
                    img.Error   = ex.Message;
                    img.MD5Hash = EMPTY_MD5_HASH;
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);
                    rejects++;
                    //return null;
                }
                catch (Exception ex)
                {
                    img.Error   = ex.Message;
                    img.MD5Hash = EMPTY_MD5_HASH;
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);
                    rejects++;
                    //return null;
                }
                return(null);
                //return wc.DownloadFileTaskAsync(uri, downloadToDirectory);
            }
        }
        private Task DownloadFile(BuzzBuzz_Developments_UnitPlans img)
        {
            //using (WebClient wc = new WebClient())
            using (DwnldWebClient wc = new DwnldWebClient())
            {
                string fullImagePath = string.Concat(UNITPLAN_IMG_S3_PATH + "/" + img.PlanImage);
                Uri    uri           = new Uri(fullImagePath);

                img.FormattedName = img.PlanImage.Length < 150 ? string.Format("{0}_{1}", img.PlanID, img.PlanImage)
                    : string.Format("{0}_{1:yyyy_MM_dd_hh_mm_ss_fff}", img.PlanID.ToString(), DateTime.Now);

                string downloadToDirectory = string.Concat(DownloadDirectory + "/" + img.FormattedName);

                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Timeout   = 2000;
                request.Method    = "HEAD";
                request.UserAgent = userAgent;

                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        //if (response.StatusCode == HttpStatusCode.OK && response.ContentType.Contains("image") || img.ImageFile.EndsWith(".ashx"))
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            /* To set up each task with cancellation token
                             * so that errored out tasks can be cancelled
                             */
                            /*
                             * CancellationTokenSource tokenSource = new CancellationTokenSource();
                             * CancellationToken token = tokenSource.Token;
                             *
                             * return Task.Factory.StartNew(() => wc.DownloadFileTaskAsync(uri, downloadToDirectory), token);
                             */
                            return(wc.DownloadFileTaskAsync(uri, downloadToDirectory));
                        }
                        else
                        {
                            img.Error   = INVALID_TYPE_ERROR;
                            img.MD5Hash = INVALID_TYPE_ERROR;
                            rejects++;
                            //img.hash = EMPTY_MD5_HASH;
                            return(null);
                        }
                    }
                }
                catch (System.Net.WebException ex)
                {
                    img.Error   = ex.Message;
                    img.MD5Hash = EMPTY_MD5_HASH;
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);
                    rejects++;
                    //return null;
                }
                catch (Exception ex)
                {
                    img.Error   = ex.Message;
                    img.MD5Hash = EMPTY_MD5_HASH;
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);
                    rejects++;
                    //return null;
                }
                return(null);
                //return wc.DownloadFileTaskAsync(uri, downloadToDirectory);
            }
        }