Skip to content

Commit

Permalink
new timezone API and module refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
robinrodricks committed Nov 30, 2024
1 parent 13f0d91 commit 04134ab
Show file tree
Hide file tree
Showing 41 changed files with 533 additions and 541 deletions.
15 changes: 8 additions & 7 deletions FluentFTP.Tests/Unit/TimezoneTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void FranceToUTC() {
// input date = 19/Feb/2020 20:30 (8:30 PM) in France

// convert to UTC (France to UTC)
AssertConvertedDateTime(client, FtpDate.UTC, 2, "20200219203000", "19-Feb-2020 6:30:00 PM");
AssertConvertedDateTime(client, FtpDate.UTC, "Romance Standard Time", "Europe/Paris", "20200219203000", "19-Feb-2020 6:30:00 PM");
}
}
[Fact]
Expand All @@ -25,7 +25,7 @@ public void FranceToLocal() {
// input date = 19/Feb/2020 20:30 (8:30 PM) in France

// convert to local time (France to Mumbai)
AssertConvertedDateTime(client, FtpDate.LocalTime, 2, "20200219203000", "20-Feb-2020 0:00:00");
AssertConvertedDateTime(client, FtpDate.LocalTime, "Romance Standard Time", "Europe/Paris", "20200219203000", "20-Feb-2020 0:00:00");

}
}
Expand All @@ -38,7 +38,7 @@ public void TokyoToUTC() {
// input date = 19/Feb/2020 00:00 (12 am) in Tokyo

// convert to UTC (Tokyo to UTC)
AssertConvertedDateTime(client, FtpDate.UTC, 9, "20200219000000", "18-Feb-2020 3:00:00 PM");
AssertConvertedDateTime(client, FtpDate.UTC, "Tokyo Standard Time", "Asia/Tokyo", "20200219000000", "18-Feb-2020 3:00:00 PM");
}
}
[Fact]
Expand All @@ -48,15 +48,16 @@ public void TokyoToLocal() {
// input date = 19/Feb/2020 00:00 (12 am) in Tokyo

// convert to local time (Tokyo to Mumbai)
AssertConvertedDateTime(client, FtpDate.LocalTime, 9, "20200219000000", "18-Feb-2020 8:30:00 PM");
AssertConvertedDateTime(client, FtpDate.LocalTime, "Tokyo Standard Time", "Asia/Tokyo", "20200219000000", "18-Feb-2020 8:30:00 PM");

}
}

