2022-03-13 00:45:32 +01:00
|
|
|
const Path = require('path')
|
2023-05-17 01:58:01 +02:00
|
|
|
const { getFileTimestampsWithIno, filePathToPOSIX } = require('../../utils/fileUtils')
|
2022-03-11 01:45:02 +01:00
|
|
|
const globals = require('../../utils/globals')
|
2022-03-09 02:31:44 +01:00
|
|
|
const FileMetadata = require('../metadata/FileMetadata')
|
|
|
|
|
|
|
|
class LibraryFile {
|
|
|
|
constructor(file) {
|
|
|
|
this.ino = null
|
|
|
|
this.metadata = null
|
|
|
|
this.addedAt = null
|
|
|
|
this.updatedAt = null
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
this.construct(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(file) {
|
|
|
|
this.ino = file.ino
|
|
|
|
this.metadata = new FileMetadata(file.metadata)
|
|
|
|
this.addedAt = file.addedAt
|
|
|
|
this.updatedAt = file.updatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
ino: this.ino,
|
|
|
|
metadata: this.metadata.toJSON(),
|
|
|
|
addedAt: this.addedAt,
|
2022-03-11 01:45:02 +01:00
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
fileType: this.fileType
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
clone() {
|
|
|
|
return new LibraryFile(this.toJSON())
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
get fileType() {
|
|
|
|
if (globals.SupportedImageTypes.includes(this.metadata.format)) return 'image'
|
|
|
|
if (globals.SupportedAudioTypes.includes(this.metadata.format)) return 'audio'
|
|
|
|
if (globals.SupportedEbookTypes.includes(this.metadata.format)) return 'ebook'
|
2022-05-31 02:26:53 +02:00
|
|
|
if (globals.SupportedVideoTypes.includes(this.metadata.format)) return 'video'
|
2022-03-11 01:45:02 +01:00
|
|
|
if (globals.TextFileTypes.includes(this.metadata.format)) return 'text'
|
|
|
|
if (globals.MetadataFileTypes.includes(this.metadata.format)) return 'metadata'
|
|
|
|
return 'unknown'
|
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
|
|
|
|
get isMediaFile() {
|
2022-05-31 02:26:53 +02:00
|
|
|
return this.fileType === 'audio' || this.fileType === 'ebook' || this.fileType === 'video'
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get isOPFFile() {
|
|
|
|
return this.metadata.ext === '.opf'
|
|
|
|
}
|
|
|
|
|
|
|
|
async setDataFromPath(path, relPath) {
|
|
|
|
var fileTsData = await getFileTimestampsWithIno(path)
|
|
|
|
var fileMetadata = new FileMetadata()
|
|
|
|
fileMetadata.setData(fileTsData)
|
|
|
|
fileMetadata.filename = Path.basename(relPath)
|
2023-05-17 01:58:01 +02:00
|
|
|
fileMetadata.path = filePathToPOSIX(path)
|
|
|
|
fileMetadata.relPath = filePathToPOSIX(relPath)
|
2022-03-13 00:45:32 +01:00
|
|
|
fileMetadata.ext = Path.extname(relPath)
|
|
|
|
this.ino = fileTsData.ino
|
|
|
|
this.metadata = fileMetadata
|
|
|
|
this.addedAt = Date.now()
|
|
|
|
this.updatedAt = Date.now()
|
|
|
|
}
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
module.exports = LibraryFile
|