Skip to content

Commit

Permalink
Supporting folders in AwaitFileCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetdummy committed Apr 10, 2024
1 parent 249d0a9 commit fc65a44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions TorrentExtractor/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.IO;
using System.Linq;

namespace TorrentExtractor;

public static class Extensions
{
public static long Length(this DirectoryInfo dir) =>
dir.GetFiles().Sum(fi => fi.Length) + dir.GetDirectories().Sum(di => di.Length());
}
18 changes: 14 additions & 4 deletions TorrentExtractor/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ await ExtractAndMoveAsync(

private async Task AwaitFileCopy(
string sourcePath,
long previousFileLength,
long previousLength,
Core coreSettings,
CancellationToken cancellationToken
)
Expand All @@ -140,10 +140,20 @@ CancellationToken cancellationToken
throw new TaskCanceledException();
}

if (
string.IsNullOrWhiteSpace(sourcePath)
|| !File.Exists(sourcePath) && !Directory.Exists(sourcePath)
)
{
throw new FileNotFoundException("Source path is not found!", sourcePath);
}

var interval = coreSettings.FileCompareInterval;
var fileLength = new FileInfo(sourcePath).Length;
var length = Directory.Exists(sourcePath)
? new DirectoryInfo(sourcePath).Length()
: new FileInfo(sourcePath).Length;

if (fileLength != previousFileLength)
if (length != previousLength)
{
_logger.LogInformation(
"File '{SourcePath}' is still being copied. Waiting for {Interval} seconds...",
Expand All @@ -152,7 +162,7 @@ CancellationToken cancellationToken
);

await Task.Delay(TimeSpan.FromSeconds(interval), cancellationToken);
await AwaitFileCopy(sourcePath, fileLength, coreSettings, cancellationToken);
await AwaitFileCopy(sourcePath, length, coreSettings, cancellationToken);
}
}

Expand Down

0 comments on commit fc65a44

Please sign in to comment.