| 1 | import { Request, Response, NextFunction } from "express"; |
| 2 | import { AppError } from "../utils/errors"; |
| 3 | import { StatusCodes } from "http-status-codes"; |
| 4 | import validator from "validator"; |
| 5 | |
| 6 | export const validateYoutubeUrl = ( |
| 7 | req: Request, |
| 8 | res: Response, |
| 9 | next: NextFunction |
| 10 | ) => { |
| 11 | const { url } = req.body; |
| 12 | |
| 13 | if (!url) { |
| 14 | return next(new AppError(StatusCodes.BAD_REQUEST, "URL is required")); |
| 15 | } |
| 16 | |
| 17 | if (!validator.isURL(url)) { |
| 18 | return next(new AppError(StatusCodes.BAD_REQUEST, "Invalid URL")); |
| 19 | } |
| 20 | |
| 21 | // check if the url is a youtube url |
| 22 | const youtubeUrlRegex = |
| 23 | /^(https?:\/\/)?(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})?/; |
| 24 | if (!youtubeUrlRegex.test(url)) { |
| 25 | return next(new AppError(StatusCodes.BAD_REQUEST, "Invalid YouTube URL")); |
| 26 | } |
| 27 | |
| 28 | next(); |
| 29 | }; |