From 6709e8505cd868a9e39c676bea05d04c12506195 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 1 May 2023 13:25:31 +0800 Subject: [PATCH] Refactor web API Use refactored core SDK based on Promises. --- package.json | 2 +- src/controllers/ClipController.ts | 68 ++++++++++--------------------- yarn.lock | 4 ++ 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index 46a6bfe..c6e0d36 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "vitest": "^0.28.1" }, "dependencies": { - "@modal/webvideo-clip-core": "*", + "@modal/webvideo-clip-core": "link:../core", "fastify": "^4.12.0" }, "scripts": { diff --git a/src/controllers/ClipController.ts b/src/controllers/ClipController.ts index fb1f899..9a3e0a0 100644 --- a/src/controllers/ClipController.ts +++ b/src/controllers/ClipController.ts @@ -1,7 +1,8 @@ import { createVideoClipper, + YouTube, VideoType, - CreateVideoClipperParams, + ClipVideoParams, } from '@modal/webvideo-clip-core'; import { constants } from 'http2'; import { RouteHandlerMethod } from 'fastify'; @@ -23,18 +24,20 @@ const validateRequestBody = (body: ClipArgs) => { const typeofStart = typeof start; if (typeofStart !== 'undefined') { - if (!['string', 'number'].includes(typeofStart)) { - messages.push('Invalid end value.'); - } else if (typeofStart === 'string' && !DURATION_STRING_REGEXP.test(start as string)) { + if ( + !['string', 'number'].includes(typeofStart) + || (typeofStart === 'string' && !DURATION_STRING_REGEXP.test(start as string)) + ) { messages.push('Invalid start value.'); } } const typeofEnd = typeof end; if (typeofEnd !== 'undefined') { - if (!['string', 'number'].includes(typeofEnd)) { - messages.push('Invalid end value.'); - } else if (typeofEnd === 'string' && !DURATION_STRING_REGEXP.test(end as string)) { + if ( + !['string', 'number'].includes(typeofEnd) + || (typeofEnd === 'string' && !DURATION_STRING_REGEXP.test(end as string)) + ) { messages.push('Invalid end value.'); } } @@ -44,7 +47,7 @@ const validateRequestBody = (body: ClipArgs) => { const getVideoType = (url: string) => { if (url.startsWith('https://www.youtube.com')) { - return VideoType.YOUTUBE; + return YouTube.VIDEO_TYPE as VideoType; } return null; @@ -65,50 +68,23 @@ export const clip: RouteHandlerMethod = async (request, reply) => { reply .status(constants.HTTP_STATUS_UNPROCESSABLE_ENTITY) .send({ - message: 'Unsupported URL.', + errors: ['Unsupported URL.'], }); } - const { url, start, end } = request.body as ClipArgs; - const videoClipperArgs = { - type: videoType, + const clipper = createVideoClipper({ + type: videoType as VideoType, + downloaderExecutablePath: process.env.YOUTUBE_DOWNLOADER_EXECUTABLE_PATH as string, + }); + + const { url, start, end } = request.body as ClipVideoParams; + const clipResult = await clipper({ url, start, end, - downloaderExecutablePath: process.env.YOUTUBE_DOWNLOADER_EXECUTABLE_PATH, - } as CreateVideoClipperParams; - const clipper = createVideoClipper(videoClipperArgs); - clipper.on('process', (arg: Record) => { - request.server.log.info(`${arg.type as string}:${arg.phase as string}`); - if (typeof arg.command === 'string') { - request.server.log.debug(`> ${arg.command}`); - } - }); - - let clipResult: Record; - clipper.on('success', (result: Record) => { - clipResult = result; - }); - - let theError: Error; - clipper.on('error', (error: Error) => { - theError = error; - }); - - clipper.on('end', () => { - if (theError) { - reply - .status(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR) - .send({ - message: theError.message, - }); - return; - } - - reply - .header('Content-Type', clipResult.type as string) - .send(clipResult.output as Buffer); }); - clipper.process(); + reply + .header('Content-Type', clipResult.contentType) + .send(clipResult.content); }; diff --git a/yarn.lock b/yarn.lock index 9c715af..2dfa8d8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -470,6 +470,10 @@ resolved "https://registry.yarnpkg.com/@mdn/browser-compat-data/-/browser-compat-data-5.2.43.tgz#3c13e6801b25e97502b124b760709b4a2c1745b1" integrity sha512-J/hkYAUdvmnN1uk7W2UkwyIFEDbM2HK+rqqZpOKxUn9paJ8yzbUrtCv0dAA+mmCoy6U2bBewRq5bENx+M2YVlA== +"@modal/webvideo-clip-core@link:../core": + version "0.0.0" + uid "" + "@next/eslint-plugin-next@^13.2.4": version "13.2.4" resolved "https://registry.yarnpkg.com/@next/eslint-plugin-next/-/eslint-plugin-next-13.2.4.tgz#3e124cd10ce24dab5d3448ce04104b4f1f4c6ca7"