|
|
|
@ -17,7 +17,8 @@ public static class FileTransfers
|
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="totalBytes"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public static ulong GetBufferSize(ulong totalBytes) => totalBytes switch |
|
|
|
|
public static ulong GetBufferSize(ulong totalBytes) => |
|
|
|
|
totalBytes switch |
|
|
|
|
{ |
|
|
|
|
< Size.MiB => 8 * Size.KiB, |
|
|
|
|
< 100 * Size.MiB => 16 * Size.KiB, |
|
|
|
@ -42,7 +43,11 @@ public static class FileTransfers
|
|
|
|
|
/// <param name="totalProgress"> |
|
|
|
|
/// Optional (total) progress. |
|
|
|
|
/// </param> |
|
|
|
|
public static async Task CopyFiles(Dictionary<string, string> files, IProgress<ProgressReport>? fileProgress = default, IProgress<ProgressReport>? totalProgress = default) |
|
|
|
|
public static async Task CopyFiles( |
|
|
|
|
Dictionary<string, string> files, |
|
|
|
|
IProgress<ProgressReport>? fileProgress = default, |
|
|
|
|
IProgress<ProgressReport>? totalProgress = default |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
var totalFiles = files.Count; |
|
|
|
|
var completedFiles = 0; |
|
|
|
@ -53,19 +58,48 @@ public static class FileTransfers
|
|
|
|
|
{ |
|
|
|
|
var totalReadForFile = 0ul; |
|
|
|
|
|
|
|
|
|
await using var outStream = new FileStream(destPath, FileMode.Create, FileAccess.Write, FileShare.Read); |
|
|
|
|
await using var inStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read); |
|
|
|
|
await using var outStream = new FileStream( |
|
|
|
|
destPath, |
|
|
|
|
FileMode.Create, |
|
|
|
|
FileAccess.Write, |
|
|
|
|
FileShare.Read |
|
|
|
|
); |
|
|
|
|
await using var inStream = new FileStream( |
|
|
|
|
sourcePath, |
|
|
|
|
FileMode.Open, |
|
|
|
|
FileAccess.Read, |
|
|
|
|
FileShare.Read |
|
|
|
|
); |
|
|
|
|
var fileSize = (ulong)inStream.Length; |
|
|
|
|
var fileName = Path.GetFileName(sourcePath); |
|
|
|
|
completedFiles++; |
|
|
|
|
await CopyStream(inStream , outStream, fileReadBytes => |
|
|
|
|
await CopyStream( |
|
|
|
|
inStream, |
|
|
|
|
outStream, |
|
|
|
|
fileReadBytes => |
|
|
|
|
{ |
|
|
|
|
var lastRead = totalReadForFile; |
|
|
|
|
totalReadForFile = Convert.ToUInt64(fileReadBytes); |
|
|
|
|
totalRead += totalReadForFile - lastRead; |
|
|
|
|
fileProgress?.Report(new ProgressReport(totalReadForFile, fileSize, fileName, $"{completedFiles}/{totalFiles}")); |
|
|
|
|
totalProgress?.Report(new ProgressReport(totalRead, totalSize, fileName, $"{completedFiles}/{totalFiles}")); |
|
|
|
|
} ); |
|
|
|
|
fileProgress?.Report( |
|
|
|
|
new ProgressReport( |
|
|
|
|
totalReadForFile, |
|
|
|
|
fileSize, |
|
|
|
|
fileName, |
|
|
|
|
$"{completedFiles}/{totalFiles}" |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
totalProgress?.Report( |
|
|
|
|
new ProgressReport( |
|
|
|
|
totalRead, |
|
|
|
|
totalSize, |
|
|
|
|
fileName, |
|
|
|
|
$"{completedFiles}/{totalFiles}" |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
.ConfigureAwait(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -104,7 +138,8 @@ public static class FileTransfers
|
|
|
|
|
DirectoryPath sourceDir, |
|
|
|
|
DirectoryPath destinationDir, |
|
|
|
|
bool overwrite = false, |
|
|
|
|
bool overwriteIfHashMatches = false) |
|
|
|
|
bool overwriteIfHashMatches = false |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
// Create the destination directory if it doesn't exist |
|
|
|
|
if (!destinationDir.Exists) |
|
|
|
@ -120,8 +155,13 @@ public static class FileTransfers
|
|
|
|
|
{ |
|
|
|
|
var destinationSubDir = destinationDir.JoinDir(subDir.Name); |
|
|
|
|
// Recursively move sub directories |
|
|
|
|
await MoveAllFilesAndDirectories(subDir, destinationSubDir, |
|
|
|
|
overwrite, overwriteIfHashMatches); |
|
|
|
|
await MoveAllFilesAndDirectories( |
|
|
|
|
subDir, |
|
|
|
|
destinationSubDir, |
|
|
|
|
overwrite, |
|
|
|
|
overwriteIfHashMatches |
|
|
|
|
) |
|
|
|
|
.ConfigureAwait(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -137,7 +177,8 @@ public static class FileTransfers
|
|
|
|
|
DirectoryPath sourceDir, |
|
|
|
|
DirectoryPath destinationDir, |
|
|
|
|
bool overwrite = false, |
|
|
|
|
bool overwriteIfHashMatches = false) |
|
|
|
|
bool overwriteIfHashMatches = false |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
foreach (var file in sourceDir.Info.EnumerateFiles()) |
|
|
|
|
{ |
|
|
|
@ -160,8 +201,9 @@ public static class FileTransfers
|
|
|
|
|
if (sourceHash == destinationHash) |
|
|
|
|
{ |
|
|
|
|
Logger.Info( |
|
|
|
|
$"Deleted source file {file.Name} as it already exists in {destinationDir}." + |
|
|
|
|
$" Matching Blake3 hash: {sourceHash}"); |
|
|
|
|
$"Deleted source file {file.Name} as it already exists in {destinationDir}." |
|
|
|
|
+ $" Matching Blake3 hash: {sourceHash}" |
|
|
|
|
); |
|
|
|
|
sourceFile.Delete(); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|