fixed #6460: Get real file name from web server before download

This commit is contained in:
Jaex 2022-08-25 07:43:40 +03:00
parent 8d20db7443
commit c6384b59a5
2 changed files with 42 additions and 0 deletions

View file

@ -30,6 +30,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
@ -366,6 +367,37 @@ public static string GetFileName(string path)
return path;
}
public static string GetFileNameFromWebServer(string url)
{
string fileName = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.UserAgent = ShareXResources.UserAgent;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
string contentDisposition = response.Headers["Content-Disposition"];
if (!string.IsNullOrEmpty(contentDisposition))
{
string fileNameMarker = "filename=\"";
int beginIndex = contentDisposition.IndexOf(fileNameMarker, StringComparison.OrdinalIgnoreCase);
contentDisposition = contentDisposition.Substring(beginIndex + fileNameMarker.Length);
int fileNameLength = contentDisposition.IndexOf("\"");
fileName = contentDisposition.Substring(0, fileNameLength);
}
}
}
catch
{
}
return fileName;
}
public static bool IsFileURL(string url)
{
int index = url.LastIndexOf('/');

View file

@ -1052,6 +1052,16 @@ private bool DownloadFromURL(bool upload)
string url = Info.Result.URL.Trim();
Info.Result.URL = "";
if (!Info.TaskSettings.UploadSettings.FileUploadUseNamePattern)
{
string fileName = URLHelpers.GetFileNameFromWebServer(url);
if (!string.IsNullOrEmpty(fileName))
{
Info.FileName = FileHelpers.SanitizeFileName(fileName);
}
}
string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings);
Info.FilePath = TaskHelpers.HandleExistsFile(screenshotsFolder, Info.FileName, Info.TaskSettings);