mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
URL related refactoring
This commit is contained in:
parent
1901a73eb5
commit
e53275a3e9
12 changed files with 62 additions and 71 deletions
|
@ -211,16 +211,6 @@ public static string Truncate(this string str, int maxLength, string endings, bo
|
|||
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)
|
||||
{
|
||||
byte[] bytes = new byte[hex.Length / 2];
|
||||
|
|
|
@ -173,59 +173,55 @@ public static string CombineURL(params string[] urls)
|
|||
return urls.Aggregate(CombineURL);
|
||||
}
|
||||
|
||||
public static bool IsValidURL(string url)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
url = url.Trim();
|
||||
return !url.StartsWith("file://") && Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsValidURLRegex(string url)
|
||||
public static bool IsValidURL(string url, bool useRegex = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url)) return false;
|
||||
|
||||
// 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*)?" +
|
||||
"$";
|
||||
url = url.Trim();
|
||||
|
||||
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)
|
||||
|
|
|
@ -73,7 +73,7 @@ private void CheckClipboardURL()
|
|||
{
|
||||
string text = Clipboard.GetText();
|
||||
|
||||
if (!string.IsNullOrEmpty(text) && URLHelpers.IsValidURLRegex(text))
|
||||
if (URLHelpers.IsValidURL(text))
|
||||
{
|
||||
txtURL.Text = text;
|
||||
}
|
||||
|
|
|
@ -223,7 +223,7 @@ private string GetRegion()
|
|||
return serviceAndRegion.Substring(separatorIndex + 1);
|
||||
}
|
||||
|
||||
public string GetUploadPath(string fileName)
|
||||
private string GetUploadPath(string fileName)
|
||||
{
|
||||
string path = NameParser.Parse(NameParserType.FolderPath, Settings.ObjectPrefix.Trim('/'));
|
||||
return URLHelpers.CombineURL(path, fileName);
|
||||
|
@ -250,6 +250,12 @@ public string GenerateURL(string uploadPath)
|
|||
return "";
|
||||
}
|
||||
|
||||
public string GetPreviewURL()
|
||||
{
|
||||
string uploadPath = GetUploadPath("example.png");
|
||||
return GenerateURL(uploadPath);
|
||||
}
|
||||
|
||||
private string CreateCanonicalHeaders(NameValueCollection headers)
|
||||
{
|
||||
string result = "";
|
||||
|
|
|
@ -166,7 +166,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
UploadResult result = new UploadResult();
|
||||
|
||||
string subFolderPath = Account.GetSubFolderPath(null, NameParserType.FolderPath);
|
||||
string path = subFolderPath.CombineURL(fileName);
|
||||
string path = URLHelpers.CombineURL(subFolderPath, fileName);
|
||||
string url = Account.GetUriPath(fileName, subFolderPath);
|
||||
|
||||
OnEarlyURLCopyRequested(url);
|
||||
|
|
|
@ -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")]
|
||||
public string PreviewFtpPath => GetFtpPath(exampleFilename);
|
||||
|
|
|
@ -53,7 +53,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
UploadResult result = new UploadResult();
|
||||
|
||||
string subFolderPath = Account.GetSubFolderPath();
|
||||
string path = subFolderPath.CombineURL(fileName);
|
||||
string path = URLHelpers.CombineURL(subFolderPath, fileName);
|
||||
string url = Account.GetUriPath(fileName, subFolderPath);
|
||||
|
||||
OnEarlyURLCopyRequested(url);
|
||||
|
|
|
@ -492,8 +492,7 @@ public void DropboxAuthComplete(string code)
|
|||
|
||||
private void UpdateAmazonS3Status()
|
||||
{
|
||||
var s3 = new AmazonS3(Config.AmazonS3Settings);
|
||||
lblAmazonS3PathPreview.Text = s3.GenerateURL(s3.GetUploadPath("Example.png"));
|
||||
lblAmazonS3PathPreview.Text = new AmazonS3(Config.AmazonS3Settings).GetPreviewURL();
|
||||
}
|
||||
|
||||
#endregion Amazon S3
|
||||
|
|
|
@ -123,7 +123,7 @@ public override UploadResult UploadText(string text, string fileName)
|
|||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -756,7 +756,7 @@ public void UseCommandLineArgs(List<CLICommand> commands)
|
|||
continue;
|
||||
}
|
||||
|
||||
if (URLHelpers.IsValidURLRegex(command.Command))
|
||||
if (URLHelpers.IsValidURL(command.Command))
|
||||
{
|
||||
UploadManager.DownloadAndUploadFile(command.Command, taskSettings);
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ public QRCodeForm(string text = null)
|
|||
{
|
||||
text = Clipboard.GetText();
|
||||
|
||||
if (!string.IsNullOrEmpty(text) && URLHelpers.IsValidURLRegex(text))
|
||||
if (URLHelpers.IsValidURL(text))
|
||||
{
|
||||
txtQRCode.Text = text;
|
||||
}
|
||||
|
|
|
@ -168,7 +168,7 @@ public static void ClipboardUpload(TaskSettings taskSettings = null)
|
|||
{
|
||||
string url = text.Trim();
|
||||
|
||||
if (URLHelpers.IsValidURLRegex(url))
|
||||
if (URLHelpers.IsValidURL(url))
|
||||
{
|
||||
if (taskSettings.UploadSettings.ClipboardUploadURLContents)
|
||||
{
|
||||
|
@ -276,7 +276,7 @@ public static void UploadURL(TaskSettings taskSettings = null)
|
|||
{
|
||||
string text = Clipboard.GetText();
|
||||
|
||||
if (!string.IsNullOrEmpty(text) && URLHelpers.IsValidURLRegex(text))
|
||||
if (URLHelpers.IsValidURL(text))
|
||||
{
|
||||
inputText = text;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue