mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Code refactoring
This commit is contained in:
parent
c6384b59a5
commit
a73193adfe
16 changed files with 134 additions and 173 deletions
|
@ -249,7 +249,16 @@ private async void tsmiImportURL_Click(object sender, EventArgs e)
|
|||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
json = Helpers.DownloadString(url);
|
||||
try
|
||||
{
|
||||
json = URLHelpers.DownloadString(url);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DebugHelper.WriteException(ex);
|
||||
MessageBox.Show(Resources.Helpers_DownloadString_Download_failed_ + "\r\n" + ex, "ShareX - " + Resources.Error,
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
});
|
||||
|
||||
OnImportRequested(json);
|
||||
|
|
|
@ -34,7 +34,6 @@ You should have received a copy of the GNU General Public License
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Media;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
|
@ -543,30 +542,6 @@ public static string SendPing(string host, int count)
|
|||
return string.Join(", ", status);
|
||||
}
|
||||
|
||||
public static string DownloadString(string url)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
try
|
||||
{
|
||||
using (WebClient wc = new WebClient())
|
||||
{
|
||||
wc.Encoding = Encoding.UTF8;
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
return wc.DownloadString(url);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugHelper.WriteException(e);
|
||||
MessageBox.Show(Resources.Helpers_DownloadString_Download_failed_ + "\r\n" + e, "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void SetDefaultUICulture(CultureInfo culture)
|
||||
{
|
||||
Type type = typeof(CultureInfo);
|
||||
|
|
|
@ -31,6 +31,7 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
@ -367,37 +368,6 @@ public static string GetFileName(string path)
|
|||
return path;
|
||||
}
|
||||
|
||||
public static string GetFileNameFromWebServer(string url)
|
||||
{
|
||||
string fileName = null;
|
||||
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = "HEAD";
|
||||
request.UserAgent = ShareXResources.UserAgent;
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
string contentDisposition = response.Headers["Content-Disposition"];
|
||||
|
||||
if (!string.IsNullOrEmpty(contentDisposition))
|
||||
{
|
||||
string fileNameMarker = "filename=\"";
|
||||
int beginIndex = contentDisposition.IndexOf(fileNameMarker, StringComparison.OrdinalIgnoreCase);
|
||||
contentDisposition = contentDisposition.Substring(beginIndex + fileNameMarker.Length);
|
||||
int fileNameLength = contentDisposition.IndexOf("\"");
|
||||
fileName = contentDisposition.Substring(0, fileNameLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public static bool IsFileURL(string url)
|
||||
{
|
||||
int index = url.LastIndexOf('/');
|
||||
|
@ -608,5 +578,77 @@ public static string BuildUri(string root, string path, string query = null)
|
|||
builder.Query = query;
|
||||
return builder.Uri.AbsoluteUri;
|
||||
}
|
||||
|
||||
public static string GetFileNameFromWebServer(string url)
|
||||
{
|
||||
string fileName = null;
|
||||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = "HEAD";
|
||||
IWebProxy proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
if (proxy != null) request.Proxy = proxy;
|
||||
request.UserAgent = ShareXResources.UserAgent;
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
string contentDisposition = response.Headers["Content-Disposition"];
|
||||
|
||||
if (!string.IsNullOrEmpty(contentDisposition))
|
||||
{
|
||||
string fileNameMarker = "filename=\"";
|
||||
int beginIndex = contentDisposition.IndexOf(fileNameMarker, StringComparison.OrdinalIgnoreCase);
|
||||
contentDisposition = contentDisposition.Substring(beginIndex + fileNameMarker.Length);
|
||||
int fileNameLength = contentDisposition.IndexOf("\"");
|
||||
fileName = contentDisposition.Substring(0, fileNameLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public static void DownloadFile(string url, string filePath)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
FileHelpers.CreateDirectoryFromFilePath(filePath);
|
||||
|
||||
using (WebClient wc = new WebClient())
|
||||
{
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
wc.DownloadFile(url, filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string DownloadString(string url, bool noCache = true)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
using (WebClient wc = new WebClient())
|
||||
{
|
||||
if (noCache)
|
||||
{
|
||||
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
}
|
||||
|
||||
wc.Encoding = Encoding.UTF8;
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
return wc.DownloadString(url);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,8 +24,6 @@ You should have received a copy of the GNU General Public License
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
{
|
||||
|
@ -33,26 +31,17 @@ public class AppVeyor
|
|||
{
|
||||
public string AccountName { get; set; }
|
||||
public string ProjectSlug { get; set; }
|
||||
public IWebProxy Proxy { get; set; }
|
||||
|
||||
private const string APIURL = "https://ci.appveyor.com/api";
|
||||
|
||||
public AppVeyorProject GetProjectByBranch(string branch = "master")
|
||||
{
|
||||
string url = $"{APIURL}/projects/{AccountName}/{ProjectSlug}/branch/{branch}";
|
||||
string response = URLHelpers.DownloadString(url);
|
||||
|
||||
using (WebClient wc = new WebClient())
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = Proxy;
|
||||
|
||||
string response = wc.DownloadString(url);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<AppVeyorProject>(response);
|
||||
}
|
||||
return JsonConvert.DeserializeObject<AppVeyorProject>(response);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -61,19 +50,11 @@ public AppVeyorProject GetProjectByBranch(string branch = "master")
|
|||
public AppVeyorProjectArtifact[] GetArtifacts(string jobId)
|
||||
{
|
||||
string url = $"{APIURL}/buildjobs/{jobId}/artifacts";
|
||||
string response = URLHelpers.DownloadString(url);
|
||||
|
||||
using (WebClient wc = new WebClient())
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = Proxy;
|
||||
|
||||
string response = wc.DownloadString(url);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<AppVeyorProjectArtifact[]>(response);
|
||||
}
|
||||
return JsonConvert.DeserializeObject<AppVeyorProjectArtifact[]>(response);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -39,8 +39,7 @@ public override void CheckUpdate()
|
|||
AppVeyor appveyor = new AppVeyor()
|
||||
{
|
||||
AccountName = "ShareX",
|
||||
ProjectSlug = "sharex",
|
||||
Proxy = Proxy
|
||||
ProjectSlug = "sharex"
|
||||
};
|
||||
|
||||
AppVeyorProject project = appveyor.GetProjectByBranch(Branch);
|
||||
|
|
|
@ -57,6 +57,7 @@ private DownloaderForm()
|
|||
InitializeComponent();
|
||||
ShareXResources.ApplyTheme(this);
|
||||
|
||||
Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
ChangeStatus(Resources.DownloaderForm_DownloaderForm_Waiting_);
|
||||
Status = DownloaderFormStatus.Waiting;
|
||||
AutoStartDownload = true;
|
||||
|
@ -74,8 +75,6 @@ public DownloaderForm(string url, string fileName) : this()
|
|||
|
||||
public DownloaderForm(UpdateChecker updateChecker) : this(updateChecker.DownloadURL, updateChecker.FileName)
|
||||
{
|
||||
Proxy = updateChecker.Proxy;
|
||||
|
||||
if (updateChecker is GitHubUpdateChecker)
|
||||
{
|
||||
AcceptHeader = "application/octet-stream";
|
||||
|
|
|
@ -27,8 +27,6 @@ You should have received a copy of the GNU General Public License
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
{
|
||||
|
@ -90,18 +88,11 @@ public virtual string GetLatestDownloadURL(bool isBrowserDownloadURL)
|
|||
|
||||
protected List<GitHubRelease> GetReleases()
|
||||
{
|
||||
using (WebClient wc = new WebClient())
|
||||
string response = URLHelpers.DownloadString(ReleasesURL);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = Proxy;
|
||||
|
||||
string response = wc.DownloadString(ReleasesURL);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<List<GitHubRelease>>(response);
|
||||
}
|
||||
return JsonConvert.DeserializeObject<List<GitHubRelease>>(response);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -96,8 +96,7 @@ public GitHubUpdateChecker CreateUpdateChecker()
|
|||
{
|
||||
IsBeta = IsBeta,
|
||||
IsPortable = IsPortable,
|
||||
IncludePreRelease = CheckPreReleaseUpdates,
|
||||
Proxy = HelpersOptions.CurrentProxy.GetWebProxy()
|
||||
IncludePreRelease = CheckPreReleaseUpdates
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,6 @@ You should have received a copy of the GNU General Public License
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Windows.Forms;
|
||||
|
@ -42,7 +41,6 @@ public abstract class UpdateChecker
|
|||
public ReleaseChannelType ReleaseType { get; set; }
|
||||
public bool IsBeta { get; set; }
|
||||
public bool IsPortable { get; set; }
|
||||
public IWebProxy Proxy { get; set; }
|
||||
|
||||
private string fileName;
|
||||
|
||||
|
|
|
@ -25,8 +25,6 @@ You should have received a copy of the GNU General Public License
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
|
||||
|
@ -47,45 +45,40 @@ public override void CheckUpdate()
|
|||
{
|
||||
try
|
||||
{
|
||||
using (WebClient wc = new WebClient())
|
||||
string response = URLHelpers.DownloadString(URL);
|
||||
|
||||
using (StringReader sr = new StringReader(response))
|
||||
using (XmlTextReader xml = new XmlTextReader(sr))
|
||||
{
|
||||
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = Proxy;
|
||||
XDocument xd = XDocument.Load(xml);
|
||||
|
||||
using (MemoryStream ms = new MemoryStream(wc.DownloadData(URL)))
|
||||
using (XmlTextReader xml = new XmlTextReader(ms))
|
||||
if (xd != null)
|
||||
{
|
||||
XDocument xd = XDocument.Load(xml);
|
||||
string node;
|
||||
|
||||
if (xd != null)
|
||||
switch (ReleaseType)
|
||||
{
|
||||
string node;
|
||||
default:
|
||||
case ReleaseChannelType.Stable:
|
||||
node = "Stable";
|
||||
break;
|
||||
case ReleaseChannelType.Beta:
|
||||
node = "Beta|Stable";
|
||||
break;
|
||||
case ReleaseChannelType.Dev:
|
||||
node = "Dev|Beta|Stable";
|
||||
break;
|
||||
}
|
||||
|
||||
switch (ReleaseType)
|
||||
{
|
||||
default:
|
||||
case ReleaseChannelType.Stable:
|
||||
node = "Stable";
|
||||
break;
|
||||
case ReleaseChannelType.Beta:
|
||||
node = "Beta|Stable";
|
||||
break;
|
||||
case ReleaseChannelType.Dev:
|
||||
node = "Dev|Beta|Stable";
|
||||
break;
|
||||
}
|
||||
string path = string.Format("Update/{0}/{1}", ApplicationName, node);
|
||||
XElement xe = xd.GetNode(path);
|
||||
|
||||
string path = string.Format("Update/{0}/{1}", ApplicationName, node);
|
||||
XElement xe = xd.GetNode(path);
|
||||
|
||||
if (xe != null)
|
||||
{
|
||||
LatestVersion = new Version(xe.Element("Version").Value);
|
||||
DownloadURL = xe.Element("URL").Value;
|
||||
RefreshStatus();
|
||||
return;
|
||||
}
|
||||
if (xe != null)
|
||||
{
|
||||
LatestVersion = new Version(xe.Element("Version").Value);
|
||||
DownloadURL = xe.Element("URL").Value;
|
||||
RefreshStatus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,6 @@ public static DialogResult DownloadFFmpeg(bool async, DownloaderForm.DownloaderI
|
|||
|
||||
using (DownloaderForm form = new DownloaderForm(url, "ffmpeg.zip"))
|
||||
{
|
||||
form.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
form.InstallType = InstallType.Event;
|
||||
form.RunInstallerInBackground = async;
|
||||
form.InstallRequested += installRequested;
|
||||
|
|
|
@ -38,7 +38,6 @@ public static DialogResult DownloadFFmpeg(bool async, DownloaderForm.DownloaderI
|
|||
|
||||
using (DownloaderForm form = new DownloaderForm(url, "ffmpeg.zip"))
|
||||
{
|
||||
form.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
form.InstallType = InstallType.Event;
|
||||
form.RunInstallerInBackground = async;
|
||||
form.InstallRequested += installRequested;
|
||||
|
|
|
@ -29,7 +29,6 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
||||
namespace ShareX.Setup
|
||||
{
|
||||
|
@ -42,11 +41,7 @@ public static string DownloadFile(string url)
|
|||
|
||||
Console.WriteLine($"Downloading: \"{url}\" -> \"{filePath}\"");
|
||||
|
||||
using (WebClient wc = new WebClient())
|
||||
{
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.DownloadFile(url, filePath);
|
||||
}
|
||||
URLHelpers.DownloadFile(url, filePath);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
|
|
@ -29,8 +29,6 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
|
||||
namespace ShareX
|
||||
{
|
||||
|
@ -66,24 +64,17 @@ public void UpdateUnread()
|
|||
|
||||
private List<NewsItem> GetNews()
|
||||
{
|
||||
using (WebClient wc = new WebClient())
|
||||
string url = URLHelpers.CombineURL(Links.Website, "news.json");
|
||||
string response = URLHelpers.DownloadString(url);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
|
||||
string url = URLHelpers.CombineURL(Links.Website, "news.json");
|
||||
string response = wc.DownloadString(url);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings
|
||||
{
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings
|
||||
{
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Local
|
||||
};
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Local
|
||||
};
|
||||
|
||||
return JsonConvert.DeserializeObject<List<NewsItem>>(response, settings);
|
||||
}
|
||||
return JsonConvert.DeserializeObject<List<NewsItem>>(response, settings);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -1889,7 +1889,6 @@ public static async Task DownloadAppVeyorBuild()
|
|||
{
|
||||
IsBeta = Program.Dev,
|
||||
IsPortable = Program.Portable,
|
||||
Proxy = HelpersOptions.CurrentProxy.GetWebProxy(),
|
||||
Branch = "develop"
|
||||
};
|
||||
|
||||
|
|
|
@ -32,7 +32,6 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
@ -1072,14 +1071,7 @@ private bool DownloadFromURL(bool upload)
|
|||
|
||||
try
|
||||
{
|
||||
FileHelpers.CreateDirectoryFromFilePath(Info.FilePath);
|
||||
|
||||
using (WebClient wc = new WebClient())
|
||||
{
|
||||
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
|
||||
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
|
||||
wc.DownloadFile(url, Info.FilePath);
|
||||
}
|
||||
URLHelpers.DownloadFile(url, Info.FilePath);
|
||||
|
||||
if (upload)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue