Skip to content

Commit

Permalink
Update new library scanner for scanning in new books
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Sep 1, 2023
1 parent 75276f5 commit 0ecfdab
Show file tree
Hide file tree
Showing 13 changed files with 694 additions and 35 deletions.
62 changes: 51 additions & 11 deletions server/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,35 @@ class Database {
return this.models.device.createFromOld(oldDevice)
}

replaceTagInFilterData(oldTag, newTag) {
for (const libraryId in this.libraryFilterData) {
const indexOf = this.libraryFilterData[libraryId].tags.findIndex(n => n === oldTag)
if (indexOf >= 0) {
this.libraryFilterData[libraryId].tags.splice(indexOf, 1, newTag)
}
}
}

removeTagFromFilterData(tag) {
for (const libraryId in this.libraryFilterData) {
this.libraryFilterData[libraryId].tags = this.libraryFilterData[libraryId].tags.filter(t => t !== tag)
}
}

addTagToFilterData(tag) {
addTagsToFilterData(libraryId, tags) {
if (!this.libraryFilterData[libraryId] || !tags?.length) return
tags.forEach((t) => {
if (!this.libraryFilterData[libraryId].tags.includes(t)) {
this.libraryFilterData[libraryId].tags.push(t)
}
})
}

replaceGenreInFilterData(oldGenre, newGenre) {
for (const libraryId in this.libraryFilterData) {
if (!this.libraryFilterData[libraryId].tags.includes(tag)) {
this.libraryFilterData[libraryId].tags.push(tag)
const indexOf = this.libraryFilterData[libraryId].genres.findIndex(n => n === oldGenre)
if (indexOf >= 0) {
this.libraryFilterData[libraryId].genres.splice(indexOf, 1, newGenre)
}
}
}
Expand All @@ -571,10 +590,20 @@ class Database {
}
}

addGenreToFilterData(genre) {
addGenresToFilterData(libraryId, genres) {
if (!this.libraryFilterData[libraryId] || !genres?.length) return
genres.forEach((g) => {
if (!this.libraryFilterData[libraryId].genres.includes(g)) {
this.libraryFilterData[libraryId].genres.push(g)
}
})
}

replaceNarratorInFilterData(oldNarrator, newNarrator) {
for (const libraryId in this.libraryFilterData) {
if (!this.libraryFilterData[libraryId].genres.includes(genre)) {
this.libraryFilterData[libraryId].genres.push(genre)
const indexOf = this.libraryFilterData[libraryId].narrators.findIndex(n => n === oldNarrator)
if (indexOf >= 0) {
this.libraryFilterData[libraryId].narrators.splice(indexOf, 1, newNarrator)
}
}
}
Expand All @@ -585,12 +614,13 @@ class Database {
}
}

addNarratorToFilterData(narrator) {
for (const libraryId in this.libraryFilterData) {
if (!this.libraryFilterData[libraryId].narrators.includes(narrator)) {
this.libraryFilterData[libraryId].narrators.push(narrator)
addNarratorsToFilterData(libraryId, narrators) {
if (!this.libraryFilterData[libraryId] || !narrators?.length) return
narrators.forEach((n) => {
if (!this.libraryFilterData[libraryId].narrators.includes(n)) {
this.libraryFilterData[libraryId].narrators.push(n)
}
}
})
}

removeSeriesFromFilterData(libraryId, seriesId) {
Expand Down Expand Up @@ -623,6 +653,16 @@ class Database {
})
}

addPublisherToFilterData(libraryId, publisher) {
if (!this.libraryFilterData[libraryId] || !publisher || this.libraryFilterData[libraryId].publishers.includes(publisher)) return
this.libraryFilterData[libraryId].publishers.push(publisher)
}

addLanguageToFilterData(libraryId, language) {
if (!this.libraryFilterData[libraryId] || !language || this.libraryFilterData[libraryId].languages.includes(language)) return
this.libraryFilterData[libraryId].languages.push(language)
}

/**
* Used when updating items to make sure author id exists
* If library filter data is set then use that for check
Expand Down
3 changes: 1 addition & 2 deletions server/controllers/LibraryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,7 @@ class LibraryController {
}

// Update filter data
Database.removeNarratorFromFilterData(narratorName)
Database.addNarratorToFilterData(updatedName)
Database.replaceNarratorInFilterData(narratorName, updatedName)

const itemsUpdated = []

Expand Down
6 changes: 2 additions & 4 deletions server/controllers/MiscController.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ class MiscController {
let numItemsUpdated = 0

// Update filter data
Database.removeTagFromFilterData(tag)
Database.addTagToFilterData(newTag)
Database.replaceTagInFilterData(tag, newTag)

const libraryItemsWithTag = await libraryItemFilters.getAllLibraryItemsWithTags([tag, newTag])
for (const libraryItem of libraryItemsWithTag) {
Expand Down Expand Up @@ -364,8 +363,7 @@ class MiscController {
let numItemsUpdated = 0

// Update filter data
Database.removeGenreFromFilterData(genre)
Database.addGenreToFilterData(newGenre)
Database.replaceGenreInFilterData(genre, newGenre)

const libraryItemsWithGenre = await libraryItemFilters.getAllLibraryItemsWithGenres([genre, newGenre])
for (const libraryItem of libraryItemsWithGenre) {
Expand Down
28 changes: 28 additions & 0 deletions server/managers/CoverManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,33 @@ class CoverManager {
}
return false
}

static async saveEmbeddedCoverArtNew(audioFiles, libraryItemId, libraryItemPath) {
let audioFileWithCover = audioFiles.find(af => af.embeddedCoverArt)
if (!audioFileWithCover) return null

let coverDirPath = null
if (global.ServerSettings.storeCoverWithItem && libraryItemPath) {
coverDirPath = libraryItemPath
} else {
coverDirPath = Path.posix.join(this.ItemMetadataPath, libraryItemId)
}
await fs.ensureDir(coverDirPath)

const coverFilename = audioFileWithCover.embeddedCoverArt === 'png' ? 'cover.png' : 'cover.jpg'
const coverFilePath = Path.join(coverDirPath, coverFilename)

const coverAlreadyExists = await fs.pathExists(coverFilePath)
if (coverAlreadyExists) {
Logger.warn(`[CoverManager] Extract embedded cover art but cover already exists for "${libraryItemPath}" - bail`)
return null
}

const success = await extractCoverArt(audioFileWithCover.metadata.path, coverFilePath)
if (success) {
return coverFilePath
}
return null
}
}
module.exports = CoverManager
4 changes: 0 additions & 4 deletions server/objects/metadata/BookMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ class BookMetadata {
{
tag: 'tagASIN',
key: 'asin'
},
{
tag: 'tagOverdriveMediaMarker',
key: 'overdriveMediaMarker'
}
]

Expand Down
10 changes: 6 additions & 4 deletions server/scanner/AudioFileScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class AudioFileScanner {

/**
* Order audio files by track/disc number
* @param {import('../models/Book')} book
* @param {string} libraryItemRelPath
* @param {import('../models/Book').AudioFileObject[]} audioFiles
* @returns {import('../models/Book').AudioFileObject[]}
*/
runSmartTrackOrder(book, audioFiles) {
runSmartTrackOrder(libraryItemRelPath, audioFiles) {
if (!audioFiles.length) return []

let discsFromFilename = []
let tracksFromFilename = []
let discsFromMeta = []
Expand Down Expand Up @@ -79,14 +81,14 @@ class AudioFileScanner {
}

if (discKey !== null) {
Logger.debug(`[AudioFileScanner] Smart track order for "${book.title}" using disc key ${discKey} and track key ${trackKey}`)
Logger.debug(`[AudioFileScanner] Smart track order for "${libraryItemRelPath}" using disc key ${discKey} and track key ${trackKey}`)
audioFiles.sort((a, b) => {
let Dx = a[discKey] - b[discKey]
if (Dx === 0) Dx = a[trackKey] - b[trackKey]
return Dx
})
} else {
Logger.debug(`[AudioFileScanner] Smart track order for "${book.title}" using track key ${trackKey}`)
Logger.debug(`[AudioFileScanner] Smart track order for "${libraryItemRelPath}" using track key ${trackKey}`)
audioFiles.sort((a, b) => a[trackKey] - b[trackKey])
}

Expand Down
Loading

0 comments on commit 0ecfdab

Please sign in to comment.