2022-07-13 04:21:19 -05:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2021-08-27 07:32:44 -05:00
|
|
|
import cors from 'cors'
|
|
|
|
import express from 'express'
|
2022-07-13 04:13:19 -05:00
|
|
|
import { CONFIG } from '../initializers/config'
|
|
|
|
import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
const staticRouter = express.Router()
|
|
|
|
|
2022-07-13 04:13:19 -05:00
|
|
|
// Cors is very important to let other servers access torrent and video files
|
2018-07-17 08:04:54 -05:00
|
|
|
staticRouter.use(cors())
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
// Videos path for webseed
|
2017-05-15 15:22:03 -05:00
|
|
|
staticRouter.use(
|
|
|
|
STATIC_PATHS.WEBSEED,
|
2022-07-13 04:21:19 -05:00
|
|
|
express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }),
|
|
|
|
handleStaticError
|
2017-05-15 15:22:03 -05:00
|
|
|
)
|
2018-12-04 09:02:49 -06:00
|
|
|
staticRouter.use(
|
2018-12-04 10:08:55 -06:00
|
|
|
STATIC_PATHS.REDUNDANCY,
|
2022-07-13 04:21:19 -05:00
|
|
|
express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }),
|
|
|
|
handleStaticError
|
2018-12-04 09:02:49 -06:00
|
|
|
)
|
|
|
|
|
2019-01-29 01:37:25 -06:00
|
|
|
// HLS
|
|
|
|
staticRouter.use(
|
2019-03-05 05:00:31 -06:00
|
|
|
STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
|
2022-07-13 04:21:19 -05:00
|
|
|
express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }),
|
|
|
|
handleStaticError
|
2019-01-29 01:37:25 -06:00
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
// Thumbnails path for express
|
|
|
|
const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
|
|
|
|
staticRouter.use(
|
|
|
|
STATIC_PATHS.THUMBNAILS,
|
2022-07-13 04:21:19 -05:00
|
|
|
express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }),
|
|
|
|
handleStaticError
|
2017-05-15 15:22:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
staticRouter
|
|
|
|
}
|
2022-07-13 04:21:19 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function handleStaticError (err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const message = err.message || ''
|
|
|
|
|
|
|
|
if (message.includes('ENOENT')) {
|
|
|
|
return res.fail({
|
|
|
|
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
|
|
|
message: err.message,
|
|
|
|
type: err.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(err)
|
|
|
|
}
|