mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-23 04:14:46 +02:00
Laying the groundwork for music media type #964
This commit is contained in:
parent
c3717f6979
commit
b884f8fe11
18 changed files with 491 additions and 134 deletions
104
server/objects/metadata/MusicMetadata.js
Normal file
104
server/objects/metadata/MusicMetadata.js
Normal file
|
@ -0,0 +1,104 @@
|
|||
const Logger = require('../../Logger')
|
||||
const { areEquivalent, copyValue, cleanStringForSearch, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')
|
||||
|
||||
class MusicMetadata {
|
||||
constructor(metadata) {
|
||||
this.title = null
|
||||
this.artist = null
|
||||
this.album = null
|
||||
this.genres = [] // Array of strings
|
||||
this.releaseDate = null
|
||||
this.language = null
|
||||
this.explicit = false
|
||||
|
||||
if (metadata) {
|
||||
this.construct(metadata)
|
||||
}
|
||||
}
|
||||
|
||||
construct(metadata) {
|
||||
this.title = metadata.title
|
||||
this.artist = metadata.artist
|
||||
this.album = metadata.album
|
||||
this.genres = metadata.genres ? [...metadata.genres] : []
|
||||
this.releaseDate = metadata.releaseDate || null
|
||||
this.language = metadata.language
|
||||
this.explicit = !!metadata.explicit
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
title: this.title,
|
||||
artist: this.artist,
|
||||
album: this.album,
|
||||
genres: [...this.genres],
|
||||
releaseDate: this.releaseDate,
|
||||
language: this.language,
|
||||
explicit: this.explicit
|
||||
}
|
||||
}
|
||||
|
||||
toJSONMinified() {
|
||||
return {
|
||||
title: this.title,
|
||||
titleIgnorePrefix: this.titlePrefixAtEnd,
|
||||
artist: this.artist,
|
||||
album: this.album,
|
||||
genres: [...this.genres],
|
||||
releaseDate: this.releaseDate,
|
||||
language: this.language,
|
||||
explicit: this.explicit
|
||||
}
|
||||
}
|
||||
|
||||
toJSONExpanded() {
|
||||
return this.toJSONMinified()
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new MusicMetadata(this.toJSON())
|
||||
}
|
||||
|
||||
get titleIgnorePrefix() {
|
||||
return getTitleIgnorePrefix(this.title)
|
||||
}
|
||||
|
||||
get titlePrefixAtEnd() {
|
||||
return getTitlePrefixAtEnd(this.title)
|
||||
}
|
||||
|
||||
searchQuery(query) { // Returns key if match is found
|
||||
const keysToCheck = ['title', 'artist', 'album']
|
||||
for (const key of keysToCheck) {
|
||||
if (this[key] && cleanStringForSearch(String(this[key])).includes(query)) {
|
||||
return {
|
||||
matchKey: key,
|
||||
matchText: this[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
setData(mediaMetadata = {}) {
|
||||
this.title = mediaMetadata.title || null
|
||||
this.artist = mediaMetadata.artist || null
|
||||
this.album = mediaMetadata.album || null
|
||||
}
|
||||
|
||||
update(payload) {
|
||||
const json = this.toJSON()
|
||||
let hasUpdates = false
|
||||
for (const key in json) {
|
||||
if (payload[key] !== undefined) {
|
||||
if (!areEquivalent(payload[key], json[key])) {
|
||||
this[key] = copyValue(payload[key])
|
||||
Logger.debug('[MusicMetadata] Key updated', key, this[key])
|
||||
hasUpdates = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return hasUpdates
|
||||
}
|
||||
}
|
||||
module.exports = MusicMetadata
|
Loading…
Add table
Add a link
Reference in a new issue