Skip to content

Commit

Permalink
upload cancelling
Browse files Browse the repository at this point in the history
  • Loading branch information
TURSAS authored and TURSAS committed Sep 29, 2016
1 parent 7b0c10b commit 46b747b
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 336 deletions.
5 changes: 4 additions & 1 deletion ScreenShotterWPF/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<value>0</value>
</setting>
<setting name="fileUploadSite" serializeAs="String">
<value>99</value>
<value>None</value>
</setting>
<setting name="fileUploadEnabled" serializeAs="String">
<value>False</value>
Expand All @@ -172,6 +172,9 @@
<setting name="gdriveTokenExpire" serializeAs="String">
<value>0</value>
</setting>
<setting name="imageUploadSite" serializeAs="String">
<value>Imgur</value>
</setting>
</ScreenShotterWPF.Properties.Settings>
</userSettings>
<runtime>
Expand Down
436 changes: 218 additions & 218 deletions ScreenShotterWPF/MainLogic.cs

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions ScreenShotterWPF/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions ScreenShotterWPF/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
<Setting Name="ftpProtocol" Provider="PortableSettingsProvider" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="fileUploadSite" Provider="PortableSettingsProvider" Type="System.Int32" Scope="User">
<Value Profile="(Default)">99</Value>
<Setting Name="fileUploadSite" Provider="PortableSettingsProvider" Type="ScreenShotterWPF.UploadSite" Scope="User">
<Value Profile="(Default)">None</Value>
</Setting>
<Setting Name="fileUploadEnabled" Provider="PortableSettingsProvider" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
Expand All @@ -173,5 +173,8 @@
<Setting Name="gdriveTokenExpire" Provider="PortableSettingsProvider" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="imageUploadSite" Provider="PortableSettingsProvider" Type="ScreenShotterWPF.UploadSite" Scope="User">
<Value Profile="(Default)">Imgur</Value>
</Setting>
</Settings>
</SettingsFile>
45 changes: 28 additions & 17 deletions ScreenShotterWPF/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Handlers;
using System.Threading;
using Hardcodet.Wpf.TaskbarNotification;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static async Task<string> GetDropboxSharedUrl(string path)
}
}

public static async Task SetGoogleDriveFileShared(string fileID)
public static async Task SetGoogleDriveFileShared(string fileID, CancellationToken token)
{
try
{
Expand All @@ -58,7 +59,7 @@ public static async Task SetGoogleDriveFileShared(string fileID)
{
["Authorization"] = $"Bearer {Properties.Settings.Default.gdriveToken}"
};
await UploadData($"https://www.googleapis.com/drive/v3/files/{fileID}/permissions", content, headers);
await UploadData($"https://www.googleapis.com/drive/v3/files/{fileID}/permissions", content, token, headers);

}
catch (Exception)
Expand All @@ -83,7 +84,7 @@ public static async Task SetGoogleDriveFileShared(string fileID)
return s;
}*/

private static async Task<string> UploadData(string uri, HttpContent formData, Dictionary<string, string> extraHeaders = null)
private static async Task<string> UploadData(string uri, HttpContent formData, CancellationToken token, Dictionary<string, string> extraHeaders = null)
{
ProgressMessageHandler progress = new ProgressMessageHandler();
progress.HttpSendProgress += HttpSendProgress;
Expand All @@ -98,6 +99,7 @@ private static async Task<string> UploadData(string uri, HttpContent formData, D
{
client.DefaultRequestHeaders.Add("User-Agent", "LXtory/1.0");
client.DefaultRequestHeaders.Add("Connection", "Close");
client.Timeout = Timeout.InfiniteTimeSpan;

if (extraHeaders != null)
{
Expand All @@ -106,17 +108,16 @@ private static async Task<string> UploadData(string uri, HttpContent formData, D
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
var response = await client.SendAsync(message);
var response = await client.SendAsync(message, token);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
var error = await response.Content.ReadAsStringAsync();
Console.WriteLine(error);
throw new Exception($"Upload error.\r\n{response.ReasonPhrase}\r\n{error}");
}
throw new Exception($"Upload error.\n{response.ReasonPhrase}");
}
}

Expand All @@ -131,7 +132,7 @@ private static void Wc_UploadProgressChanged(object sender, UploadProgressChange
ProgressBarUpdate(progress);
}

public static async Task<string> HttpGyazoUpload(XImage img)
public static async Task<string> HttpGyazoUpload(XImage img, CancellationToken token)
{
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
using (var form = new MultipartFormDataContent(boundary))
Expand All @@ -142,11 +143,11 @@ public static async Task<string> HttpGyazoUpload(XImage img)
? new StreamContent(new MemoryStream(img.image))
: new StreamContent(new FileStream(img.filepath, FileMode.Open)), "imagedata", img.filename);
img.image = null;
return await UploadData("http://upload.gyazo.com/api/upload", form);
return await UploadData("http://upload.gyazo.com/api/upload", form, token);
}
}

