diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Files/FileService.cs | 26 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 6 |
2 files changed, 18 insertions, 14 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 18bf72d1..e5ce63f9 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -301,14 +301,14 @@ namespace slskd.Files /// <param name="sourceFilename">The fully qualified filename of the file to move.</param> /// <param name="destinationDirectory">The fully qualified directory to which to move the source file.</param> /// <param name="overwrite">An optional value indicating whether the destination file should be overwritten if it already exists.</param> - /// <param name="deleteEmptiedParentDirectory"> + /// <param name="deleteSourceDirectoryIfEmptyAfterMove"> /// An optional value indicating whether the parent directory of the source file should be deleted if it is empty after the move. /// </param> /// <returns>The fully qualified filename of the resulting file.</returns> /// <exception cref="ArgumentNullException">Thrown if either of the specified file or directories are null or contain only whitespace.</exception> /// <exception cref="FileNotFoundException">Thrown if the specified <paramref name="sourceFilename"/> does not exist.</exception> - /// <exception cref="IOException">Thrown if the file can't be moved, or the <paramref name="deleteEmptiedParentDirectory"/> option is set and the operation fails.</exception> - public virtual string MoveFile(string sourceFilename, string destinationDirectory, bool overwrite = false, bool deleteEmptiedParentDirectory = false) + /// <exception cref="IOException">Thrown if the file can't be moved, or the <paramref name="deleteSourceDirectoryIfEmptyAfterMove"/> option is set and the operation fails.</exception> + public virtual string MoveFile(string sourceFilename, string destinationDirectory, bool overwrite = false, bool deleteSourceDirectoryIfEmptyAfterMove = false) { ArgumentNullException.ThrowIfNullOrWhiteSpace(sourceFilename, nameof(sourceFilename)); ArgumentNullException.ThrowIfNullOrWhiteSpace(destinationDirectory, nameof(destinationDirectory)); @@ -323,18 +323,20 @@ namespace slskd.Files Directory.CreateDirectory(destinationDirectory); } - var filename = Path.GetFileName(sourceFilename); // without directory - var destinationFilename = Path.Combine(destinationDirectory, filename); + var destinationFilename = Path.Combine(destinationDirectory, Path.GetFileName(sourceFilename)); + + Log.Debug("Attempting to move {Source} to {Destination}", sourceFilename, destinationFilename); if (!overwrite && File.Exists(destinationFilename)) { - string extensionlessFilename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename)); - string extension = Path.GetExtension(filename); + Log.Debug("Destination file {Destination} exists, and overwite option is not set; attempting to generate new filename", destinationFilename); + + string extensionlessFilename = Path.Combine(Path.GetDirectoryName(destinationFilename), Path.GetFileNameWithoutExtension(sourceFilename)); + string extension = Path.GetExtension(sourceFilename); while (File.Exists(destinationFilename)) { - string filenameUTC = $"{extensionlessFilename}_{DateTime.UtcNow.Ticks}{extension}"; - destinationFilename = filenameUTC.ToLocalFilename(destinationDirectory); + destinationFilename = $"{extensionlessFilename}_{DateTime.UtcNow.Ticks}{extension}"; } } @@ -342,8 +344,10 @@ namespace slskd.Files { File.Move(sourceFilename, destinationFilename, overwrite: overwrite); + Log.Debug("Successfully moved {Source} to {Destination}", sourceFilename, destinationFilename); + // if the parent directory is empty after the move, delete it - if (deleteEmptiedParentDirectory && !Directory.EnumerateFileSystemEntries(Path.GetDirectoryName(sourceFilename)).Any()) + if (deleteSourceDirectoryIfEmptyAfterMove && !Directory.EnumerateFileSystemEntries(Path.GetDirectoryName(sourceFilename)).Any()) { Directory.Delete(Path.GetDirectoryName(sourceFilename)); } @@ -354,7 +358,7 @@ namespace slskd.Files { // the operation above can throw quite a few exceptions, all granular variations of // IOException. to make handling downstream easier, wrap them all up and re-throw. - throw new IOException($"Failed to move file {Path.GetFileName(filename)}: {ex.Message}", ex); + throw new IOException($"Failed to move file {Path.GetFileName(sourceFilename)}: {ex.Message}", ex); } } } diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 8ec7def2..90d40b22 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -1,4 +1,4 @@ -// <copyright file="DownloadService.cs" company="slskd Team"> +// <copyright file="DownloadService.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -318,9 +318,9 @@ namespace slskd.Transfers.Downloads // argue both ways for that being the correct behavior. revisit this later. var finalFilename = Files.MoveFile( sourceFilename: file.Filename.ToLocalFilename(baseDirectory: OptionsMonitor.CurrentValue.Directories.Incomplete), - destinationDirectory: OptionsMonitor.CurrentValue.Directories.Downloads, + destinationDirectory: System.IO.Path.GetDirectoryName(file.Filename.ToLocalFilename(baseDirectory: OptionsMonitor.CurrentValue.Directories.Downloads)), overwrite: false, - deleteEmptiedParentDirectory: true); + deleteSourceDirectoryIfEmptyAfterMove: true); Log.Debug("Moved file to {Destination}", finalFilename); |
