Skip to content

Commit

Permalink
Merge pull request #2 from robinrodricks/master
Browse files Browse the repository at this point in the history
Pull from robinrodricks/FluentFTP
  • Loading branch information
taoyouh authored Oct 11, 2017
2 parents 821e04a + f75333f commit 0c5fb13
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 36 deletions.
69 changes: 46 additions & 23 deletions FluentFTP/Client/FtpClient_HighLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public async Task<int> DownloadFilesAsync(string localDir, IEnumerable<string> r
/// but only if you are SURE that the files do not exist on the server.</param>
/// <param name="createRemoteDir">Create the remote directory if it does not exist. Slows down upload due to additional checks required.</param>
/// <param name="verifyOptions">Sets if checksum verification is required for a successful upload and what to do if it fails verification (See Remarks)</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was uploaded, false otherwise.</returns>
/// <remarks>
/// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server. If the server does not support
Expand Down Expand Up @@ -721,7 +721,7 @@ public bool UploadFile(string localPath, string remotePath, FtpExists existsMode
/// <param name="createRemoteDir">Create the remote directory if it does not exist. Slows down upload due to additional checks required.</param>
/// <param name="verifyOptions">Sets if checksum verification is required for a successful upload and what to do if it fails verification (See Remarks)</param>
/// <param name="token">The token to monitor for cancellation requests.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was uploaded, false otherwise.</returns>
/// <remarks>
/// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server. If the server does not support
Expand Down Expand Up @@ -869,7 +869,7 @@ private async Task<bool> UploadFileFromFileAsync(string localPath, string remote
/// <param name="existsMode">What to do if the file already exists? Skip, overwrite or append? Set this to <see cref="FtpExists.NoCheck"/> for fastest performance
/// but only if you are SURE that the files do not exist on the server.</param>
/// <param name="createRemoteDir">Create the remote directory if it does not exist. Slows down upload due to additional checks required.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
public bool Upload(Stream fileStream, string remotePath, FtpExists existsMode = FtpExists.Overwrite, bool createRemoteDir = false, IProgress<double> progress = null) {

// verify args
Expand All @@ -893,7 +893,7 @@ public bool Upload(Stream fileStream, string remotePath, FtpExists existsMode =
/// <param name="existsMode">What to do if the file already exists? Skip, overwrite or append? Set this to <see cref="FtpExists.NoCheck"/> for fastest performance
/// but only if you are SURE that the files do not exist on the server.</param>
/// <param name="createRemoteDir">Create the remote directory if it does not exist. Slows down upload due to additional checks required.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
public bool Upload(byte[] fileData, string remotePath, FtpExists existsMode = FtpExists.Overwrite, bool createRemoteDir = false, IProgress<double> progress = null) {

// verify args
Expand Down Expand Up @@ -924,7 +924,7 @@ public bool Upload(byte[] fileData, string remotePath, FtpExists existsMode = Ft
/// but only if you are SURE that the files do not exist on the server.</param>
/// <param name="createRemoteDir">Create the remote directory if it does not exist. Slows down upload due to additional checks required.</param>
/// <param name="token">The token to monitor for cancellation requests.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was uploaded, false otherwise.</returns>
public async Task<bool> UploadAsync(Stream fileStream, string remotePath, FtpExists existsMode, bool createRemoteDir, CancellationToken token, IProgress<double> progress) {

Expand All @@ -951,7 +951,7 @@ public async Task<bool> UploadAsync(Stream fileStream, string remotePath, FtpExi
/// but only if you are SURE that the files do not exist on the server.</param>
/// <param name="createRemoteDir">Create the remote directory if it does not exist. Slows down upload due to additional checks required.</param>
/// <param name="token">The token to monitor for cancellation requests.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was uploaded, false otherwise.</returns>
public async Task<bool> UploadAsync(byte[] fileData, string remotePath, FtpExists existsMode, bool createRemoteDir, CancellationToken token, IProgress<double> progress) {

Expand Down Expand Up @@ -1092,7 +1092,7 @@ private bool UploadFileInternal(Stream fileData, string remotePath, bool createR

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)len) * 100);
ReportProgress(progress, len, offset);
}
}

Expand Down Expand Up @@ -1126,7 +1126,7 @@ private bool UploadFileInternal(Stream fileData, string remotePath, bool createR

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)len) * 100);
ReportProgress(progress, len, offset);
}

// honor the speed limit
Expand Down Expand Up @@ -1278,7 +1278,7 @@ private async Task<bool> UploadFileInternalAsync(Stream fileData, string remoteP

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)len) * 100);
ReportProgress(progress, len, offset);
}
}

Expand Down Expand Up @@ -1313,7 +1313,7 @@ private async Task<bool> UploadFileInternalAsync(Stream fileData, string remoteP

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)len) * 100);
ReportProgress(progress, len, offset);
}

// honor the rate limit
Expand Down Expand Up @@ -1401,7 +1401,7 @@ private bool ResumeUpload(string remotePath, ref Stream upStream, long offset, I
/// <param name="remotePath">The full or relative path to the file on the server</param>
/// <param name="overwrite">True if you want the local file to be overwritten if it already exists. (Default value is true)</param>
/// <param name="verifyOptions">Sets if checksum verification is required for a successful download and what to do if it fails verification (See Remarks)</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was downloaded, false otherwise.</returns>
/// <remarks>
/// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server. If the server does not support
Expand Down Expand Up @@ -1488,7 +1488,7 @@ private bool DownloadFileToFile(string localPath, string remotePath, bool overwr
/// <param name="overwrite">True if you want the local file to be overwritten if it already exists. (Default value is true)</param>
/// <param name="verifyOptions">Sets if checksum verification is required for a successful download and what to do if it fails verification (See Remarks)</param>
/// <param name="token">The token to monitor for cancellation requests</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was downloaded, false otherwise.</returns>
/// <remarks>
/// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server. If the server does not support
Expand Down Expand Up @@ -1517,7 +1517,7 @@ public async Task<bool> DownloadFileAsync(string localPath, string remotePath, b
/// <param name="remotePath">The full or relative path to the file on the server</param>
/// <param name="overwrite">True if you want the local file to be overwritten if it already exists. (Default value is true)</param>
/// <param name="verifyOptions">Sets if checksum verification is required for a successful download and what to do if it fails verification (See Remarks)</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was downloaded, false otherwise.</returns>
/// <remarks>
/// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server. If the server does not support
Expand Down Expand Up @@ -1607,7 +1607,7 @@ private async Task<bool> DownloadFileToFileAsync(string localPath, string remote
/// </summary>
/// <param name="outStream">The stream that the file will be written to. Provide a new MemoryStream if you only want to read the file into memory.</param>
/// <param name="remotePath">The full or relative path to the file on the server</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was downloaded, false otherwise.</returns>
public bool Download(Stream outStream, string remotePath, IProgress<double> progress = null) {

Expand All @@ -1630,7 +1630,7 @@ public bool Download(Stream outStream, string remotePath, IProgress<double> prog
/// </summary>
/// <param name="outBytes">The variable that will receive the bytes.</param>
/// <param name="remotePath">The full or relative path to the file on the server</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was downloaded, false otherwise.</returns>
public bool Download(out byte[] outBytes, string remotePath, IProgress<double> progress = null) {

Expand Down Expand Up @@ -1662,7 +1662,7 @@ public bool Download(out byte[] outBytes, string remotePath, IProgress<double> p
/// <param name="outStream">The stream that the file will be written to. Provide a new MemoryStream if you only want to read the file into memory.</param>
/// <param name="remotePath">The full or relative path to the file on the server</param>
/// <param name="token">The token to monitor cancellation requests</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>If true then the file was downloaded, false otherwise.</returns>
public async Task<bool> DownloadAsync(Stream outStream, string remotePath, CancellationToken token, IProgress<double> progress = null) {

Expand Down Expand Up @@ -1707,7 +1707,7 @@ public async Task<bool> DownloadAsync(Stream outStream, string remotePath) {
/// </summary>
/// <param name="remotePath">The full or relative path to the file on the server</param>
/// <param name="token">The token to monitor cancellation requests</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, representing a percentage. It is calculated based on bytes transferred vs the length of the file.</param>
/// <param name="progress">Provide an implementation of IProgress to track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.</param>
/// <returns>A byte array containing the contents of the downloaded file if successful, otherwise null.</returns>
public async Task<byte[]> DownloadAsync(string remotePath, CancellationToken token, IProgress<double> progress = null) {

Expand Down Expand Up @@ -1754,7 +1754,7 @@ private bool DownloadFileInternal(string remotePath, Stream outStream, IProgress

// get file size if downloading in binary mode (in ASCII mode we read until EOF)
long fileLen = 0;
if (CurrentDataType == FtpDataType.Binary){
if (DownloadDataType == FtpDataType.Binary){
fileLen = GetFileSize(remotePath);
}

Expand Down Expand Up @@ -1782,7 +1782,7 @@ private bool DownloadFileInternal(string remotePath, Stream outStream, IProgress

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)fileLen) * 100);
ReportProgress(progress, fileLen, offset);
}
}

Expand Down Expand Up @@ -1822,7 +1822,7 @@ private bool DownloadFileInternal(string remotePath, Stream outStream, IProgress

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)fileLen) * 100);
ReportProgress(progress, fileLen, offset);
}

// honor the rate limit
Expand Down Expand Up @@ -1905,7 +1905,8 @@ private async Task<bool> DownloadFileInternalAsync(string remotePath, Stream out

// get file size if downloading in binary mode (in ASCII mode we read until EOF)
long fileLen = 0;
if (CurrentDataType == FtpDataType.Binary){

if (DownloadDataType == FtpDataType.Binary){
fileLen = await GetFileSizeAsync(remotePath);
}

Expand Down Expand Up @@ -1933,7 +1934,7 @@ private async Task<bool> DownloadFileInternalAsync(string remotePath, Stream out

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)fileLen) * 100);
ReportProgress(progress, fileLen, offset);
}
}

Expand Down Expand Up @@ -1973,7 +1974,7 @@ private async Task<bool> DownloadFileInternalAsync(string remotePath, Stream out

// send progress reports
if (progress != null) {
progress.Report(((double)offset / (double)fileLen) * 100);
ReportProgress(progress, fileLen, offset);
}

// honor the rate limit
Expand Down Expand Up @@ -2110,5 +2111,27 @@ private async Task<bool> VerifyTransferAsync(string localPath, string remotePath

#endregion

#region Utilities

/// <summary>
/// Sends progress to the user, either a value between 0-100 indicating percentage complete, or -1 for indeterminate.
/// </summary>
private void ReportProgress(IProgress<double> progress, long fileSize, long position) {

// calculate % based on file len vs file offset
double value = ((double)position / (double)fileSize) * 100;

// suppress invalid values and send -1 instead
if (double.IsNaN(value) || double.IsInfinity(value)) {
progress.Report(-1);
} else {

// send a value between 0-100 indicating percentage complete
progress.Report(value);
}
}

#endregion

}
}
2 changes: 1 addition & 1 deletion FluentFTP/Stream/FtpDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public FtpDataStream(FtpClient conn) : base(conn.SslProtocols) {
/// </summary>
~FtpDataStream() {
try {
Dispose();
Dispose(false);
} catch (Exception ex) {
FtpTrace.WriteLine(FtpTraceLevel.Warn, "[Finalizer] Caught and discarded an exception while disposing the FtpDataStream: " + ex.ToString());
}
Expand Down
20 changes: 8 additions & 12 deletions FluentFTP/Stream/FtpSocketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,25 +662,21 @@ public async Task WriteLineAsync(System.Text.Encoding encoding, string buf) {
}
#endif

#if CORE
/// <summary>
/// Disposes the stream
/// Disconnects from server
/// </summary>
public new void Dispose() {
FtpTrace.WriteStatus(FtpTraceLevel.Verbose, "Disposing FtpSocketStream...");
Close();
public void Close()
{
Dispose(true);
}
#endif

/// <summary>
/// Disconnects from server
/// </summary>
#if CORE
public void Close()
{
base.Dispose(true);
#else
public override void Close() {
base.Close();
#endif
protected override void Dispose(bool disposing) {
FtpTrace.WriteStatus(FtpTraceLevel.Verbose, "Disposing FtpSocketStream...");
if (m_socket != null) {
try {
if (m_socket.Connected) {
Expand Down
Loading

0 comments on commit 0c5fb13

Please sign in to comment.