From ddb22ba6d2a6aacc817b3f81716f4b590d46e923 Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Sun, 8 Sep 2024 16:12:04 -0500 Subject: [PATCH] Fixed a bug where there could be a duplicate key in OPDS for some unique configurations. --- API/Controllers/OPDSController.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/API/Controllers/OPDSController.cs b/API/Controllers/OPDSController.cs index d25321ad8a..b025be4a4a 100644 --- a/API/Controllers/OPDSController.cs +++ b/API/Controllers/OPDSController.cs @@ -873,6 +873,7 @@ public async Task GetSeries(string apiKey, int seriesId) feed.Links.Add(CreateLink(FeedLinkRelation.Image, FeedLinkType.Image, $"{baseUrl}api/image/series-cover?seriesId={seriesId}&apiKey={apiKey}")); var chapterDict = new Dictionary(); + var fileDict = new Dictionary(); var seriesDetail = await _seriesService.GetSeriesDetail(seriesId, userId); foreach (var volume in seriesDetail.Volumes) { @@ -881,12 +882,14 @@ public async Task GetSeries(string apiKey, int seriesId) foreach (var chapter in chaptersForVolume) { var chapterId = chapter.Id; - if (chapterDict.ContainsKey(chapterId)) continue; + if (!chapterDict.TryAdd(chapterId, 0)) continue; var chapterDto = _mapper.Map(chapter); foreach (var mangaFile in chapter.Files) { - chapterDict.Add(chapterId, 0); + // If a chapter has multiple files that are within one chapter, this dict prevents duplicate key exception + if (!fileDict.TryAdd(mangaFile.Id, 0)) continue; + feed.Entries.Add(await CreateChapterWithFile(userId, seriesId, volume.Id, chapterId, _mapper.Map(mangaFile), series, chapterDto, apiKey, prefix, baseUrl)); } @@ -905,6 +908,8 @@ public async Task GetSeries(string apiKey, int seriesId) var chapterDto = _mapper.Map(chapter); foreach (var mangaFile in files) { + // If a chapter has multiple files that are within one chapter, this dict prevents duplicate key exception + if (!fileDict.TryAdd(mangaFile.Id, 0)) continue; feed.Entries.Add(await CreateChapterWithFile(userId, seriesId, chapter.VolumeId, chapter.Id, _mapper.Map(mangaFile), series, chapterDto, apiKey, prefix, baseUrl)); } @@ -916,6 +921,9 @@ public async Task GetSeries(string apiKey, int seriesId) var chapterDto = _mapper.Map(special); foreach (var mangaFile in files) { + // If a chapter has multiple files that are within one chapter, this dict prevents duplicate key exception + if (!fileDict.TryAdd(mangaFile.Id, 0)) continue; + feed.Entries.Add(await CreateChapterWithFile(userId, seriesId, special.VolumeId, special.Id, _mapper.Map(mangaFile), series, chapterDto, apiKey, prefix, baseUrl)); }