Browse Source

Update route

Use the /api base route.
master
TheoryOfNekomata 1 year ago
parent
commit
0ac6549d47
3 changed files with 10 additions and 12 deletions
  1. +2
    -0
      .env.example
  2. +7
    -11
      src/controllers/ClipController.ts
  3. +1
    -1
      src/routes.ts

+ 2
- 0
.env.example View File

@@ -0,0 +1,2 @@
# Path to yt-dlp.
VIDEO_DOWNLOADER_EXECUTABLE_PATH=

+ 7
- 11
src/controllers/ClipController.ts View File

@@ -7,15 +7,9 @@ import {
import { constants } from 'http2'; import { constants } from 'http2';
import { RouteHandlerMethod } from 'fastify'; 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 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 messages = [] as string[];
const { url, start, end } = body; const { url, start, end } = body;
if (typeof url !== 'string') { if (typeof url !== 'string') {
@@ -54,7 +48,10 @@ const getVideoType = (url: string) => {
}; };


export const clip: RouteHandlerMethod = async (request, reply) => { 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) { if (validationMessages.length > 0) {
reply reply
.status(constants.HTTP_STATUS_BAD_REQUEST) .status(constants.HTTP_STATUS_BAD_REQUEST)
@@ -63,7 +60,7 @@ export const clip: RouteHandlerMethod = async (request, reply) => {
}); });
return; return;
} }
const videoType = getVideoType((request.body as ClipArgs).url as string);
const videoType = getVideoType(url);
if (videoType === null) { if (videoType === null) {
reply reply
.status(constants.HTTP_STATUS_UNPROCESSABLE_ENTITY) .status(constants.HTTP_STATUS_UNPROCESSABLE_ENTITY)
@@ -74,10 +71,9 @@ export const clip: RouteHandlerMethod = async (request, reply) => {


const clipper = createVideoClipper({ const clipper = createVideoClipper({
type: videoType as VideoType, 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({ const clipResult = await clipper({
url, url,
start, start,


+ 1
- 1
src/routes.ts View File

@@ -3,6 +3,6 @@ import SERVER from './server';


SERVER.route({ SERVER.route({
method: 'POST', method: 'POST',
url: '/clip',
url: '/api/clip',
handler: ClipController.clip, handler: ClipController.clip,
}); });

Loading…
Cancel
Save