From f7ce623db1225b7b6f8383c33cab9515b36907fe Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Jul 2022 11:21:19 +0200 Subject: Don't log error on static 404 --- server/controllers/static.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'server') diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 7668ceb82..0b5c12b76 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts @@ -1,3 +1,4 @@ +import { HttpStatusCode } from '@shared/models' import cors from 'cors' import express from 'express' import { CONFIG } from '../initializers/config' @@ -11,24 +12,28 @@ staticRouter.use(cors()) // Videos path for webseed staticRouter.use( 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( 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 staticRouter.use( 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 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR staticRouter.use( 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 { 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) +} -- cgit v1.2.3