mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-02 18:26:27 +13:00
FTP dispose fix, SFTP stop upload override
This commit is contained in:
parent
c315482735
commit
99e9bbc7c8
6 changed files with 104 additions and 68 deletions
|
@ -117,10 +117,11 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
}
|
||||
finally
|
||||
{
|
||||
Dispose();
|
||||
IsUploading = false;
|
||||
}
|
||||
|
||||
if (!stopUpload && Errors.Count == 0)
|
||||
if (!StopUploadRequested && !IsError)
|
||||
{
|
||||
result.URL = Account.GetUriPath(fileName);
|
||||
}
|
||||
|
@ -130,9 +131,9 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
|
||||
public override void StopUpload()
|
||||
{
|
||||
if (IsUploading && !stopUpload)
|
||||
if (IsUploading && !StopUploadRequested)
|
||||
{
|
||||
stopUpload = true;
|
||||
StopUploadRequested = true;
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +150,7 @@ public bool Connect()
|
|||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (client != null && client.IsConnected)
|
||||
if (client != null)
|
||||
{
|
||||
client.Disconnect();
|
||||
}
|
||||
|
@ -412,9 +413,15 @@ public void Dispose()
|
|||
{
|
||||
if (client != null)
|
||||
{
|
||||
Disconnect();
|
||||
try
|
||||
{
|
||||
client.Dispose();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugHelper.WriteException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -88,7 +88,7 @@ private void TranscodeFile(string key, UploadResult result)
|
|||
OnProgressChanged(progress);
|
||||
}
|
||||
|
||||
while (!stopUpload)
|
||||
while (!StopUploadRequested)
|
||||
{
|
||||
string statusJson = SendRequest(HttpMethod.GET, "https://upload.gfycat.com/status/" + key);
|
||||
GfycatStatusResponse response = JsonConvert.DeserializeObject<GfycatStatusResponse>(statusJson);
|
||||
|
|
|
@ -39,7 +39,7 @@ public class MediaCrushUploader : FileUploader
|
|||
{
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
{
|
||||
SuppressWebExceptions = true;
|
||||
ThrowWebExceptions = true;
|
||||
|
||||
string hash = CreateHash(stream);
|
||||
|
||||
|
@ -73,7 +73,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
|
||||
hash = JToken.Parse(result.Response)["hash"].Value<string>();
|
||||
|
||||
while (!stopUpload)
|
||||
while (!StopUploadRequested)
|
||||
{
|
||||
result.Response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash + "/status");
|
||||
JToken jsonResponse = JToken.Parse(result.Response);
|
||||
|
|
|
@ -61,6 +61,45 @@ public SFTP(FTPAccount account)
|
|||
Account = account;
|
||||
}
|
||||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
{
|
||||
UploadResult result = new UploadResult();
|
||||
|
||||
if (Connect())
|
||||
{
|
||||
fileName = Helpers.GetValidURL(fileName);
|
||||
string folderPath = Account.GetSubFolderPath();
|
||||
string filePath = URLHelpers.CombineURL(folderPath, fileName);
|
||||
|
||||
try
|
||||
{
|
||||
IsUploading = true;
|
||||
UploadStream(stream, filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Dispose();
|
||||
IsUploading = false;
|
||||
}
|
||||
|
||||
if (!StopUploadRequested && !IsError)
|
||||
{
|
||||
result.URL = Account.GetUriPath(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override void StopUpload()
|
||||
{
|
||||
if (IsUploading && !StopUploadRequested)
|
||||
{
|
||||
StopUploadRequested = true;
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
if (client == null)
|
||||
|
@ -170,57 +209,39 @@ public List<string> CreateMultiDirectory(string path)
|
|||
return directoryList;
|
||||
}
|
||||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
private void UploadStream(Stream stream, string remotePath)
|
||||
{
|
||||
UploadResult result = new UploadResult();
|
||||
|
||||
if (Connect())
|
||||
{
|
||||
fileName = Helpers.GetValidURL(fileName);
|
||||
string folderPath = Account.GetSubFolderPath();
|
||||
string filePath = URLHelpers.CombineURL(folderPath, fileName);
|
||||
|
||||
try
|
||||
{
|
||||
IsUploading = true;
|
||||
UploadStream(stream, filePath);
|
||||
using (SftpFileStream sftpStream = client.OpenWrite(remotePath))
|
||||
{
|
||||
TransferData(stream, sftpStream);
|
||||
}
|
||||
}
|
||||
catch (SftpPathNotFoundException)
|
||||
{
|
||||
CreateDirectory(folderPath);
|
||||
UploadStream(stream, filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsUploading = false;
|
||||
}
|
||||
CreateDirectory(URLHelpers.GetDirectoryPath(remotePath));
|
||||
|
||||
Disconnect();
|
||||
|
||||
if (!stopUpload && Errors.Count == 0)
|
||||
{
|
||||
result.URL = Account.GetUriPath(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void UploadStream(Stream stream, string path)
|
||||
{
|
||||
using (SftpFileStream sftpStream = client.OpenWrite(path))
|
||||
using (SftpFileStream sftpStream = client.OpenWrite(remotePath))
|
||||
{
|
||||
TransferData(stream, sftpStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (client != null)
|
||||
{
|
||||
Disconnect();
|
||||
try
|
||||
{
|
||||
client.Dispose();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugHelper.WriteException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -534,20 +534,14 @@ private void bw_DoWork(object sender, DoWorkEventArgs e)
|
|||
}
|
||||
}
|
||||
|
||||
public class ProgressInfo
|
||||
private class ProgressInfo
|
||||
{
|
||||
public string Status { get; set; }
|
||||
|
||||
public string ETA { get; set; }
|
||||
|
||||
public string Speed { get; set; }
|
||||
|
||||
public string UploadedBytes { get; set; }
|
||||
|
||||
public string TotalSize { get; set; }
|
||||
|
||||
public string Elapsed { get; set; }
|
||||
|
||||
public string Meter { get; set; }
|
||||
|
||||
public ProgressInfo()
|
||||
|
|
|
@ -49,9 +49,17 @@ public class Uploader
|
|||
public bool IsUploading { get; protected set; }
|
||||
public int BufferSize { get; set; }
|
||||
public bool AllowReportProgress { get; set; }
|
||||
public bool SuppressWebExceptions { get; set; }
|
||||
public bool ThrowWebExceptions { get; set; }
|
||||
|
||||
protected bool stopUpload;
|
||||
public bool IsError
|
||||
{
|
||||
get
|
||||
{
|
||||
return !StopUploadRequested && Errors != null && Errors.Count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
public bool StopUploadRequested { get; protected set; }
|
||||
|
||||
private HttpWebRequest currentRequest;
|
||||
|
||||
|
@ -61,6 +69,7 @@ public Uploader()
|
|||
IsUploading = false;
|
||||
BufferSize = 8192;
|
||||
AllowReportProgress = true;
|
||||
ThrowWebExceptions = false;
|
||||
|
||||
ServicePointManager.DefaultConnectionLimit = 25;
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
|
@ -77,14 +86,19 @@ protected void OnProgressChanged(ProgressManager progress)
|
|||
|
||||
public string ToErrorString()
|
||||
{
|
||||
return string.Join("\r\n", Errors.ToArray());
|
||||
if (IsError)
|
||||
{
|
||||
return string.Join(Environment.NewLine, Errors);
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public virtual void StopUpload()
|
||||
{
|
||||
if (IsUploading)
|
||||
{
|
||||
stopUpload = true;
|
||||
StopUploadRequested = true;
|
||||
|
||||
if (currentRequest != null)
|
||||
{
|
||||
|
@ -169,7 +183,7 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Dictionary<st
|
|||
NameValueCollection headers = null, CookieCollection cookies = null, Stream dataStream = null)
|
||||
{
|
||||
IsUploading = true;
|
||||
stopUpload = false;
|
||||
StopUploadRequested = false;
|
||||
|
||||
url = CreateQuery(url, arguments);
|
||||
|
||||
|
@ -192,9 +206,9 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Dictionary<st
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!stopUpload)
|
||||
if (!StopUploadRequested)
|
||||
{
|
||||
if (SuppressWebExceptions && e is WebException) throw;
|
||||
if (ThrowWebExceptions && e is WebException) throw;
|
||||
AddWebError(e);
|
||||
}
|
||||
}
|
||||
|
@ -254,7 +268,7 @@ private HttpWebResponse GetResponseUsingPost(string url, Stream dataStream, stri
|
|||
CookieCollection cookies = null, NameValueCollection headers = null)
|
||||
{
|
||||
IsUploading = true;
|
||||
stopUpload = false;
|
||||
StopUploadRequested = false;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -269,9 +283,9 @@ private HttpWebResponse GetResponseUsingPost(string url, Stream dataStream, stri
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!stopUpload)
|
||||
if (!StopUploadRequested)
|
||||
{
|
||||
if (SuppressWebExceptions && e is WebException) throw;
|
||||
if (ThrowWebExceptions && e is WebException) throw;
|
||||
AddWebError(e);
|
||||
}
|
||||
}
|
||||
|
@ -291,7 +305,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
|
|||
UploadResult result = new UploadResult();
|
||||
|
||||
IsUploading = true;
|
||||
stopUpload = false;
|
||||
StopUploadRequested = false;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -317,9 +331,9 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!stopUpload)
|
||||
if (!StopUploadRequested)
|
||||
{
|
||||
if (SuppressWebExceptions && e is WebException) throw;
|
||||
if (ThrowWebExceptions && e is WebException) throw;
|
||||
AddWebError(e);
|
||||
}
|
||||
}
|
||||
|
@ -396,7 +410,7 @@ protected bool TransferData(Stream dataStream, Stream requestStream)
|
|||
byte[] buffer = new byte[length];
|
||||
int bytesRead;
|
||||
|
||||
while (!stopUpload && (bytesRead = dataStream.Read(buffer, 0, length)) > 0)
|
||||
while (!StopUploadRequested && (bytesRead = dataStream.Read(buffer, 0, length)) > 0)
|
||||
{
|
||||
requestStream.Write(buffer, 0, bytesRead);
|
||||
|
||||
|
@ -406,7 +420,7 @@ protected bool TransferData(Stream dataStream, Stream requestStream)
|
|||
}
|
||||
}
|
||||
|
||||
return !stopUpload;
|
||||
return !StopUploadRequested;
|
||||
}
|
||||
|
||||
private string CreateBoundary()
|
||||
|
|
Loading…
Reference in a new issue