public Stream HttpGet(string url) { UrlDownloadToFile urlDownloadToFile = new UrlDownloadToFile(); urlDownloadToFile.Url = url; urlDownloadToFile.DownloadAction = UrlDownloadToFile.DownloadActions.GET; urlDownloadToFile.ShowSecurityUI = true; return DoDownload(urlDownloadToFile); }
private static Stream DoDownload(UrlDownloadToFile urlDownloadToFile) { string path = TempFileManager.Instance.CreateTempFile("urldownload"); urlDownloadToFile.FilePath = path; urlDownloadToFile.Download(SilentProgressHost.Instance); return(new FileStream(path, FileMode.Open, FileAccess.Read)); }
public Stream HttpPost(string url, Stream postData) { UrlDownloadToFile urlDownloadToFile = new UrlDownloadToFile(); urlDownloadToFile.Url = url; urlDownloadToFile.DownloadAction = UrlDownloadToFile.DownloadActions.POST; urlDownloadToFile.ShowSecurityUI = true; urlDownloadToFile.PostData = StreamHelper.AsBytes(postData); return DoDownload(urlDownloadToFile); }
public Stream HttpGet(string url) { UrlDownloadToFile urlDownloadToFile = new UrlDownloadToFile(); urlDownloadToFile.Url = url; urlDownloadToFile.DownloadAction = UrlDownloadToFile.DownloadActions.GET; urlDownloadToFile.ShowSecurityUI = true; return(DoDownload(urlDownloadToFile)); }
public Stream HttpPost(string url, Stream postData) { UrlDownloadToFile urlDownloadToFile = new UrlDownloadToFile(); urlDownloadToFile.Url = url; urlDownloadToFile.DownloadAction = UrlDownloadToFile.DownloadActions.POST; urlDownloadToFile.ShowSecurityUI = true; urlDownloadToFile.PostData = StreamHelper.AsBytes(postData); return(DoDownload(urlDownloadToFile)); }
/// <summary> /// Simple wrapper function that downloads a url and returns a file path /// (uses all defaults, including writing the file to a temporary path) /// </summary> /// <param name="url"></param> /// <returns></returns> public static string Download(string url, int timeoutMs, WinInetCredentialsContext credentialsContext) { // do the download (will use cached images correctly, etc.) UrlDownloadToFile urlDownloadToFile = new UrlDownloadToFile(); urlDownloadToFile.Url = url; urlDownloadToFile.TimeoutMs = timeoutMs; urlDownloadToFile.CredentialsContext = credentialsContext; urlDownloadToFile.Download(); // return a bitmap based on the file return urlDownloadToFile.FilePath; }
/// <summary> /// Simple wrapper function that downloads a url and returns a file path /// (uses all defaults, including writing the file to a temporary path) /// </summary> /// <param name="url"></param> /// <returns></returns> public static string Download(string url, int timeoutMs, WinInetCredentialsContext credentialsContext) { // do the download (will use cached images correctly, etc.) UrlDownloadToFile urlDownloadToFile = new UrlDownloadToFile(); urlDownloadToFile.Url = url; urlDownloadToFile.TimeoutMs = timeoutMs; urlDownloadToFile.CredentialsContext = credentialsContext; urlDownloadToFile.Download(); // return a bitmap based on the file return(urlDownloadToFile.FilePath); }
public static Bitmap DownloadBitmap(string bitmapUrl, WinInetCredentialsContext credentialsContext) { // download the image try { string fileName = UrlDownloadToFile.Download(bitmapUrl, credentialsContext); return(new Bitmap(fileName)); } catch (Exception ex) { // reformat error for contextual clarity throw new Exception( String.Format(CultureInfo.CurrentCulture, "Error attempting to download bitmap from url {0}: {1}", bitmapUrl, ex.Message), ex); } }
/// <summary> /// Download a reference, providing progress /// </summary> /// <param name="reference">The reference to download</param> /// <param name="fileStorage">The storage to download the refernce into</param> /// <param name="progressHost">The progressHost to provide feedback to</param> private void DownloadReference(ReferenceToDownload reference, FileBasedSiteStorage fileStorage, IProgressHost progressHost) { UrlDownloadToFile downloader; string fullPath; downloader = new UrlDownloadToFile(); downloader.TimeoutMs = 30000; if (progressHost.CancelRequested) throw new OperationCancelledException(); // make sure that the directory exists fullPath = Path.Combine(fileStorage.BasePath, reference.RelativePath); string directory = Path.GetDirectoryName(fullPath); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); // Make sure there aren't any conflicts lock (this) { string newFileName = Path.GetFileName(fullPath); do { fullPath = PathHelper.GetNonConflictingPath(fullPath); newFileName = Path.GetFileName(fullPath); reference.SetFileName(ref newFileName); } while (newFileName != Path.GetFileName(fullPath)); using (File.Open(fullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { } } string downloadUrl = reference.AbsoluteUrl; if (UrlHelper.IsFileUrl(downloadUrl)) downloadUrl = HttpUtility.UrlDecode(downloadUrl); downloader.Url = downloadUrl.Trim(); downloader.FilePath = fullPath; downloader.ShowSecurityUI = true; if (CredentialsContext != null) downloader.CredentialsContext = CredentialsContext; try { downloader.Download(progressHost); } catch (COMException e) { // If the file couldn't be downloaded, this doesn't matter. But log it Trace.WriteLine("Didn't download file: " + downloader.Url + " " + e.ToString()); } // Fix the filename of the downloaded entity to have the correct extension string contentType = downloader.ContentType; if (contentType != null && File.Exists(fullPath)) { string suggestedExtension = MimeHelper.GetExtensionFromContentType(contentType); if (suggestedExtension == null) suggestedExtension = UrlHelper.GetExtensionForUrl(downloader.FinalUrl); if (Path.GetExtension(fullPath) != suggestedExtension) { string newFilePath = Path.ChangeExtension(fullPath, suggestedExtension); string newFileName = Path.GetFileName(newFilePath); // Try to reset the name until we can both agree while (true) { newFilePath = PathHelper.GetNonConflictingPath(newFilePath); newFileName = Path.GetFileName(newFilePath); FileHelper.Rename(fullPath, newFilePath); reference.SetFileName(ref newFileName); if (newFileName != Path.GetFileName(newFilePath)) { try { File.Delete(newFilePath); } catch (Exception e) { Debug.Fail("Unable to delete failed temp file: " + e.ToString()); } } else { break; } } fullPath = newFilePath; } } // Handle anything special we need to do for stylesheet and js file references if (Path.GetExtension(fullPath) == ".css" && File.Exists(fullPath)) { string fileContents = string.Empty; using (StreamReader reader = new StreamReader(fullPath)) fileContents = reader.ReadToEnd(); if (fileContents != string.Empty) { LightWeightCSSReplacer cssReplacer = new LightWeightCSSReplacer(fileContents); // fix up references foreach (ReferenceToDownload referenceInfo in _referencesToDownload.Values) cssReplacer.AddUrlToReplace(new UrlToReplace(referenceInfo.UrlToReplace, referenceInfo.FileName)); string newCss = cssReplacer.DoReplace(); using (StreamWriter writer = new StreamWriter(fullPath, false)) writer.Write(newCss); } } }
/// <summary> /// Download a reference, providing progress /// </summary> /// <param name="reference">The reference to download</param> /// <param name="fileStorage">The storage to download the reference into</param> /// <param name="progressHost">The progressHost to provide feedback to</param> private void DownloadReference(ReferenceToDownload reference, FileBasedSiteStorage fileStorage, IProgressHost progressHost) { if (IsBase64EmbededImage(reference.AbsoluteUrl)) { return; } UrlDownloadToFile downloader; string fullPath; downloader = new UrlDownloadToFile(); downloader.TimeoutMs = 30000; if (progressHost.CancelRequested) { throw new OperationCancelledException(); } // make sure that the directory exists fullPath = Path.Combine(fileStorage.BasePath, reference.RelativePath); string directory = Path.GetDirectoryName(fullPath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // Make sure there aren't any conflicts lock (this) { string newFileName = Path.GetFileName(fullPath); do { fullPath = PathHelper.GetNonConflictingPath(fullPath); newFileName = Path.GetFileName(fullPath); reference.SetFileName(ref newFileName); } while (newFileName != Path.GetFileName(fullPath)); using (File.Open(fullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { } } string downloadUrl = reference.AbsoluteUrl; if (UrlHelper.IsFileUrl(downloadUrl)) { downloadUrl = HttpUtility.UrlDecode(downloadUrl); } downloader.Url = downloadUrl.Trim(); downloader.FilePath = fullPath; downloader.ShowSecurityUI = true; if (CredentialsContext != null) { downloader.CredentialsContext = CredentialsContext; } try { downloader.Download(progressHost); } catch (COMException e) { // If the file couldn't be downloaded, this doesn't matter. But log it Trace.WriteLine("Didn't download file: " + downloader.Url + " " + e.ToString()); } // Fix the filename of the downloaded entity to have the correct extension string contentType = downloader.ContentType; if (contentType != null && File.Exists(fullPath)) { string suggestedExtension = MimeHelper.GetExtensionFromContentType(contentType); if (suggestedExtension == null) { suggestedExtension = UrlHelper.GetExtensionForUrl(downloader.FinalUrl); } if (Path.GetExtension(fullPath) != suggestedExtension) { string newFilePath = Path.ChangeExtension(fullPath, suggestedExtension); string newFileName = Path.GetFileName(newFilePath); // Try to reset the name until we can both agree while (true) { newFilePath = PathHelper.GetNonConflictingPath(newFilePath); newFileName = Path.GetFileName(newFilePath); FileHelper.Rename(fullPath, newFilePath); reference.SetFileName(ref newFileName); if (newFileName != Path.GetFileName(newFilePath)) { try { File.Delete(newFilePath); } catch (Exception e) { Debug.Fail("Unable to delete failed temp file: " + e.ToString()); } } else { break; } } fullPath = newFilePath; } } // Handle anything special we need to do for stylesheet and js file references if (Path.GetExtension(fullPath) == ".css" && File.Exists(fullPath)) { string fileContents = string.Empty; using (StreamReader reader = new StreamReader(fullPath)) fileContents = reader.ReadToEnd(); if (fileContents != string.Empty) { LightWeightCSSReplacer cssReplacer = new LightWeightCSSReplacer(fileContents); // fix up references foreach (ReferenceToDownload referenceInfo in _referencesToDownload.Values) { cssReplacer.AddUrlToReplace(new UrlToReplace(referenceInfo.UrlToReplace, referenceInfo.FileName)); } string newCss = cssReplacer.DoReplace(); using (StreamWriter writer = new StreamWriter(fullPath, false)) writer.Write(newCss); } } }
private static Stream DoDownload(UrlDownloadToFile urlDownloadToFile) { string path = TempFileManager.Instance.CreateTempFile("urldownload"); urlDownloadToFile.FilePath = path; urlDownloadToFile.Download(SilentProgressHost.Instance); return new FileStream(path, FileMode.Open, FileAccess.Read); }