public static async Task<string> HttpDropboxUpload(XImage img)
public static async Task<string> HttpDropboxUpload(XImage img, CancellationToken token)
{
HttpContent content = img.image != null ? new StreamContent(new MemoryStream(img.image)) : new StreamContent(new FileStream(img.filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
JObject args = new JObject(
Expand All @@ -162,10 +163,10 @@ public static async Task<string> HttpDropboxUpload(XImage img)
["Authorization"] = $"Bearer {Properties.Settings.Default.dropboxToken}"
};
img.image = null;
return await UploadData("https://content.dropboxapi.com/2/files/upload", content, headers);
return await UploadData("https://content.dropboxapi.com/2/files/upload", content, token, headers);
}

public static async Task<string> HttpGoogleDriveUpload(XImage img)
public static async Task<string> HttpGoogleDriveUpload(XImage img, CancellationToken token)
{
JObject metadata = new JObject(
new JProperty("name", img.filename),
Expand All @@ -183,11 +184,11 @@ public static async Task<string> HttpGoogleDriveUpload(XImage img)
{
["Authorization"] = $"Bearer {Properties.Settings.Default.gdriveToken}"
};
return await UploadData("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", form, headers);
return await UploadData("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", form, token, headers);
}
}

public static async Task<string> HttpPuushUpload(XImage img)
public static async Task<string> HttpPuushUpload(XImage img, CancellationToken token)
{
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
using (var form = new MultipartFormDataContent(boundary))
Expand All @@ -200,11 +201,11 @@ public static async Task<string> HttpPuushUpload(XImage img)
: new StreamContent(new FileStream(img.filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)), "f", img.filename);
img.image = null;

return await UploadData("https://puush.me/api/up", form);
return await UploadData("https://puush.me/api/up", form, token);
}
}

public static async Task<string> HttpImgurUpload(XImage img)
public static async Task<string> HttpImgurUpload(XImage img, CancellationToken token)
{
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
using (var form = new MultipartFormDataContent(boundary))
Expand All @@ -220,7 +221,7 @@ public static async Task<string> HttpImgurUpload(XImage img)
{
["Authorization"] = img.anonupload ? "Client-ID 83c1c8bf9f4d2b1" : $"Bearer {Properties.Settings.Default.accessToken}"
};
return await UploadData("https://api.imgur.com/3/image", form, extraHeaders);
return await UploadData("https://api.imgur.com/3/image", form, token, extraHeaders);
}
}

Expand Down Expand Up @@ -249,7 +250,7 @@ public static async Task<string> FTPUpload(XImage img)
}
}

public static string SFTPUpload(XImage img, ConnectionInfo ftpConnectionInfo)
public static string SFTPUpload(XImage img, ConnectionInfo ftpConnectionInfo, CancellationToken token)
{
if (ftpConnectionInfo == null)
{
Expand All @@ -270,6 +271,11 @@ public static string SFTPUpload(XImage img, ConnectionInfo ftpConnectionInfo)
client.UploadFile(stream, path,
bytesUploaded =>
{
// Maybe works as cancel?
if (token.IsCancellationRequested)
{
if (client != null) client.Disconnect();
}
int percent = (int)(((double)bytesUploaded / fileSize) * 100.0);
ProgressBarUpdate(percent);
});
Expand All @@ -283,6 +289,11 @@ public static string SFTPUpload(XImage img, ConnectionInfo ftpConnectionInfo)
client.UploadFile(stream, path,
bytesUploaded =>
{
// Maybe works as cancel?
if (token.IsCancellationRequested)
{
if (client != null) client.Disconnect();
}
int percent = (int)(((double)bytesUploaded / fileSize) * 100.0);
ProgressBarUpdate(percent);
});
Expand Down
Loading

0 comments on commit 46b747b

Please sign in to comment.