From 3ce053da364ad1536b2bcc9b7a0983249529d881 Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 31 Aug 2015 09:20:20 +0300 Subject: [PATCH] Make sure DownloadAndUpload task creates valid filename --- ShareX.HelpersLib/Helpers/URLHelpers.cs | 27 +++++++++++++------------ ShareX/WorkerTask.cs | 8 ++++++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/URLHelpers.cs b/ShareX.HelpersLib/Helpers/URLHelpers.cs index bf1858ae1..b98a0ef3b 100644 --- a/ShareX.HelpersLib/Helpers/URLHelpers.cs +++ b/ShareX.HelpersLib/Helpers/URLHelpers.cs @@ -27,7 +27,6 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Diagnostics; using System.Globalization; -using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -123,6 +122,19 @@ public static string HtmlEncode(string text) return result.ToString(); } + public static string URLDecode(string url, int count = 1) + { + string temp = null; + + for (int i = 0; i < count && url != temp; i++) + { + temp = url; + url = HttpUtility.UrlDecode(url); + } + + return url; + } + public static string CombineURL(string url1, string url2) { bool url1Empty = string.IsNullOrEmpty(url1); @@ -251,19 +263,8 @@ public static string AddSlash(string url, SlashType slashType, int count) return url; } - public static string GetFileName(string path, bool urlDecode = false) + public static string GetFileName(string path) { - if (urlDecode) - { - string tempPath = null; - - for (int i = 0; i < 10 && path != tempPath; i++) - { - tempPath = path; - path = HttpUtility.UrlDecode(path); - } - } - if (path.Contains('/')) { path = path.Substring(path.LastIndexOf('/') + 1); diff --git a/ShareX/WorkerTask.cs b/ShareX/WorkerTask.cs index 489361f73..9e477f5be 100644 --- a/ShareX/WorkerTask.cs +++ b/ShareX/WorkerTask.cs @@ -191,13 +191,17 @@ public static WorkerTask CreateDownloadUploadTask(string url, TaskSettings taskS WorkerTask task = new WorkerTask(taskSettings); task.Info.Job = TaskJob.DownloadUpload; task.Info.DataType = TaskHelpers.FindDataType(url, taskSettings); - task.Info.FileName = URLHelpers.GetFileName(url, true); - if (string.IsNullOrEmpty(task.Info.FileName)) + string filename = URLHelpers.URLDecode(url, 10); + filename = URLHelpers.GetFileName(filename); + filename = Helpers.GetValidFileName(filename); + + if (string.IsNullOrEmpty(filename)) { return null; } + task.Info.FileName = filename; task.Info.Result.URL = url; return task; }