Kaynağa Gözat

Refactor web API

Use refactored core SDK based on Promises.
master
TheoryOfNekomata 1 yıl önce
ebeveyn
işleme
6709e8505c
3 değiştirilmiş dosya ile 27 ekleme ve 47 silme
  1. +1
    -1
      package.json
  2. +22
    -46
      src/controllers/ClipController.ts
  3. +4
    -0
      yarn.lock

+ 1
- 1
package.json Dosyayı Görüntüle

@@ -22,7 +22,7 @@
"vitest": "^0.28.1"
},
"dependencies": {
"@modal/webvideo-clip-core": "*",
"@modal/webvideo-clip-core": "link:../core",
"fastify": "^4.12.0"
},
"scripts": {


+ 22
- 46
src/controllers/ClipController.ts Dosyayı Görüntüle

@@ -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<string, unknown>) => {
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<string, unknown>;
clipper.on('success', (result: Record<string, unknown>) => {
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);
};

+ 4
- 0
yarn.lock Dosyayı Görüntüle

@@ -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"


Yükleniyor…
İptal
Kaydet