URL related refactoring

This commit is contained in:
Jaex 2017-04-24 17:01:35 +03:00
parent 1901a73eb5
commit e53275a3e9
12 changed files with 62 additions and 71 deletions

View file

@ -211,16 +211,6 @@ public static string Truncate(this string str, int maxLength, string endings, bo
return str; return str;
} }
public static bool IsValidUrl(this string url)
{
return Uri.IsWellFormedUriString(url.Trim(), UriKind.Absolute);
}
public static string CombineURL(this string url, string url2)
{
return URLHelpers.CombineURL(url, url2);
}
public static byte[] HexToBytes(this string hex) public static byte[] HexToBytes(this string hex)
{ {
byte[] bytes = new byte[hex.Length / 2]; byte[] bytes = new byte[hex.Length / 2];

View file

@ -173,59 +173,55 @@ public static string CombineURL(params string[] urls)
return urls.Aggregate(CombineURL); return urls.Aggregate(CombineURL);
} }
public static bool IsValidURL(string url) public static bool IsValidURL(string url, bool useRegex = true)
{
if (!string.IsNullOrEmpty(url))
{
url = url.Trim();
return !url.StartsWith("file://") && Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
}
return false;
}
public static bool IsValidURLRegex(string url)
{ {
if (string.IsNullOrEmpty(url)) return false; if (string.IsNullOrEmpty(url)) return false;
// https://gist.github.com/729294 url = url.Trim();
string pattern =
"^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" +
"(?:" +
// IP address exclusion
// private & local networks
"(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
"|" +
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
// TLD may end with dot
"\\.?" +
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:[/?#]\\S*)?" +
"$";
return Regex.IsMatch(url.Trim(), pattern, RegexOptions.IgnoreCase); if (useRegex)
{
// https://gist.github.com/729294
string pattern =
"^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" +
"(?:" +
// IP address exclusion
// private & local networks
"(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
"|" +
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
// TLD may end with dot
"\\.?" +
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:[/?#]\\S*)?" +
"$";
return Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase);
}
return !url.StartsWith("file://") && Uri.IsWellFormedUriString(url, UriKind.Absolute);
} }
public static string AddSlash(string url, SlashType slashType) public static string AddSlash(string url, SlashType slashType)

View file

@ -73,7 +73,7 @@ private void CheckClipboardURL()
{ {
string text = Clipboard.GetText(); string text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text) && URLHelpers.IsValidURLRegex(text)) if (URLHelpers.IsValidURL(text))
{ {
txtURL.Text = text; txtURL.Text = text;
} }

View file

@ -223,7 +223,7 @@ private string GetRegion()
return serviceAndRegion.Substring(separatorIndex + 1); return serviceAndRegion.Substring(separatorIndex + 1);
} }
public string GetUploadPath(string fileName) private string GetUploadPath(string fileName)
{ {
string path = NameParser.Parse(NameParserType.FolderPath, Settings.ObjectPrefix.Trim('/')); string path = NameParser.Parse(NameParserType.FolderPath, Settings.ObjectPrefix.Trim('/'));
return URLHelpers.CombineURL(path, fileName); return URLHelpers.CombineURL(path, fileName);
@ -250,6 +250,12 @@ public string GenerateURL(string uploadPath)
return ""; return "";
} }
public string GetPreviewURL()
{
string uploadPath = GetUploadPath("example.png");
return GenerateURL(uploadPath);
}
private string CreateCanonicalHeaders(NameValueCollection headers) private string CreateCanonicalHeaders(NameValueCollection headers)
{ {
string result = ""; string result = "";

View file

@ -166,7 +166,7 @@ public override UploadResult Upload(Stream stream, string fileName)
UploadResult result = new UploadResult(); UploadResult result = new UploadResult();
string subFolderPath = Account.GetSubFolderPath(null, NameParserType.FolderPath); string subFolderPath = Account.GetSubFolderPath(null, NameParserType.FolderPath);
string path = subFolderPath.CombineURL(fileName); string path = URLHelpers.CombineURL(subFolderPath, fileName);
string url = Account.GetUriPath(fileName, subFolderPath); string url = Account.GetUriPath(fileName, subFolderPath);
OnEarlyURLCopyRequested(url); OnEarlyURLCopyRequested(url);

View file

@ -99,7 +99,7 @@ public string FTPAddress
} }
} }
private string exampleFilename = "screenshot.png"; private string exampleFilename = "example.png";
[Category("FTP"), Description("Preview of the FTP path based on the settings above")] [Category("FTP"), Description("Preview of the FTP path based on the settings above")]
public string PreviewFtpPath => GetFtpPath(exampleFilename); public string PreviewFtpPath => GetFtpPath(exampleFilename);

