2022-03-18 01:10:47 +01:00
|
|
|
const express = require('express')
|
|
|
|
const Path = require('path')
|
2022-07-24 20:32:05 +02:00
|
|
|
const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils')
|
2022-03-18 01:10:47 +01:00
|
|
|
|
|
|
|
class StaticRouter {
|
|
|
|
constructor(db) {
|
|
|
|
this.db = db
|
|
|
|
|
|
|
|
this.router = express()
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
// Library Item static file routes
|
|
|
|
this.router.get('/item/:id/*', (req, res) => {
|
|
|
|
var item = this.db.libraryItems.find(ab => ab.id === req.params.id)
|
|
|
|
if (!item) return res.status(404).send('Item not found with id ' + req.params.id)
|
|
|
|
|
|
|
|
var remainingPath = req.params['0']
|
2022-05-24 01:31:11 +02:00
|
|
|
var fullPath = null
|
|
|
|
if (item.isFile) fullPath = item.path
|
|
|
|
else fullPath = Path.join(item.path, remainingPath)
|
2022-07-24 20:32:05 +02:00
|
|
|
|
|
|
|
var opts = {}
|
|
|
|
|
|
|
|
// Express does not set the correct mimetype for m4b files so use our defined mimetypes if available
|
|
|
|
const audioMimeType = getAudioMimeTypeFromExtname(Path.extname(fullPath))
|
|
|
|
if (audioMimeType) {
|
|
|
|
opts = { headers: { 'Content-Type': audioMimeType } }
|
|
|
|
}
|
|
|
|
|
|
|
|
res.sendFile(fullPath, opts)
|
2022-03-18 01:10:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = StaticRouter
|