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