private static void AssertConvertedDateTime(FtpClient client, FtpDate conversion, double tz, string input, string expected) {
private static void AssertConvertedDateTime(FtpClient client, FtpDate conversion, string winTZ, string unixTZ, string input, string expected) {
client.Config.TimeConversion = conversion;
client.Config.TimeZone = tz;
client.Config.LocalTimeZone = 5.5;

client.Config.SetServerTimeZone(winTZ, unixTZ);
client.Config.SetClientTimeZone("Tokyo Standard Time", "Asia/Tokyo");

var result = client.ConvertDate(input.ParseFtpDate(client));

Expand Down
2 changes: 1 addition & 1 deletion FluentFTP/Client/AsyncClient/DeleteDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public partial class AsyncFtpClient {
// when GetListing is called with recursive option, then it does not
// make any sense to call another DeleteDirectory with force flag set.
// however this requires always delete files first.
var recurse = !WasGetListingRecursive(options);
var recurse = !ListingModule.WasGetListingRecursive(options);

// items that are deeper in directory tree are listed first,
// then files will be listed before directories. This matters
Expand Down
3 changes: 2 additions & 1 deletion FluentFTP/Client/AsyncClient/DownloadDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluentFTP.Exceptions;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -74,7 +75,7 @@ public async Task<List<FtpResult>> DownloadDirectory(string localFolder, string
var shouldExist = new Dictionary<string, bool>();

// loop through each file and transfer it #1
var toDownload = GetFilesToDownload(localFolder, remoteFolder, rules, results, listing, shouldExist);
var toDownload = FileDownloadModule.GetFilesToDownload(this, localFolder, remoteFolder, rules, results, listing, shouldExist);

// break if task is cancelled
token.ThrowIfCancellationRequested();
Expand Down
2 changes: 1 addition & 1 deletion FluentFTP/Client/AsyncClient/DownloadFileInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected async Task<bool> DownloadFileInternalAsync(string localPath, string re

const int rateControlResolution = 100;
var rateLimitBytes = Config.DownloadRateLimit != 0 ? (long)Config.DownloadRateLimit * 1024 : 0;
var chunkSize = CalculateTransferChunkSize(rateLimitBytes, rateControlResolution);
var chunkSize = FileTransferModule.CalculateTransferChunkSize(this, rateLimitBytes, rateControlResolution);

var buffer = new byte[chunkSize];
var offset = restartPosition;
Expand Down
3 changes: 2 additions & 1 deletion FluentFTP/Client/AsyncClient/DownloadFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluentFTP.Rules;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -63,7 +64,7 @@ public async Task<List<FtpResult>> DownloadFiles(string localDir, IEnumerable<st
localDir = !localDir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? localDir + Path.DirectorySeparatorChar.ToString() : localDir;

// check which files should be downloaded or filtered out based on rules
var filesToDownload = GetFilesToDownload2(localDir, remotePaths, rules, results, shouldExist);
var filesToDownload = FileDownloadModule.GetFilesToDownload2(this, localDir, remotePaths, rules, results, shouldExist);

// per remote file
var r = -1;
Expand Down
8 changes: 4 additions & 4 deletions FluentFTP/Client/AsyncClient/GetListing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class AsyncFtpClient {
public async IAsyncEnumerable<FtpListItem> GetListingEnumerable(string path, FtpListOption options, CancellationToken token = default(CancellationToken), [EnumeratorCancellation] CancellationToken enumToken = default(CancellationToken)) {

// start recursive process if needed and unsupported by the server
if (options.HasFlag(FtpListOption.Recursive) && !IsServerSideRecursionSupported(options)) {
if (options.HasFlag(FtpListOption.Recursive) && !ListingModule.IsServerSideRecursionSupported(this, options)) {
await foreach (FtpListItem i in GetListingRecursiveEnumerable(await GetAbsolutePathAsync(path, token), options, token, enumToken)) {
yield return i;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public partial class AsyncFtpClient {
}

bool machineList;
CalculateGetListingCommand(path, options, out listcmd, out machineList);
ListingModule.CalculateGetListingCommand(this, path, options, out listcmd, out machineList);

if (autoNav) {
pwdSave = await GetWorkingDirectory(token);
Expand Down Expand Up @@ -170,7 +170,7 @@ public partial class AsyncFtpClient {
public async Task<FtpListItem[]> GetListing(string path, FtpListOption options, CancellationToken token = default(CancellationToken)) {

// start recursive process if needed and unsupported by the server
if (options.HasFlag(FtpListOption.Recursive) && !IsServerSideRecursionSupported(options)) {
if (options.HasFlag(FtpListOption.Recursive) && !ListingModule.IsServerSideRecursionSupported(this, options)) {
return await GetListingRecursive(await GetAbsolutePathAsync(path, token), options, token);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ public partial class AsyncFtpClient {
}

bool machineList;
CalculateGetListingCommand(path, options, out listcmd, out machineList);
ListingModule.CalculateGetListingCommand(this, path, options, out listcmd, out machineList);

if (autoNav) {
pwdSave = await GetWorkingDirectory(token);
Expand Down
5 changes: 3 additions & 2 deletions FluentFTP/Client/AsyncClient/OpenPassiveDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluentFTP.Exceptions;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -53,7 +54,7 @@ public partial class AsyncFtpClient {
}

// read the connection port from the EPSV response
GetEnhancedPassivePort(reply, out host, out port);
PassivePortModule.GetEnhancedPassivePort(this, reply, out host, out port);

}
else {
Expand All @@ -72,7 +73,7 @@ public partial class AsyncFtpClient {
}

// get the passive port taking proxy config into account (if any)
GetPassivePort(type, reply, out host, out port);
PassivePortModule.GetPassivePort(this, type, reply, out host, out port);

}

Expand Down
7 changes: 4 additions & 3 deletions FluentFTP/Client/AsyncClient/TransferDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentFTP.Helpers;
using System.Threading;
using System.Threading.Tasks;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -88,7 +89,7 @@ public async Task<List<FtpResult>> TransferDirectory(string sourceFolder, AsyncF
token.ThrowIfCancellationRequested();

// loop through each folder and ensure it exists #1
var dirsToUpload = GetSubDirectoriesToTransfer(sourceFolder, remoteFolder, rules, results, dirListing);
var dirsToUpload = DirectoryModule.GetSubDirectoriesToTransfer(this, sourceFolder, remoteFolder, rules, results, dirListing);

// break if task is cancelled
token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -139,7 +140,7 @@ protected async Task<List<FtpResult>> GetFilesToTransfer(string sourceFolder, st
results.Add(result);

// skip transferring the file if it does not pass all the rules
if (!FilePassesRules(result, rules, true)) {
if (!FileRuleModule.FilePassesRules(this, result, rules, true)) {
continue;
}

Expand Down Expand Up @@ -169,7 +170,7 @@ protected async Task TransferServerFiles(List<FtpResult> filesToTransfer, AsyncF

// skip uploading if the file already exists on the server
FtpRemoteExists existsModeToUse;
if (!CanUploadFile(result, remoteListing, existsMode, out existsModeToUse)) {
if (!FileUploadModule.CanUploadFile(this, result, remoteListing, existsMode, out existsModeToUse)) {
continue;
}

Expand Down
9 changes: 5 additions & 4 deletions FluentFTP/Client/AsyncClient/UploadDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentFTP.Helpers;
using System.Threading;
using System.Threading.Tasks;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -88,7 +89,7 @@ public async Task<List<FtpResult>> UploadDirectory(string localFolder, string re
token.ThrowIfCancellationRequested();

// loop through each folder and ensure it exists #1
var dirsToUpload = GetSubDirectoriesToUpload(localFolder, remoteFolder, rules, results, dirListing);
var dirsToUpload = DirectoryModule.GetSubDirectoriesToUpload(this, localFolder, remoteFolder, rules, results, dirListing);

// break if task is cancelled
token.ThrowIfCancellationRequested();
Expand All @@ -104,7 +105,7 @@ public async Task<List<FtpResult>> UploadDirectory(string localFolder, string re
var fileListing = Directory.GetFiles(localFolder, "*.*", SearchOption.AllDirectories);

// loop through each file and transfer it
var filesToUpload = GetFilesToUpload(localFolder, remoteFolder, rules, results, shouldExist, fileListing);
var filesToUpload = FileUploadModule.GetFilesToUpload(this, localFolder, remoteFolder, rules, results, shouldExist, fileListing);
await UploadDirectoryFiles(filesToUpload, existsMode, verifyOptions, progress, remoteListing, token);

// delete the extra remote files if in mirror mode and the directory was pre-existing
Expand Down Expand Up @@ -158,7 +159,7 @@ protected async Task UploadDirectoryFiles(List<FtpResult> filesToUpload, FtpRemo

// skip uploading if the file already exists on the server
FtpRemoteExists existsModeToUse;
if (!CanUploadFile(result, remoteListing, existsMode, out existsModeToUse)) {
if (!FileUploadModule.CanUploadFile(this, result, remoteListing, existsMode, out existsModeToUse)) {
continue;
}

Expand Down Expand Up @@ -199,7 +200,7 @@ protected async Task DeleteExtraServerFiles(FtpFolderSyncMode mode, string remot
if (!shouldExist.ContainsKey(existingServerFile.FullName.ToLower())) {

// only delete the remote file if its permitted by the configuration
if (CanDeleteRemoteFile(rules, existingServerFile)) {
if (DirectoryModule.CanDeleteRemoteFile(this, rules, existingServerFile)) {
LogWithPrefix(FtpTraceLevel.Info, "Delete extra file from server: " + existingServerFile.FullName);

// delete the file from the server
Expand Down
7 changes: 4 additions & 3 deletions FluentFTP/Client/AsyncClient/UploadFileInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Security.Authentication;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -64,7 +65,7 @@ protected async Task<FtpStatus> UploadFileInternalAsync(Stream fileData, string
remoteFileLen = remotePosition = await GetFileSize(remotePath, 0, token);

// calculate the local position for appending / resuming
localPosition = CalculateAppendLocalPosition(remotePath, existsMode, remotePosition);
localPosition = FileTransferModule.CalculateAppendLocalPosition(remotePath, existsMode, remotePosition);

}
else {
Expand Down Expand Up @@ -102,7 +103,7 @@ protected async Task<FtpStatus> UploadFileInternalAsync(Stream fileData, string
remoteFileLen = remotePosition = await GetFileSize(remotePath, 0, token);

// calculate the local position for appending / resuming
localPosition = CalculateAppendLocalPosition(remotePath, existsMode, remotePosition);
localPosition = FileTransferModule.CalculateAppendLocalPosition(remotePath, existsMode, remotePosition);
}

}
Expand Down Expand Up @@ -158,7 +159,7 @@ protected async Task<FtpStatus> UploadFileInternalAsync(Stream fileData, string
// calculate chunk size and rate limiting
const int rateControlResolution = 100;
long rateLimitBytes = Config.UploadRateLimit != 0 ? (long)Config.UploadRateLimit * 1024 : 0;
var chunkSize = CalculateTransferChunkSize(rateLimitBytes, rateControlResolution);
var chunkSize = FileTransferModule.CalculateTransferChunkSize(this, rateLimitBytes, rateControlResolution);

// calc desired length based on the mode (if need to append to the end of remote file, length is sum of local+remote)
var remoteFileDesiredLen = (existsMode is FtpRemoteExists.AddToEnd or FtpRemoteExists.AddToEndNoCheck) ?
Expand Down
3 changes: 2 additions & 1 deletion FluentFTP/Client/AsyncClient/UploadFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Data;
using FluentFTP.Rules;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -214,7 +215,7 @@ protected async Task<List<FtpResult>> GetFilesToUpload2Async(IEnumerable<string>
remoteFilePath = await GetAbsoluteFilePathAsync(remoteDir, fileName, token);

// record that this file should be uploaded
RecordFileToUpload(rules, results, shouldExist, filesToUpload, localPath, remoteFilePath);
FileUploadModule.RecordFileToUpload(this, rules, results, shouldExist, filesToUpload, localPath, remoteFilePath);
}

return filesToUpload;
Expand Down
34 changes: 0 additions & 34 deletions FluentFTP/Client/BaseClient/CalculateAppendLocalPosition.cs

This file was deleted.

29 changes: 0 additions & 29 deletions FluentFTP/Client/BaseClient/CalculateTransferChunkSize.cs

This file was deleted.

25 changes: 0 additions & 25 deletions FluentFTP/Client/BaseClient/CanUploadFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,5 @@
namespace FluentFTP.Client.BaseClient {
public partial class BaseFtpClient {

/// <summary>
/// Check if the file is cleared to be uploaded, taking its existence/filesize and existsMode options into account.
/// </summary>
protected bool CanUploadFile(FtpResult result, FtpListItem[] remoteListing, FtpRemoteExists existsMode, out FtpRemoteExists existsModeToUse) {

// check if the file already exists on the server
existsModeToUse = existsMode;
var fileExists = FileListings.FileExistsInListing(remoteListing, result.RemotePath);

// if we want to skip uploaded files and the file already exists, mark its skipped
if (existsMode == FtpRemoteExists.Skip && fileExists) {

LogWithPrefix(FtpTraceLevel.Info, "Skipped file that already exists: " + result.LocalPath);

result.IsSuccess = true;
result.IsSkipped = true;
return false;
}

// in any mode if the file does not exist, mark that exists check is not required
if (!fileExists) {
existsModeToUse = existsMode == FtpRemoteExists.Resume ? FtpRemoteExists.ResumeNoCheck : FtpRemoteExists.NoCheck;
}
return true;
}
}
}
Loading

0 comments on commit 04134ab

Please sign in to comment.