View file

@ -53,7 +53,7 @@ public override UploadResult Upload(Stream stream, string fileName)
UploadResult result = new UploadResult(); UploadResult result = new UploadResult();
string subFolderPath = Account.GetSubFolderPath(); string subFolderPath = Account.GetSubFolderPath();
string path = subFolderPath.CombineURL(fileName); string path = URLHelpers.CombineURL(subFolderPath, fileName);
string url = Account.GetUriPath(fileName, subFolderPath); string url = Account.GetUriPath(fileName, subFolderPath);
OnEarlyURLCopyRequested(url); OnEarlyURLCopyRequested(url);

View file

@ -492,8 +492,7 @@ public void DropboxAuthComplete(string code)
private void UpdateAmazonS3Status() private void UpdateAmazonS3Status()
{ {
var s3 = new AmazonS3(Config.AmazonS3Settings); lblAmazonS3PathPreview.Text = new AmazonS3(Config.AmazonS3Settings).GetPreviewURL();
lblAmazonS3PathPreview.Text = s3.GenerateURL(s3.GetUploadPath("Example.png"));
} }
#endregion Amazon S3 #endregion Amazon S3

View file

@ -123,7 +123,7 @@ public override UploadResult UploadText(string text, string fileName)
ur.Response = SendRequestMultiPart("https://pastebin.com/api/api_post.php", args); ur.Response = SendRequestMultiPart("https://pastebin.com/api/api_post.php", args);
if (!string.IsNullOrEmpty(ur.Response) && !ur.Response.StartsWith("Bad API request") && ur.Response.IsValidUrl()) if (URLHelpers.IsValidURL(ur.Response))
{ {
if (Settings.RawURL) if (Settings.RawURL)
{ {

View file

@ -756,7 +756,7 @@ public void UseCommandLineArgs(List<CLICommand> commands)
continue; continue;
} }
if (URLHelpers.IsValidURLRegex(command.Command)) if (URLHelpers.IsValidURL(command.Command))
{ {
UploadManager.DownloadAndUploadFile(command.Command, taskSettings); UploadManager.DownloadAndUploadFile(command.Command, taskSettings);
} }

View file

@ -60,7 +60,7 @@ public QRCodeForm(string text = null)
{ {
text = Clipboard.GetText(); text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text) && URLHelpers.IsValidURLRegex(text)) if (URLHelpers.IsValidURL(text))
{ {
txtQRCode.Text = text; txtQRCode.Text = text;
} }

View file

@ -168,7 +168,7 @@ public static void ClipboardUpload(TaskSettings taskSettings = null)
{ {
string url = text.Trim(); string url = text.Trim();
if (URLHelpers.IsValidURLRegex(url)) if (URLHelpers.IsValidURL(url))
{ {
if (taskSettings.UploadSettings.ClipboardUploadURLContents) if (taskSettings.UploadSettings.ClipboardUploadURLContents)
{ {
@ -276,7 +276,7 @@ public static void UploadURL(TaskSettings taskSettings = null)
{ {
string text = Clipboard.GetText(); string text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text) && URLHelpers.IsValidURLRegex(text)) if (URLHelpers.IsValidURL(text))
{ {
inputText = text; inputText = text;
} }