2021-11-25 03:15:50 +01:00
|
|
|
const { isNullOrNaN } = require('../utils/index')
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
const Logger = require('../Logger')
|
2021-09-30 03:43:36 +02:00
|
|
|
const AudioFileMetadata = require('./AudioFileMetadata')
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
class AudioFile {
|
|
|
|
constructor(data) {
|
|
|
|
this.index = null
|
|
|
|
this.ino = null
|
|
|
|
this.filename = null
|
|
|
|
this.ext = null
|
|
|
|
this.path = null
|
|
|
|
this.fullPath = null
|
|
|
|
this.addedAt = null
|
|
|
|
|
2021-08-26 14:09:23 +02:00
|
|
|
this.trackNumFromMeta = null
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromMeta = null
|
2021-08-26 14:09:23 +02:00
|
|
|
this.trackNumFromFilename = null
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromFilename = null
|
2021-08-26 14:09:23 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.format = null
|
|
|
|
this.duration = null
|
|
|
|
this.size = null
|
|
|
|
this.bitRate = null
|
|
|
|
this.language = null
|
|
|
|
this.codec = null
|
|
|
|
this.timeBase = null
|
|
|
|
this.channels = null
|
|
|
|
this.channelLayout = null
|
2021-09-08 16:15:54 +02:00
|
|
|
this.chapters = []
|
2021-09-30 03:43:36 +02:00
|
|
|
this.embeddedCoverArt = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2021-09-30 03:43:36 +02:00
|
|
|
// Tags scraped from the audio file
|
|
|
|
this.metadata = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
|
|
|
this.manuallyVerified = false
|
|
|
|
this.invalid = false
|
2021-09-05 01:02:42 +02:00
|
|
|
this.exclude = false
|
2021-08-26 00:36:54 +02:00
|
|
|
this.error = null
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
this.construct(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
index: this.index,
|
|
|
|
ino: this.ino,
|
|
|
|
filename: this.filename,
|
|
|
|
ext: this.ext,
|
|
|
|
path: this.path,
|
|
|
|
fullPath: this.fullPath,
|
|
|
|
addedAt: this.addedAt,
|
2021-08-26 14:09:23 +02:00
|
|
|
trackNumFromMeta: this.trackNumFromMeta,
|
2022-01-10 00:36:25 +01:00
|
|
|
discNumFromMeta: this.discNumFromMeta,
|
2021-08-26 14:09:23 +02:00
|
|
|
trackNumFromFilename: this.trackNumFromFilename,
|
2022-01-10 00:36:25 +01:00
|
|
|
discNumFromFilename: this.discNumFromFilename,
|
2021-08-26 00:36:54 +02:00
|
|
|
manuallyVerified: !!this.manuallyVerified,
|
|
|
|
invalid: !!this.invalid,
|
2021-09-05 01:02:42 +02:00
|
|
|
exclude: !!this.exclude,
|
2021-08-26 00:36:54 +02:00
|
|
|
error: this.error || null,
|
|
|
|
format: this.format,
|
|
|
|
duration: this.duration,
|
|
|
|
size: this.size,
|
|
|
|
bitRate: this.bitRate,
|
|
|
|
language: this.language,
|
2021-10-01 01:52:32 +02:00
|
|
|
codec: this.codec,
|
2021-08-26 00:36:54 +02:00
|
|
|
timeBase: this.timeBase,
|
|
|
|
channels: this.channels,
|
|
|
|
channelLayout: this.channelLayout,
|
2021-09-08 16:15:54 +02:00
|
|
|
chapters: this.chapters,
|
2021-09-30 03:43:36 +02:00
|
|
|
embeddedCoverArt: this.embeddedCoverArt,
|
|
|
|
metadata: this.metadata ? this.metadata.toJSON() : {}
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(data) {
|
|
|
|
this.index = data.index
|
|
|
|
this.ino = data.ino
|
|
|
|
this.filename = data.filename
|
|
|
|
this.ext = data.ext
|
|
|
|
this.path = data.path
|
|
|
|
this.fullPath = data.fullPath
|
|
|
|
this.addedAt = data.addedAt
|
|
|
|
this.manuallyVerified = !!data.manuallyVerified
|
|
|
|
this.invalid = !!data.invalid
|
2021-09-05 01:02:42 +02:00
|
|
|
this.exclude = !!data.exclude
|
2021-08-26 00:36:54 +02:00
|
|
|
this.error = data.error || null
|
|
|
|
|
2021-12-05 18:29:42 +01:00
|
|
|
this.trackNumFromMeta = data.trackNumFromMeta
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromMeta = data.discNumFromMeta
|
2021-12-05 18:29:42 +01:00
|
|
|
this.trackNumFromFilename = data.trackNumFromFilename
|
2022-01-10 00:36:25 +01:00
|
|
|
|
|
|
|
if (data.cdNumFromFilename !== undefined) this.discNumFromFilename = data.cdNumFromFilename // TEMP:Support old var name
|
|
|
|
else this.discNumFromFilename = data.discNumFromFilename
|
2021-08-26 14:09:23 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.format = data.format
|
|
|
|
this.duration = data.duration
|
|
|
|
this.size = data.size
|
|
|
|
this.bitRate = data.bitRate
|
|
|
|
this.language = data.language
|
2021-10-01 01:52:32 +02:00
|
|
|
this.codec = data.codec || null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.timeBase = data.timeBase
|
|
|
|
this.channels = data.channels
|
|
|
|
this.channelLayout = data.channelLayout
|
2021-09-08 16:15:54 +02:00
|
|
|
this.chapters = data.chapters
|
2021-09-30 03:43:36 +02:00
|
|
|
this.embeddedCoverArt = data.embeddedCoverArt || null
|
|
|
|
|
|
|
|
// Old version of AudioFile used `tagAlbum` etc.
|
|
|
|
var isOldVersion = Object.keys(data).find(key => key.startsWith('tag'))
|
|
|
|
if (isOldVersion) {
|
|
|
|
this.metadata = new AudioFileMetadata(data)
|
|
|
|
} else {
|
|
|
|
this.metadata = new AudioFileMetadata(data.metadata || {})
|
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 02:58:20 +01:00
|
|
|
// New scanner creates AudioFile from AudioFileScanner
|
2021-11-25 03:15:50 +01:00
|
|
|
setDataFromProbe(fileData, probeData) {
|
2021-11-23 02:58:20 +01:00
|
|
|
this.index = fileData.index || null
|
|
|
|
this.ino = fileData.ino || null
|
|
|
|
this.filename = fileData.filename
|
|
|
|
this.ext = fileData.ext
|
|
|
|
this.path = fileData.path
|
|
|
|
this.fullPath = fileData.fullPath
|
|
|
|
this.addedAt = Date.now()
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
this.trackNumFromMeta = fileData.trackNumFromMeta
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromMeta = fileData.discNumFromMeta
|
2021-11-26 01:39:02 +01:00
|
|
|
this.trackNumFromFilename = fileData.trackNumFromFilename
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromFilename = fileData.discNumFromFilename
|
2021-11-25 03:15:50 +01:00
|
|
|
|
|
|
|
this.format = probeData.format
|
|
|
|
this.duration = probeData.duration
|
|
|
|
this.size = probeData.size
|
|
|
|
this.bitRate = probeData.bitRate || null
|
|
|
|
this.language = probeData.language
|
|
|
|
this.codec = probeData.codec || null
|
|
|
|
this.timeBase = probeData.timeBase
|
|
|
|
this.channels = probeData.channels
|
|
|
|
this.channelLayout = probeData.channelLayout
|
|
|
|
this.chapters = probeData.chapters || []
|
|
|
|
this.metadata = probeData.audioFileMetadata
|
2021-11-26 01:39:02 +01:00
|
|
|
this.embeddedCoverArt = probeData.embeddedCoverArt
|
2021-11-25 03:15:50 +01:00
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
validateTrackIndex() {
|
2021-11-25 03:15:50 +01:00
|
|
|
var numFromMeta = isNullOrNaN(this.trackNumFromMeta) ? null : Number(this.trackNumFromMeta)
|
|
|
|
var numFromFilename = isNullOrNaN(this.trackNumFromFilename) ? null : Number(this.trackNumFromFilename)
|
|
|
|
|
|
|
|
if (numFromMeta !== null) return numFromMeta
|
|
|
|
if (numFromFilename !== null) return numFromFilename
|
|
|
|
|
|
|
|
this.invalid = true
|
|
|
|
this.error = 'Failed to get track number'
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
setDuplicateTrackNumber(num) {
|
|
|
|
this.invalid = true
|
|
|
|
this.error = 'Duplicate track number "' + num + '"'
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
syncChapters(updatedChapters) {
|
|
|
|
if (this.chapters.length !== updatedChapters.length) {
|
|
|
|
this.chapters = updatedChapters.map(ch => ({ ...ch }))
|
|
|
|
return true
|
|
|
|
} else if (updatedChapters.length === 0) {
|
|
|
|
if (this.chapters.length > 0) {
|
|
|
|
this.chapters = []
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasUpdates = false
|
|
|
|
for (let i = 0; i < updatedChapters.length; i++) {
|
|
|
|
if (JSON.stringify(updatedChapters[i]) !== JSON.stringify(this.chapters[i])) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasUpdates) {
|
|
|
|
this.chapters = updatedChapters.map(ch => ({ ...ch }))
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:02:42 +02:00
|
|
|
clone() {
|
|
|
|
return new AudioFile(this.toJSON())
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
// If the file or parent directory was renamed it is synced here
|
2021-08-26 00:36:54 +02:00
|
|
|
syncFile(newFile) {
|
|
|
|
var hasUpdates = false
|
|
|
|
var keysToSync = ['path', 'fullPath', 'ext', 'filename']
|
|
|
|
keysToSync.forEach((key) => {
|
|
|
|
if (newFile[key] !== undefined && newFile[key] !== this[key]) {
|
|
|
|
hasUpdates = true
|
|
|
|
this[key] = newFile[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return hasUpdates
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
updateFromScan(scannedAudioFile) {
|
|
|
|
var hasUpdated = false
|
|
|
|
|
|
|
|
var newjson = scannedAudioFile.toJSON()
|
|
|
|
if (this.manuallyVerified) newjson.manuallyVerified = true
|
|
|
|
if (this.exclude) newjson.exclude = true
|
|
|
|
newjson.addedAt = this.addedAt
|
|
|
|
|
|
|
|
for (const key in newjson) {
|
|
|
|
if (key === 'metadata') {
|
|
|
|
if (!this.metadata || !this.metadata.isEqual(scannedAudioFile.metadata)) {
|
|
|
|
this.metadata = scannedAudioFile.metadata
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
} else if (key === 'chapters') {
|
|
|
|
if (this.syncChapters(newjson.chapters || [])) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
} else if (this[key] !== newjson[key]) {
|
2021-12-05 18:29:42 +01:00
|
|
|
// console.log(this.filename, 'key', key, 'updated', this[key], newjson[key])
|
2021-11-26 01:39:02 +01:00
|
|
|
this[key] = newjson[key]
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdated
|
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
module.exports = AudioFile
|