hopefully fix crashing

This commit is contained in:
matty 2025-05-10 23:49:38 -04:00
parent 4ab4a0fa7f
commit 99a0ba6945

View File

@ -2,90 +2,104 @@
// //
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterServerOptions } from '@peertube/peertube-types' import type { RegisterServerOptions } from '@peertube/peertube-types';
import type { Router, Request, Response, NextFunction } from 'express' import type { Router, Request, Response, NextFunction } from 'express';
import { asyncMiddleware } from '../middlewares/async' import { asyncMiddleware } from '../middlewares/async';
import { Emojis } from '../emojis' import { Emojis } from '../emojis';
export async function initEmojisRouter( export async function initEmojisRouter(
options: RegisterServerOptions, options: RegisterServerOptions,
router: Router router: Router
): Promise<void> { ): Promise<void> {
const logger = options.peertubeHelpers.logger const logger = options.peertubeHelpers.logger;
router.get( router.get(
'/emojis/channel/:channelId/definition', '/emojis/channel/:channelId/definition',
asyncMiddleware(async (req: Request, res: Response, _next: NextFunction): Promise<void> => { asyncMiddleware(
async (
req: Request,
res: Response,
_next: NextFunction
): Promise<void> => {
try { try {
const emojis = Emojis.singletonSafe() const emojis = Emojis.singletonSafe();
if (!emojis) { if (!emojis) {
res.sendStatus(404) res.sendStatus(404);
return return;
} }
const channelId = parseInt(req.params.channelId) const channelId = parseInt(req.params.channelId);
if (!channelId || isNaN(channelId)) { if (!channelId || isNaN(channelId)) {
res.sendStatus(400) res.sendStatus(400);
return return;
} }
if (!await emojis.channelHasCustomEmojis(channelId)) { if (!(await emojis.channelHasCustomEmojis(channelId))) {
res.sendStatus(404) res.sendStatus(404);
return return;
} }
res.sendFile(emojis.channelCustomEmojisDefinitionPath(channelId)) res.sendFile(emojis.channelCustomEmojisDefinitionPath(channelId));
} catch (err) { } catch (err) {
logger.error(err) logger.error(err);
res.sendStatus(500) res.sendStatus(500);
}
} }
})
) )
);
// Note: CORS is handled by Peertube. // Note: CORS is handled by Peertube.
router.get( router.get(
'/emojis/channel/:channelId/files/:fileName', '/emojis/channel/:channelId/files/:fileName',
asyncMiddleware(async (req: Request, res: Response, _next: NextFunction): Promise<void> => { asyncMiddleware(
async (
req: Request,
res: Response,
_next: NextFunction
): Promise<void> => {
try { try {
const emojis = Emojis.singletonSafe() const emojis = Emojis.singletonSafe();
if (!emojis) { if (!emojis) {
res.sendStatus(404) res.sendStatus(404);
return return;
} }
const channelId = parseInt(req.params.channelId) const channelId = parseInt(req.params.channelId);
if (!channelId || isNaN(channelId)) { if (!channelId || isNaN(channelId)) {
res.sendStatus(400) res.sendStatus(400);
return return;
} }
const fileName = req.params.fileName const fileName = req.params.fileName;
if (!emojis.validImageFileName(fileName)) { if (!emojis.validImageFileName(fileName)) {
res.sendStatus(400) res.sendStatus(400);
return return;
} }
if (!await emojis.channelHasCustomEmojis(channelId)) { if (!(await emojis.channelHasCustomEmojis(channelId))) {
res.sendStatus(404) res.sendStatus(404);
return return;
} }
res.sendFile( res.sendFile(
emojis.channelCustomEmojisFilePath(channelId, fileName), emojis.channelCustomEmojisFilePath(channelId, fileName),
{ {
immutable: true, immutable: true,
maxAge: 1000 * 60 * 60 * 24 // 24h maxAge: 1000 * 60 * 60 * 24, // 24h
}, },
(err) => { (err) => {
if (err) { if (err) {
res.sendStatus(404) res.sendStatus(404);
return;
} }
} }
) );
} catch (err) { } catch (err) {
logger.error(err) logger.error(err);
res.sendStatus(500) res.sendStatus(500);
return;
}
} }
})
) )
);
} }