private string fetchSingleFile(string url) { using (WebClient webClient = new WebDownload(_timeout)) { try { webClient.UseDefaultCredentials = true; webClient.Proxy = WebRequest.GetSystemWebProxy(); webClient.Headers.Add("user-agent", @"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); var bytes = webClient.DownloadData(url); var totalBytes = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]); if (bytes.Length < totalBytes) { throw new Exception("Download incompleted - received only " + bytes.Length + " bytes but expecting " + totalBytes + "bytes."); } var contentType = webClient.ResponseHeaders["Content-Type"]; if (contentType == null) { throw new Exception("Got NULL content type"); } var postfix = RandomFileNameUtils.GetFilePostfixFromContentType(contentType); var tempFilePath = _mainControl.FileStorage.GetTempFilePath(postfix); using (var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); } return(tempFilePath); } catch (Exception e) { Trace.WriteLine(string.Format("Exception {0} while downloading {1}", e, url)); return(null); } } }
public string GetTempFilePath(string postfix) { if (_mainControl == null) { Trace.WriteLine("Error: function called before initialize."); throw new ResourceNotInitializedException(this.GetType().Name + ": function " + new StackTrace().GetFrame(1).GetMethod().Name + " called without inialization."); } return(RandomFileNameUtils.GetTempFileName(_tempFileFolder, postfix)); }
private string getRandomFileName(string postfix) { var randomFileName = RandomFileNameUtils.GetRandomFileName(); var directoryPath = getDirectoryPath(randomFileName); while (File.Exists(directoryPath) || Directory.Exists(directoryPath)) { randomFileName = RandomFileNameUtils.GetRandomFileName(); directoryPath = getDirectoryPath(randomFileName); } return(randomFileName + postfix); }