mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-30 15:55:26 +02:00
Scan for covers now saves covers, server settings to save covers in audiobook folder
This commit is contained in:
parent
8d9d5a8d1b
commit
3dd8dc6dd4
9 changed files with 83 additions and 31 deletions
|
@ -8,7 +8,6 @@ const imageType = require('image-type')
|
|||
const globals = require('./utils/globals')
|
||||
const { CoverDestination } = require('./utils/constants')
|
||||
|
||||
|
||||
class CoverController {
|
||||
constructor(db, MetadataPath, AudiobookPath) {
|
||||
this.db = db
|
||||
|
@ -52,8 +51,8 @@ class CoverController {
|
|||
}
|
||||
}
|
||||
|
||||
// Remove covers in metadata/books/{ID} that dont have the same filename as the new cover
|
||||
async checkBookMetadataCovers(dirpath, newCoverExt) {
|
||||
// Remove covers that dont have the same filename as the new cover
|
||||
async removeOldCovers(dirpath, newCoverExt) {
|
||||
var filesInDir = await this.getFilesInDirectory(dirpath)
|
||||
|
||||
for (let i = 0; i < filesInDir.length; i++) {
|
||||
|
@ -97,17 +96,11 @@ class CoverController {
|
|||
|
||||
var { fullPath, relPath } = this.getCoverDirectory(audiobook)
|
||||
await fs.ensureDir(fullPath)
|
||||
var isStoringInMetadata = relPath.slice(1).startsWith('metadata')
|
||||
|
||||
var coverFilename = `cover${extname}`
|
||||
var coverFullPath = Path.join(fullPath, coverFilename)
|
||||
var coverPath = Path.join(relPath, coverFilename)
|
||||
|
||||
|
||||
if (isStoringInMetadata) {
|
||||
await this.checkBookMetadataCovers(fullPath, extname)
|
||||
}
|
||||
|
||||
// Move cover from temp upload dir to destination
|
||||
var success = await coverFile.mv(coverFullPath).then(() => true).catch((error) => {
|
||||
Logger.error('[CoverController] Failed to move cover file', path, error)
|
||||
|
@ -115,12 +108,13 @@ class CoverController {
|
|||
})
|
||||
|
||||
if (!success) {
|
||||
// return res.status(500).send('Failed to move cover into destination')
|
||||
return {
|
||||
error: 'Failed to move cover into destination'
|
||||
}
|
||||
}
|
||||
|
||||
await this.removeOldCovers(fullPath, extname)
|
||||
|
||||
Logger.info(`[CoverController] Uploaded audiobook cover "${coverPath}" for "${audiobook.title}"`)
|
||||
|
||||
audiobook.updateBookCover(coverPath)
|
||||
|
@ -171,10 +165,7 @@ class CoverController {
|
|||
var coverFullPath = Path.join(fullPath, coverFilename)
|
||||
await fs.rename(temppath, coverFullPath)
|
||||
|
||||
var isStoringInMetadata = relPath.slice(1).startsWith('metadata')
|
||||
if (isStoringInMetadata) {
|
||||
await this.checkBookMetadataCovers(fullPath, '.' + imgtype.ext)
|
||||
}
|
||||
await this.removeOldCovers(fullPath, '.' + imgtype.ext)
|
||||
|
||||
Logger.info(`[CoverController] Downloaded audiobook cover "${coverPath}" from url "${url}" for "${audiobook.title}"`)
|
||||
|
||||
|
|
|
@ -10,12 +10,13 @@ const { secondsToTimestamp } = require('./utils/fileUtils')
|
|||
const { ScanResult, CoverDestination } = require('./utils/constants')
|
||||
|
||||
class Scanner {
|
||||
constructor(AUDIOBOOK_PATH, METADATA_PATH, db, emitter) {
|
||||
constructor(AUDIOBOOK_PATH, METADATA_PATH, db, coverController, emitter) {
|
||||
this.AudiobookPath = AUDIOBOOK_PATH
|
||||
this.MetadataPath = METADATA_PATH
|
||||
this.BookMetadataPath = Path.join(this.MetadataPath, 'books')
|
||||
|
||||
this.db = db
|
||||
this.coverController = coverController
|
||||
this.emitter = emitter
|
||||
|
||||
this.cancelScan = false
|
||||
|
@ -453,6 +454,8 @@ class Scanner {
|
|||
var audiobooksNeedingCover = this.audiobooks.filter(ab => !ab.cover && ab.author)
|
||||
var found = 0
|
||||
var notFound = 0
|
||||
var failed = 0
|
||||
|
||||
for (let i = 0; i < audiobooksNeedingCover.length; i++) {
|
||||
var audiobook = audiobooksNeedingCover[i]
|
||||
var options = {
|
||||
|
@ -462,10 +465,15 @@ class Scanner {
|
|||
var results = await this.bookFinder.findCovers('openlibrary', audiobook.title, audiobook.author, options)
|
||||
if (results.length) {
|
||||
Logger.debug(`[Scanner] Found best cover for "${audiobook.title}"`)
|
||||
audiobook.book.cover = results[0]
|
||||
await this.db.updateAudiobook(audiobook)
|
||||
found++
|
||||
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
||||
var coverUrl = results[0]
|
||||
var result = await this.coverController.downloadCoverFromUrl(audiobook, coverUrl)
|
||||
if (result.error) {
|
||||
failed++
|
||||
} else {
|
||||
found++
|
||||
await this.db.updateAudiobook(audiobook)
|
||||
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
||||
}
|
||||
} else {
|
||||
notFound++
|
||||
}
|
||||
|
|
|
@ -36,10 +36,10 @@ class Server {
|
|||
this.db = new Db(this.ConfigPath)
|
||||
this.auth = new Auth(this.db)
|
||||
this.watcher = new Watcher(this.AudiobookPath)
|
||||
this.scanner = new Scanner(this.AudiobookPath, this.MetadataPath, this.db, this.emitter.bind(this))
|
||||
this.coverController = new CoverController(this.db, this.MetadataPath, this.AudiobookPath)
|
||||
this.scanner = new Scanner(this.AudiobookPath, this.MetadataPath, this.db, this.coverController, this.emitter.bind(this))
|
||||
this.streamManager = new StreamManager(this.db, this.MetadataPath)
|
||||
this.rssFeeds = new RssFeeds(this.Port, this.db)
|
||||
this.coverController = new CoverController(this.db, this.MetadataPath, this.AudiobookPath)
|
||||
this.downloadManager = new DownloadManager(this.db, this.MetadataPath, this.AudiobookPath, this.emitter.bind(this))
|
||||
this.apiController = new ApiController(this.MetadataPath, this.db, this.scanner, this.auth, this.streamManager, this.rssFeeds, this.downloadManager, this.coverController, this.emitter.bind(this), this.clientEmitter.bind(this))
|
||||
this.hlsController = new HlsController(this.db, this.scanner, this.auth, this.streamManager, this.emitter.bind(this), this.streamManager.StreamsPath)
|
||||
|
|
|
@ -437,7 +437,10 @@ class Audiobook {
|
|||
this.otherFiles = this.otherFiles.filter(f => newOtherFilePaths.includes(f.path))
|
||||
|
||||
// Some files are not there anymore and filtered out
|
||||
if (currOtherFileNum !== this.otherFiles.length) hasUpdates = true
|
||||
if (currOtherFileNum !== this.otherFiles.length) {
|
||||
Logger.debug(`[Audiobook] ${currOtherFileNum - this.otherFiles.length} other files were removed for "${this.title}"`)
|
||||
hasUpdates = true
|
||||
}
|
||||
|
||||
// If desc.txt is new or forcing rescan then read it and update description if empty
|
||||
var descriptionTxt = newOtherFiles.find(file => file.filename === 'desc.txt')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue