/// <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); } } }