mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
fixed #6460: Get real file name from web server before download
This commit is contained in:
parent
8d20db7443
commit
c6384b59a5
2 changed files with 42 additions and 0 deletions
|
@ -30,6 +30,7 @@ You should have received a copy of the GNU General Public License
|
|||
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('/');
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue