FTP dispose fix, SFTP stop upload override

This commit is contained in:
Jaex 2014-06-25 00:26:00 +03:00
parent c315482735
commit 99e9bbc7c8
6 changed files with 104 additions and 68 deletions

View file

@ -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,8 +413,14 @@ public void Dispose()
{
if (client != null)
{
Disconnect();
client.Dispose();
try
{
client.Dispose();
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
}
}

View file

@ -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);

View file

@ -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);

View file

@ -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,47 +209,23 @@ 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())
try
{
fileName = Helpers.GetValidURL(fileName);
string folderPath = Account.GetSubFolderPath();
string filePath = URLHelpers.CombineURL(folderPath, fileName);
try
using (SftpFileStream sftpStream = client.OpenWrite(remotePath))
{
IsUploading = true;
UploadStream(stream, filePath);
}
catch (SftpPathNotFoundException)
{
CreateDirectory(folderPath);
UploadStream(stream, filePath);
}
finally
{
IsUploading = false;
}
Disconnect();
if (!stopUpload && Errors.Count == 0)
{
result.URL = Account.GetUriPath(fileName);
TransferData(stream, sftpStream);
}
}
return result;
}
private void UploadStream(Stream stream, string path)
{
using (SftpFileStream sftpStream = client.OpenWrite(path))
catch (SftpPathNotFoundException)
{
TransferData(stream, sftpStream);
CreateDirectory(URLHelpers.GetDirectoryPath(remotePath));
using (SftpFileStream sftpStream = client.OpenWrite(remotePath))
{
TransferData(stream, sftpStream);
}
}
}
@ -218,8 +233,14 @@ public void Dispose()
{
if (client != null)
{
Disconnect();
client.Dispose();
try
{
client.Dispose();
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
}
}

View file

@ -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()

View file

@ -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 @@ public virtual void StopUpload()
NameValueCollection headers = null, CookieCollection cookies = null, Stream dataStream = null)
{
IsUploading = true;
stopUpload = false;
StopUploadRequested = false;
url = CreateQuery(url, arguments);
@ -192,9 +206,9 @@ public virtual void StopUpload()
}
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 PostResponseJSON(string url, string json, CookieCollecti
CookieCollection cookies = null, NameValueCollection headers = null)
{
IsUploading = true;
stopUpload = false;
StopUploadRequested = false;
try
{
@ -269,9 +283,9 @@ private HttpWebResponse PostResponseJSON(string url, string json, CookieCollecti
}
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 @@ private HttpWebResponse PostResponseJSON(string url, string json, CookieCollecti
UploadResult result = new UploadResult();
IsUploading = true;
stopUpload = false;
StopUploadRequested = false;
try
{
@ -317,9 +331,9 @@ private HttpWebResponse PostResponseJSON(string url, string json, CookieCollecti
}
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()