private static string outputExtension(ImageFormat imageFormat)
        {
            string result;

            try
            {
                result = ImageTypeMapper.ByImageFormat(imageFormat).extension;
            }
            catch (UnknownImageTypeException)
            {
                result = "png";
            }
            return(result);
        }
Example #2
0
        public override Present Realize(string refCredit)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(documentUri);

            httpWebRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
            D.Sayf(0, "Fetching {0}", new object[] { documentUri });
            HttpWebResponse httpWebResponse;

            try
            {
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                throw new Exception(string.Format("timeout waiting for url ({0})", ex.ToString()));
            }

            if (httpWebResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(string.Format("HTTP {0} from web source", httpWebResponse.StatusCode.ToString()));
            }

            Stream        responseStream = httpWebResponse.GetResponseStream();
            HashAlgorithm hashAlgorithm  = new SHA1CryptoServiceProvider();
            string        text           = FileUtilities.MakeTempFilename(MakeDownloadCacheDir(), "Download");
            Stream        outputStream   = new FileStream(text, FileMode.CreateNew);
            StreamTee     streamTee      = new StreamTee(responseStream, outputStream);

            byte[] buffer = hashAlgorithm.ComputeHash(streamTee);
            streamTee.Close();
            string arg   = BytesToHexString(buffer);
            string text2 = Path.Combine(MakeDownloadCacheDir(),
                                        string.Format("Hash-{0}.{1}", arg, ImageTypeMapper.ByMimeType(httpWebResponse.ContentType).extension));

            if (File.Exists(text2))
            {
                File.Delete(text);
            }
            else
            {
                File.Move(text, text2);
            }

            httpWebResponse.Close();
            return(new SourceDocument(new LocalDocumentDescriptor(text2, pageNumber)));
        }