2023-11-12 14:30:23 +01:00
|
|
|
const { parseNfoMetadata } = require('../utils/parsers/parseNfoMetadata')
|
|
|
|
const { readTextFile } = require('../utils/fileUtils')
|
|
|
|
|
|
|
|
class NfoFileScanner {
|
2024-07-18 23:09:40 +02:00
|
|
|
constructor() {}
|
2023-11-12 14:30:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse metadata from .nfo file found in library scan and update bookMetadata
|
2024-07-18 23:09:40 +02:00
|
|
|
*
|
|
|
|
* @param {import('../models/LibraryItem').LibraryFileObject} nfoLibraryFileObj
|
|
|
|
* @param {Object} bookMetadata
|
2023-11-12 14:30:23 +01:00
|
|
|
*/
|
|
|
|
async scanBookNfoFile(nfoLibraryFileObj, bookMetadata) {
|
|
|
|
const nfoText = await readTextFile(nfoLibraryFileObj.metadata.path)
|
|
|
|
const nfoMetadata = nfoText ? await parseNfoMetadata(nfoText) : null
|
|
|
|
if (nfoMetadata) {
|
|
|
|
for (const key in nfoMetadata) {
|
2024-07-18 23:09:40 +02:00
|
|
|
if (key === 'tags') {
|
|
|
|
// Add tags only if tags are empty
|
2023-11-12 14:30:23 +01:00
|
|
|
if (nfoMetadata.tags.length) {
|
|
|
|
bookMetadata.tags = nfoMetadata.tags
|
|
|
|
}
|
2024-07-18 23:09:40 +02:00
|
|
|
} else if (key === 'genres') {
|
|
|
|
// Add genres only if genres are empty
|
2023-11-12 14:30:23 +01:00
|
|
|
if (nfoMetadata.genres.length) {
|
|
|
|
bookMetadata.genres = nfoMetadata.genres
|
|
|
|
}
|
|
|
|
} else if (key === 'authors') {
|
|
|
|
if (nfoMetadata.authors?.length) {
|
|
|
|
bookMetadata.authors = nfoMetadata.authors
|
|
|
|
}
|
|
|
|
} else if (key === 'narrators') {
|
|
|
|
if (nfoMetadata.narrators?.length) {
|
|
|
|
bookMetadata.narrators = nfoMetadata.narrators
|
|
|
|
}
|
|
|
|
} else if (key === 'series') {
|
|
|
|
if (nfoMetadata.series) {
|
2024-07-18 23:09:40 +02:00
|
|
|
bookMetadata.series = [
|
|
|
|
{
|
|
|
|
name: nfoMetadata.series,
|
|
|
|
sequence: nfoMetadata.sequence || null
|
|
|
|
}
|
|
|
|
]
|
2024-07-17 17:50:27 +02:00
|
|
|
}
|
2023-11-12 14:30:23 +01:00
|
|
|
} else if (nfoMetadata[key] && key !== 'sequence') {
|
|
|
|
bookMetadata[key] = nfoMetadata[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 23:09:40 +02:00
|
|
|
module.exports = new NfoFileScanner()
|