Don't log error on static 404

This commit is contained in:
Chocobozzz 2022-07-13 11:21:19 +02:00
parent ea8107bff8
commit f7ce623db1
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 25 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import { HttpStatusCode } from '@shared/models'
import cors from 'cors' import cors from 'cors'
import express from 'express' import express from 'express'
import { CONFIG } from '../initializers/config' import { CONFIG } from '../initializers/config'
@ -11,24 +12,28 @@ staticRouter.use(cors())
// Videos path for webseed // Videos path for webseed
staticRouter.use( staticRouter.use(
STATIC_PATHS.WEBSEED, STATIC_PATHS.WEBSEED,
express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }),
handleStaticError
) )
staticRouter.use( staticRouter.use(
STATIC_PATHS.REDUNDANCY, STATIC_PATHS.REDUNDANCY,
express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }),
handleStaticError
) )
// HLS // HLS
staticRouter.use( staticRouter.use(
STATIC_PATHS.STREAMING_PLAYLISTS.HLS, STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }),
handleStaticError
) )
// Thumbnails path for express // Thumbnails path for express
const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
staticRouter.use( staticRouter.use(
STATIC_PATHS.THUMBNAILS, STATIC_PATHS.THUMBNAILS,
express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }),
handleStaticError
) )
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -36,3 +41,19 @@ staticRouter.use(
export { export {
staticRouter staticRouter
} }
// ---------------------------------------------------------------------------
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)
}