- const startIsOk = req.query.url.startsWith(urlShouldStartWith)
- const matches = videoWatchRegex.exec(req.query.url)
- if (startIsOk === false || matches === null) {
- return res.status(400)
- .json({ error: 'Invalid url.' })
- .end()
+ try {
+ urlPath = new URL(url).pathname
+ } catch (err) {
+ return res.fail({
+ status: HttpStatusCode.BAD_REQUEST_400,
+ message: err.message,
+ data: {
+ url
+ }
+ })
+ }
+
+ const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
+ const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
+
+ const startIsOk = isVideo || isPlaylist
+
+ const parts = urlPath.split('/')
+
+ if (startIsOk === false || parts.length === 0) {
+ return res.fail({
+ status: HttpStatusCode.BAD_REQUEST_400,
+ message: 'Invalid url.',
+ data: {
+ url
+ }
+ })
+ }
+
+ const elementId = toCompleteUUID(parts.pop())
+ if (isIdOrUUIDValid(elementId) === false) {
+ return res.fail({ message: 'Invalid video or playlist id.' })
+ }
+
+ if (isVideo) {
+ const video = await loadVideo(elementId, 'all')
+
+ if (!video) {
+ return res.fail({
+ status: HttpStatusCode.NOT_FOUND_404,
+ message: 'Video not found'
+ })