public async Task GetImage(string url, string options)
 {
     Uri uri;
     if (Uri.TryCreate(url, UriKind.Absolute, out uri))
     {
         DownloadRequest request = new DownloadRequest
         {
             Url = uri,
             ReferrerUrl = Request.UrlReferrer,
             UserAgent = Request.UserAgent,
             Options = options.FilterOptions()
         };
         Task<Tuple<MimeTypes, string>> processTask = ImageTracker.ProcessRequest(request);
         Tuple<MimeTypes, string> result = await processTask;
         Response.PublicCache(60);
         if (result != null)
         {
             Response.WriteFile(result.Item2);
             Response.ContentType = result.Item1.ToString();
         }
         else
         {
             HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(uri);
             using (HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse())
             using (Stream stream = httpWebResponse.GetResponseStream())
             {
                 if (stream != Stream.Null)
                 {
                     Response.BinaryWrite(StreamHelper.ReadToEnd(stream));
                     Response.ContentType = Utility.Helper.GetImageType(request).Item1.ToString();
                 }
             }
         }
     }
     else
         Response.Redirect("~/Error");
 }
 public async Task<Tuple<MimeTypes, string>> ProcessRequest(DownloadRequest request)
 {
     return await Task.Factory.StartNew(() => ProcessAsync(request));
 }
        private static Tuple<MimeTypes, string> ProcessAsync(DownloadRequest request)
        {
            if (string.IsNullOrEmpty(request.Options))
            {
                if (_savedImages.Exists(request.Url.ToString(), SAVED_IMAGE_SECTION))
                    return DecideWhichImageToUse(request, _savedImages.Get<DownloadResponse>(request.Url.ToString(), SAVED_IMAGE_SECTION));
            }
            else
            {
                if (_customImages.Exists(request.Url + request.Options, CUSTOM_IMAGES_SECTION))
                    return _customImages.Get<DownloadResponse>(request.Url + request.Options, CUSTOM_IMAGES_SECTION).ImageLocations[ImageSizes.Default];
            }

            DownloadResponse response = DownloadProcess.Download(request);
            if (response != null)
            {
                if (!string.IsNullOrEmpty(request.Options))
                {
                    if (!_customImages.Exists(request.Url + request.Options, CUSTOM_IMAGES_SECTION))
                        _customImages.Add(request.Url + request.Options, response, CUSTOM_IMAGES_SECTION);

                    return response.ImageLocations[ImageSizes.Default];
                }
                if (!_savedImages.Exists(request.Url.ToString(), SAVED_IMAGE_SECTION))
                    _savedImages.Add(request.Url.ToString(), response, SAVED_IMAGE_SECTION);

                return DecideWhichImageToUse(request, response);
            }
            return null;
        }
 private static Tuple<MimeTypes, string> DecideWhichImageToUse(DownloadRequest request, DownloadResponse response)
 {
     ImageSizes imageSize = _deviceServices.GetImageSize(request.UserAgent);
     return response.ImageLocations[imageSize];
 }
示例#5
0
 public static Tuple<ImageFormat, string> GetImageType(DownloadRequest request)
 {
     string imageName = request.Url.Segments[request.Url.Segments.Length - 1];
     imageName = imageName.ToLower();
     return DecideImageFormat(imageName);
 }