This commit is contained in:
cuyua9 2026-05-21 11:46:31 +00:00 committed by GitHub
commit d8fb36ccf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View File

@ -187,6 +187,35 @@ export class KiwixLibraryService {
.filter((b) => b.id && b.path)
}
private _validateLibraryXml(xmlContent: string): void {
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@_',
isArray: (name) => name === 'book',
})
const parsed = parser.parse(xmlContent)
if (!parsed?.library || typeof parsed.library !== 'object') {
throw new Error('Kiwix library XML is missing the library root element.')
}
}
async ensureValidLibraryXml(): Promise<boolean> {
try {
const content = await readFile(this.getLibraryFilePath(), 'utf-8')
this._validateLibraryXml(content)
return false
} catch (err: any) {
if (err.code && err.code !== 'ENOENT') {
throw err
}
logger.warn('[KiwixLibraryService] Library XML is missing or invalid; rebuilding from disk.')
await this.rebuildFromDisk()
return true
}
}
async rebuildFromDisk(opts?: { excludeFilenames?: string[] }): Promise<void> {
const dirPath = join(process.cwd(), ZIM_STORAGE_PATH)
await ensureDirectoryExists(dirPath)

View File

@ -22,6 +22,7 @@ export default class KiwixMigrationProvider {
const Service = (await import('#models/service')).default
const { SERVICE_NAMES } = await import('../constants/service_names.js')
const { DockerService } = await import('#services/docker_service')
const { KiwixLibraryService } = await import('#services/kiwix_library_service')
const kiwixService = await Service.query()
.where('service_name', SERVICE_NAMES.KIWIX)
@ -34,9 +35,15 @@ export default class KiwixMigrationProvider {
const dockerService = new DockerService()
const isLegacy = await dockerService.isKiwixOnLegacyConfig()
const kiwixLibraryService = new KiwixLibraryService()
if (!isLegacy) {
logger.info('[KiwixMigrationProvider] Kiwix is already in library mode — no migration needed.')
const rebuilt = await kiwixLibraryService.ensureValidLibraryXml()
if (rebuilt) {
logger.info('[KiwixMigrationProvider] Rebuilt missing or invalid Kiwix library XML.')
} else {
logger.info('[KiwixMigrationProvider] Kiwix is already in library mode — no migration needed.')
}
return
}