Make sure DownloadAndUpload task creates valid filename

This commit is contained in:
Jaex 2015-08-31 09:20:20 +03:00
parent 9933cf8110
commit 3ce053da36
2 changed files with 20 additions and 15 deletions

View file

@ -27,7 +27,6 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -123,6 +122,19 @@ public static string HtmlEncode(string text)
return result.ToString(); 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) public static string CombineURL(string url1, string url2)
{ {
bool url1Empty = string.IsNullOrEmpty(url1); bool url1Empty = string.IsNullOrEmpty(url1);
@ -251,19 +263,8 @@ public static string AddSlash(string url, SlashType slashType, int count)
return url; 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('/')) if (path.Contains('/'))
{ {
path = path.Substring(path.LastIndexOf('/') + 1); path = path.Substring(path.LastIndexOf('/') + 1);

View file

@ -191,13 +191,17 @@ public static WorkerTask CreateDownloadUploadTask(string url, TaskSettings taskS
WorkerTask task = new WorkerTask(taskSettings); WorkerTask task = new WorkerTask(taskSettings);
task.Info.Job = TaskJob.DownloadUpload; task.Info.Job = TaskJob.DownloadUpload;
task.Info.DataType = TaskHelpers.FindDataType(url, taskSettings); 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; return null;
} }
task.Info.FileName = filename;
task.Info.Result.URL = url; task.Info.Result.URL = url;
return task; return task;
} }