Skip to content

Commit 8505c59

Browse files
committed
Merge branch 'duplicateEntries'
2 parents 634bff2 + cd45436 commit 8505c59

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

ComicRack.Engine/IO/ComicExporter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class ComicExporter
3939

4040
public event EventHandler<StorageProgressEventArgs> Progress;
4141

42+
public FileIsInDatabaseChecker FileIsInDatabase { get; set; }
43+
public delegate bool FileIsInDatabaseChecker(string targetPath, string sourcePath);
44+
45+
4246
public ComicExporter(IEnumerable<ComicBook> books, ExportSetting setting, int sequence)
4347
{
4448
comicBooks = books.ToList();
@@ -57,6 +61,13 @@ public string Export(IPagePool pagePool)
5761
{
5862
throw new InvalidOperationException(StringUtility.Format(TR.Messages["OutputFileExists", "Output file '{0}' already exists!"], targetPath));
5963
}
64+
// Check if the file already exists in the database.
65+
bool existsInDatabase = FileIsInDatabase?.Invoke(targetPath, ComicBook.FilePath) ?? false;
66+
if (File.Exists(targetPath) && existsInDatabase && setting.Target == ExportTarget.ReplaceSource)
67+
{
68+
// If the file exists in the database and we are replacing the source, we throw an exception
69+
throw new InvalidOperationException(StringUtility.Format(TR.Messages["AlreadyExistsInDatabase", "Resulting operation would result in a duplicate entry in the database, Output file '{0}' already exists in the library"], targetPath));
70+
}
6071
if ((setting.AddToLibrary || setting.Target == ExportTarget.ReplaceSource) && Providers.Readers.GetFormatProviderType(setting.FormatId) == null)
6172
{
6273
throw new ArgumentException(TR.Messages["InvalidExportSettings", "The export settings do not match (e.g. adding a not supported format to the library)"]);

ComicRack.Engine/Metadata/ComicBook/Comparer/ComicBookDublicateComparer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ public class ComicBookDublicateComparer : Comparer<ComicBook>
77
{
88
public override int Compare(ComicBook x, ComicBook y)
99
{
10-
int num = string.Compare(GroupInfo.CompressedName(x.ShadowSeries), GroupInfo.CompressedName(y.ShadowSeries), ignoreCase: true);
10+
int num = string.Compare(x.FilePath, y.FilePath, ignoreCase: true);
11+
if (num != 0)
12+
{
13+
return num;
14+
}
15+
num = string.Compare(GroupInfo.CompressedName(x.ShadowSeries), GroupInfo.CompressedName(y.ShadowSeries), ignoreCase: true);
1116
if (num != 0)
1217
{
1318
return num;

ComicRack.Engine/QueueManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@ public void ExportComic(IEnumerable<ComicBook> cbs, ExportSetting setting, int s
423423
IEnumerable<string> source = (from cb in cbs
424424
where cb.EditMode.IsLocalComic()
425425
select cb.FilePath).ToArray();
426+
427+
428+
//Callback function to check if the file is already in the database, will be checked when calling Export
429+
comicExporter.FileIsInDatabase = (string targetPath, string sourceFile) =>
430+
{
431+
if (string.IsNullOrEmpty(targetPath))
432+
return false;
433+
434+
// If the target path is the same as the source file, we assume it's not a duplicate
435+
return targetPath != sourceFile && DatabaseManager.Database.Books.FindItemByFile(targetPath) != null;
436+
};
437+
426438
outPath = comicExporter.Export(CacheManager.ImagePool);
427439
if (outPath != null)
428440
{

0 commit comments

Comments
 (0)