Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

CopyFilesToDirectory will now allow for one duplicate copy over and put (2) (#1126)

Joseph Milazzofe865feca64c
diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs
index 89981d43f..da9e4698b 100644
--- a/API/Services/DirectoryService.cs
+++ b/API/Services/DirectoryService.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
@@ -354,6 +354,7 @@ namespace API.Services
/// <summary>
/// Copies files to a destination directory. If the destination directory doesn't exist, this will create it.
/// </summary>
+ /// <remarks>If a file already exists in dest, this will rename as (2). It does not support multiple iterations of this. Overwriting is not supported.</remarks>
/// <param name="filePaths"></param>
/// <param name="directoryPath"></param>
/// <param name="prepend">An optional string to prepend to the target file's name</param>
@@ -370,7 +371,16 @@ namespace API.Services
var fileInfo = FileSystem.FileInfo.FromFileName(file);
if (fileInfo.Exists)
{
- fileInfo.CopyTo(FileSystem.Path.Join(directoryPath, prepend + fileInfo.Name));
+ // TODO: I need to handle if file already exists and allow either an overwrite or prepend (2) to it
+ try
+ {
+ fileInfo.CopyTo(FileSystem.Path.Join(directoryPath, prepend + fileInfo.Name));
+ }
+ catch (IOException ex)
+ {
+ _logger.LogError(ex, "File copy, dest already exists. Appending (2)");
+ fileInfo.CopyTo(FileSystem.Path.Join(directoryPath, prepend + FileSystem.Path.GetFileNameWithoutExtension(fileInfo.Name) + " (2)" + FileSystem.Path.GetExtension(fileInfo.Name)));
+ }
}
else
{