Fixed Delete Series + Issue Covers from Kavita+ (#3784)

This commit is contained in:
Joe Milazzo 2025-05-03 13:46:40 -06:00 committed by GitHub
parent 3a0d33ca13
commit bc41b0256e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 2189 additions and 1596 deletions

View file

@ -69,6 +69,7 @@ public interface IDirectoryService
IEnumerable<string> GetFiles(string path, string fileNameRegex = "", SearchOption searchOption = SearchOption.TopDirectoryOnly);
bool ExistOrCreate(string directoryPath);
void DeleteFiles(IEnumerable<string> files);
void CopyFile(string sourcePath, string destinationPath, bool overwrite = true);
void RemoveNonImages(string directoryName);
void Flatten(string directoryName);
Task<bool> CheckWriteAccess(string directoryName);
@ -937,6 +938,27 @@ public class DirectoryService : IDirectoryService
}
}
public void CopyFile(string sourcePath, string destinationPath, bool overwrite = true)
{
if (!File.Exists(sourcePath))
{
throw new FileNotFoundException("Source file not found", sourcePath);
}
var destinationDirectory = Path.GetDirectoryName(destinationPath);
if (string.IsNullOrEmpty(destinationDirectory))
{
throw new ArgumentException("Destination path does not contain a directory", nameof(destinationPath));
}
if (!Directory.Exists(destinationDirectory))
{
FileSystem.Directory.CreateDirectory(destinationDirectory);
}
FileSystem.File.Copy(sourcePath, destinationPath, overwrite);
}
/// <summary>
/// Returns the human-readable file size for an arbitrary, 64-bit file size
/// <remarks>The default format is "0.## XB", e.g. "4.2 KB" or "1.43 GB"</remarks>
@ -1090,4 +1112,23 @@ public class DirectoryService : IDirectoryService
FlattenDirectory(root, subDirectory, ref directoryIndex);
}
}
/// <summary>
/// If the file is locked or not existing
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool IsFileLocked(string filePath)
{
try
{
if (!File.Exists(filePath)) return false;
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
return false; // If this works, the file is not locked
}
catch (IOException)
{
return true; // File is locked by another process
}
}
}