Better url file checking

This commit is contained in:
Jaex 2014-05-07 19:55:42 +03:00
parent b2fdc2a9fd
commit 65e07b618c
3 changed files with 36 additions and 12 deletions

View file

@ -854,5 +854,18 @@ public static Size MeasureText(string text, Font font, int width)
return g.MeasureString(text, font, width).ToSize(); return g.MeasureString(text, font, width).ToSize();
} }
} }
public static string GetURLFilename(string url)
{
Uri uri = new Uri(url);
try
{
return Path.GetFileName(uri.LocalPath);
}
catch { }
return null;
}
} }
} }

View file

@ -1630,9 +1630,9 @@ private void InitializeComponent()
this.chkClipboardUploadContents.AutoSize = true; this.chkClipboardUploadContents.AutoSize = true;
this.chkClipboardUploadContents.Location = new System.Drawing.Point(16, 16); this.chkClipboardUploadContents.Location = new System.Drawing.Point(16, 16);
this.chkClipboardUploadContents.Name = "chkClipboardUploadContents"; this.chkClipboardUploadContents.Name = "chkClipboardUploadContents";
this.chkClipboardUploadContents.Size = new System.Drawing.Size(345, 17); this.chkClipboardUploadContents.Size = new System.Drawing.Size(308, 17);
this.chkClipboardUploadContents.TabIndex = 3; this.chkClipboardUploadContents.TabIndex = 3;
this.chkClipboardUploadContents.Text = "If clipboard contains a image/text URL then download it and upload"; this.chkClipboardUploadContents.Text = "If clipboard contains a file URL then download it and upload";
this.chkClipboardUploadContents.UseVisualStyleBackColor = true; this.chkClipboardUploadContents.UseVisualStyleBackColor = true;
this.chkClipboardUploadContents.CheckedChanged += new System.EventHandler(this.chkClipboardUploadContents_CheckedChanged); this.chkClipboardUploadContents.CheckedChanged += new System.EventHandler(this.chkClipboardUploadContents_CheckedChanged);
// //

View file

@ -173,17 +173,28 @@ public static void ClipboardUpload(TaskSettings taskSettings = null)
if (!string.IsNullOrEmpty(text)) if (!string.IsNullOrEmpty(text))
{ {
string url = text.Trim(); string url = text.Trim();
bool isURL = Helpers.IsValidURLRegex(url);
if (taskSettings.UploadSettings.ClipboardUploadURLContents && isURL && (Helpers.IsImageFile(url) || Helpers.IsTextFile(url))) if (Helpers.IsValidURLRegex(url))
{ {
DownloadAndUploadFile(url, taskSettings); if (taskSettings.UploadSettings.ClipboardUploadURLContents)
{
string filename = Helpers.GetURLFilename(url);
if (!string.IsNullOrEmpty(filename))
{
DownloadAndUploadFile(url, filename, taskSettings);
return;
}
}
if (taskSettings.UploadSettings.ClipboardUploadShortenURL)
{
ShortenURL(url, taskSettings);
return;
}
} }
else if (taskSettings.UploadSettings.ClipboardUploadShortenURL && isURL)
{ if (taskSettings.UploadSettings.ClipboardUploadAutoIndexFolder && text.Length <= 260 && Directory.Exists(text))
ShortenURL(url, taskSettings);
}
else if (taskSettings.UploadSettings.ClipboardUploadAutoIndexFolder && text.Length <= 260 && Directory.Exists(text))
{ {
IndexFolder(text, taskSettings); IndexFolder(text, taskSettings);
} }
@ -319,7 +330,7 @@ public static void ShortenURL(string url, TaskSettings taskSettings = null)
} }
} }
public static void DownloadAndUploadFile(string url, TaskSettings taskSettings = null) public static void DownloadAndUploadFile(string url, string filename, TaskSettings taskSettings = null)
{ {
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings(); if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
@ -327,7 +338,7 @@ public static void DownloadAndUploadFile(string url, TaskSettings taskSettings =
Helpers.AsyncJob(() => Helpers.AsyncJob(() =>
{ {
downloadPath = TaskHelpers.CheckFilePath(taskSettings.CaptureFolder, Path.GetFileName(url), taskSettings); downloadPath = TaskHelpers.CheckFilePath(taskSettings.CaptureFolder, filename, taskSettings);
using (WebClient wc = new WebClient()) using (WebClient wc = new WebClient())
{ {