mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
refactor: simplify episode metadata writer
Remove verbose JSDoc, shorten variable names, match existing code style. Rename saveEpisodeMetadataFiles -> writeEpisodeMetadata.
This commit is contained in:
parent
49c06f16f5
commit
9477d9a808
1 changed files with 24 additions and 38 deletions
|
|
@ -477,9 +477,8 @@ class PodcastScanner {
|
|||
|
||||
libraryScan.addLog(LogLevel.DEBUG, `Success saving abmetadata to "${metadataFilePath}"`)
|
||||
|
||||
// Write per-episode metadata files alongside each episode's audio file
|
||||
if (storeMetadataWithItem && libraryItem.media.podcastEpisodes?.length) {
|
||||
await this.saveEpisodeMetadataFiles(libraryItem, libraryItem.media.podcastEpisodes, libraryScan)
|
||||
await this.writeEpisodeMetadata(libraryItem, libraryItem.media.podcastEpisodes, libraryScan)
|
||||
}
|
||||
|
||||
return metadataLibraryFile
|
||||
|
|
@ -490,48 +489,35 @@ class PodcastScanner {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Write per-episode metadata files alongside each episode's audio file.
|
||||
* For an episode at /path/to/episode.mp3, writes /path/to/episode.json
|
||||
* containing title, description, pubDate, chapters, etc.
|
||||
*
|
||||
* @param {import('../models/LibraryItem')} libraryItem
|
||||
* @param {import('../models/PodcastEpisode')[]} podcastEpisodes
|
||||
* @param {import('./LibraryScan')} libraryScan
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async saveEpisodeMetadataFiles(libraryItem, podcastEpisodes, libraryScan) {
|
||||
for (const episode of podcastEpisodes) {
|
||||
if (!episode.audioFile?.metadata?.path) continue
|
||||
async writeEpisodeMetadata(libraryItem, episodes, libraryScan) {
|
||||
for (const ep of episodes) {
|
||||
if (!ep.audioFile?.metadata?.path) continue
|
||||
|
||||
const audioFilePath = episode.audioFile.metadata.path
|
||||
const episodeMetadataPath = audioFilePath.replace(/\.[^.]+$/, '.json')
|
||||
|
||||
const episodeJson = {
|
||||
title: episode.title,
|
||||
subtitle: episode.subtitle,
|
||||
season: episode.season,
|
||||
episode: episode.episode,
|
||||
episodeType: episode.episodeType,
|
||||
pubDate: episode.pubDate,
|
||||
publishedAt: episode.publishedAt,
|
||||
description: episode.description,
|
||||
chapters: episode.chapters || []
|
||||
const metaPath = ep.audioFile.metadata.path.replace(/\.[^.]+$/, '.json')
|
||||
const data = {
|
||||
title: ep.title,
|
||||
subtitle: ep.subtitle,
|
||||
season: ep.season,
|
||||
episode: ep.episode,
|
||||
episodeType: ep.episodeType,
|
||||
pubDate: ep.pubDate,
|
||||
publishedAt: ep.publishedAt,
|
||||
description: ep.description,
|
||||
chapters: ep.chapters || []
|
||||
}
|
||||
|
||||
try {
|
||||
await fsExtra.writeFile(episodeMetadataPath, JSON.stringify(episodeJson, null, 2))
|
||||
libraryScan.addLog(LogLevel.DEBUG, `Saved episode metadata to "${episodeMetadataPath}"`)
|
||||
await fsExtra.writeFile(metaPath, JSON.stringify(data, null, 2))
|
||||
libraryScan.addLog(LogLevel.DEBUG, `Saved episode meta: ${metaPath}`)
|
||||
|
||||
// Add to libraryFiles if not already tracked
|
||||
const metadataLibraryFile = libraryItem.libraryFiles.find((lf) => lf.metadata.path === filePathToPOSIX(episodeMetadataPath))
|
||||
if (!metadataLibraryFile) {
|
||||
const newLibraryFile = new LibraryFile()
|
||||
await newLibraryFile.setDataFromPath(episodeMetadataPath, Path.basename(episodeMetadataPath))
|
||||
libraryItem.libraryFiles.push(newLibraryFile.toJSON())
|
||||
const exists = libraryItem.libraryFiles.find((f) => f.metadata.path === filePathToPOSIX(metaPath))
|
||||
if (!exists) {
|
||||
const lf = new LibraryFile()
|
||||
await lf.setDataFromPath(metaPath, Path.basename(metaPath))
|
||||
libraryItem.libraryFiles.push(lf.toJSON())
|
||||
}
|
||||
} catch (error) {
|
||||
libraryScan.addLog(LogLevel.ERROR, `Failed to save episode metadata at "${episodeMetadataPath}"`, error)
|
||||
} catch (err) {
|
||||
libraryScan.addLog(LogLevel.ERROR, `Failed writing ${metaPath}: ${err.message}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue