diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..cc166ba --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +# Path to yt-dlp. +VIDEO_DOWNLOADER_EXECUTABLE_PATH= diff --git a/src/controllers/ClipController.ts b/src/controllers/ClipController.ts index 9a3e0a0..1a8f280 100644 --- a/src/controllers/ClipController.ts +++ b/src/controllers/ClipController.ts @@ -7,15 +7,9 @@ import { import { constants } from 'http2'; import { RouteHandlerMethod } from 'fastify'; -export type ClipArgs = { - url?: unknown, - start?: string | number, - end?: string | number, -} - const DURATION_STRING_REGEXP = /^\d\d:[0-5]\d:[0-5]\d(\.\d+)?$/; -const validateRequestBody = (body: ClipArgs) => { +const validateRequestBody = (body: ClipVideoParams) => { const messages = [] as string[]; const { url, start, end } = body; if (typeof url !== 'string') { @@ -54,7 +48,10 @@ const getVideoType = (url: string) => { }; export const clip: RouteHandlerMethod = async (request, reply) => { - const validationMessages = validateRequestBody(request.body as ClipArgs); + const body = request.body as ClipVideoParams; + const { url, start, end } = body; + + const validationMessages = validateRequestBody(body); if (validationMessages.length > 0) { reply .status(constants.HTTP_STATUS_BAD_REQUEST) @@ -63,7 +60,7 @@ export const clip: RouteHandlerMethod = async (request, reply) => { }); return; } - const videoType = getVideoType((request.body as ClipArgs).url as string); + const videoType = getVideoType(url); if (videoType === null) { reply .status(constants.HTTP_STATUS_UNPROCESSABLE_ENTITY) @@ -74,10 +71,9 @@ export const clip: RouteHandlerMethod = async (request, reply) => { const clipper = createVideoClipper({ type: videoType as VideoType, - downloaderExecutablePath: process.env.YOUTUBE_DOWNLOADER_EXECUTABLE_PATH as string, + downloaderExecutablePath: process.env.VIDEO_DOWNLOADER_EXECUTABLE_PATH as string, }); - const { url, start, end } = request.body as ClipVideoParams; const clipResult = await clipper({ url, start, diff --git a/src/routes.ts b/src/routes.ts index d3662be..81a3f51 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -3,6 +3,6 @@ import SERVER from './server'; SERVER.route({ method: 'POST', - url: '/clip', + url: '/api/clip', handler: ClipController.clip, });