mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-10-17 13:19:18 +02:00
onWikipediaDownloadComplete was deleting every file whose name starts with `wikipedia_en_`, treating distinct corpora (simple, medicine, wikivoyage, climate_change, etc.) as competing versions of the same selection slot. Whichever wiki finished second silently wiped the other from disk. Match by filename stem instead — strip the trailing `_YYYY-MM(-DD).zim` date suffix and only delete files with the same stem as the new download. Different release dates of the same variant still get cleaned up; distinct variants are preserved. Extracted the predicate to `app/utils/zim_filename.ts` so the boundary is covered by unit tests (8 cases incl. the #884 repro scenario).
27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
/**
|
|
* Strip the trailing `_YYYY-MM(-DD).zim` date suffix from a Kiwix-style ZIM
|
|
* filename so different release dates of the same variant share a stem
|
|
* (e.g., `wikipedia_en_all_nopic`) while distinct corpora keep distinct stems
|
|
* (`wikipedia_en_simple_all_nopic`, `wikipedia_en_medicine_nopic`, etc.).
|
|
*/
|
|
export function zimFilenameStem(name: string): string {
|
|
return name.replace(/_\d{4}-\d{2}(?:-\d{2})?\.zim$/i, '')
|
|
}
|
|
|
|
/**
|
|
* Of the existing files, return only those that are prior-version replacements
|
|
* of `currentFilename` — same Wikipedia variant stem, different release. Used
|
|
* by the post-download cleanup to avoid deleting unrelated Wikipedia corpora
|
|
* the user has installed independently (issue #884).
|
|
*/
|
|
export function findReplacedWikipediaFiles(
|
|
currentFilename: string,
|
|
existingNames: string[]
|
|
): string[] {
|
|
const currentStem = zimFilenameStem(currentFilename)
|
|
return existingNames.filter(
|
|
(n) =>
|
|
n.startsWith('wikipedia_en_') && n !== currentFilename && zimFilenameStem(n) === currentStem
|
|
)
|
|
}
|