public static GenerateWindowsSafeFilename ( string fileNameIn ) : string | ||
fileNameIn | string | |
return | string |
/// <summary> /// Downloads a file /// </summary> /// <param name="urlDownload"></param> /// <param name="downloadToDirectory"></param> /// <param name="baseFileName">Filename without extension</param> /// <returns>The path to the downloaded file</returns> private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper) { //[2016-05-06] Interestingly 'GetFileNameWithoutExtension' does more than remove a ".xxxx" extension; it will also remove a preceding // path (e.g. GetFileNameWithoutExtension('foo/bar.xxx') -> "bar'. This is undesirable because these characters are valid // in Tableau Server content names. Since this function is supposed to be called with a 'baseFilename' that DOES NOT have a .xxx // extension, it is safe to remove this call //baseFilename = FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename)); //Strip off an extension if its there baseFilename = FileIOHelper.GenerateWindowsSafeFilename(baseFilename); var webClient = this.CreateLoggedInWebClient(); using (webClient) { //Choose a temp file name to download to var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp"); _onlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10); webClient.DownloadFile(urlDownload, starterName); //Download the file //Look up the correct file extension based on the content type downloaded var contentType = webClient.ResponseHeaders["Content-Type"]; var fileExtension = downloadTypeMapper.GetFileExtension(contentType); var finishName = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension); //Rename the downloaded file System.IO.File.Move(starterName, finishName); return(finishName); } }
/// <summary> /// Downloads a file /// </summary> /// <param name="urlDownload"></param> /// <param name="downloadToDirectory"></param> /// <param name="baseFileName">Filename without extension</param> /// <returns>The path to the downloaded file</returns> private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper) { //Strip off an extension if its there baseFilename = FileIOHelper.GenerateWindowsSafeFilename(baseFilename); var webClient = CreateLoggedInWebClient(); using (webClient) { //Choose a temp file name to download to var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp"); Login.Logger.Information("Attempting file download: " + urlDownload); using (HttpResponseMessage response = webClient.GetAsync(urlDownload, HttpCompletionOption.ResponseHeadersRead).Result) { response.EnsureSuccessStatusCode(); using ( Stream contentStream = response.Content.ReadAsStreamAsync().Result, fileStream = new FileStream(starterName, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true)) { var buffer = new byte[8192]; var isMoreToRead = true; do { var read = contentStream.ReadAsync(buffer, 0, buffer.Length).Result; if (read == 0) { isMoreToRead = false; } else { fileStream.WriteAsync(buffer, 0, read); } } while (isMoreToRead); } //Look up the correct file extension based on the content type downloaded var contentType = response.Content.Headers.ContentType.ToString(); var fileExtension = downloadTypeMapper.GetFileExtension(contentType); var finishName = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension); //Rename the downloaded file System.IO.File.Move(starterName, finishName); return(finishName); } } }
/// <summary> /// Returns a file system path we can use to store information about the specified site /// </summary> /// <param name="siteUrl"></param> /// <returns></returns> private string GeneratePathFromSiteUrl(TableauServerUrls siteUrl) { string appPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TabMigrate"); //Add the server name to the path appPath = Path.Combine(appPath, FileIOHelper.GenerateWindowsSafeFilename(siteUrl.ServerName)); //Add the site name to the path string siteName = siteUrl.SiteUrlSegement; if (!string.IsNullOrEmpty(siteName)) { appPath = Path.Combine(appPath, siteName); } FileIOHelper.CreatePathIfNeeded(appPath); return(appPath); }
/// <summary> /// Downloads a file /// </summary> /// <param name="urlDownload"></param> /// <param name="downloadToDirectory"></param> /// <param name="baseFileName">Filename without extension</param> /// <returns>The path to the downloaded file</returns> private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper) { //Strip off an extension if its there baseFilename = FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename)); var webClient = this.CreateLoggedInWebClient(); using (webClient) { //Choose a temp file name to download to var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp"); _onlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10); webClient.DownloadFile(urlDownload, starterName); //Download the file //Look up the correct file extension based on the content type downloaded var contentType = webClient.ResponseHeaders["Content-Type"]; var fileExtension = downloadTypeMapper.GetFileExtension(contentType); var finishName = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension); //Rename the downloaded file System.IO.File.Move(starterName, finishName); return(finishName); } }