backend/controller/video.js

163 lines
4.4 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const DOMPurify = require('isomorphic-dompurify')
const metadata = require('../utils/metadata.js')
exports.getVideo = async (req, res) => {
const info = await prisma.videos.findFirst({
where: {
id: req.params.id
},
select: {
title: true,
description: true,
thumbnail: true,
source: true,
published: true,
archived: true,
channel: true,
channelId: true,
channelAvatar: true,
channelVerified: true,
disabled: true
}
})
if (!info) return res.json({ error: '404' })
res.json({
...info,
description: DOMPurify.sanitize(info.description),
})
}
exports.getChannel = async (req, res) => {
2023-03-24 14:20:06 +00:00
const videos = await metadata.getChannelVideos(req.params.id)
const channel = await metadata.getChannel(req.params.id)
2023-03-24 15:49:25 +00:00
if (!videos || !channel) return res.json({ error: '500' })
2023-03-03 16:44:40 +00:00
if (videos.error) return res.json({ error: '404' })
const archived = await prisma.videos.findMany({
where: {
channelId: req.params.id
},
select: {
id: true,
title: true,
thumbnail: true,
published: true,
archived: true
}
})
var allVideos = []
allVideos = allVideos.concat((videos).map(video => {
2023-03-03 16:44:40 +00:00
return {
id: video.url.replace('/watch?v=', ''),
published: (new Date(video.uploaded)).toISOString().slice(0,10),
...video
}
}))
await Promise.all(archived.map(async (v) => {
const allVideo = allVideos.find(o => o.id == v.id)
if (allVideo) {
const index = allVideos.findIndex(o => o.id == v.id)
allVideos[index] = v
} else {
2023-03-24 14:20:06 +00:00
const live = await metadata.getVideoMetadata(v.id)
2023-03-03 16:44:40 +00:00
allVideos.push({
...v,
deleted: live.error ? true : false
})
}
}))
allVideos.sort((a, b) => new Date(b.published) - new Date(a.published))
res.json({
name: channel.name,
avatar: channel.avatarUrl,
verified: channel.verified,
2023-03-03 16:44:40 +00:00
videos: allVideos
})
}
exports.getPlaylist = async (req, res) => {
2023-03-24 14:20:06 +00:00
const playlist = await metadata.getPlaylistVideos(req.params.id)
2023-03-24 15:49:25 +00:00
if (!playlist) return res.json({ error: '500' })
2023-03-03 16:44:40 +00:00
if (playlist.error) return res.json({ error: '404' })
const playlistArchived = await prisma.videos.findMany({
where: {
playlist: req.params.id
},
select: {
id: true,
title: true,
thumbnail: true,
published: true,
archived: true
}
})
var allVideos = []
allVideos = allVideos.concat((playlist.relatedStreams).map(video => {
return {
id: video.url.replace('/watch?v=', ''),
published: (new Date(video.uploaded)).toISOString().slice(0,10),
...video
}
}))
await Promise.all(playlistArchived.map(async (v) => {
const allVideo = allVideos.find(o => o.id == v.id)
if (allVideo) {
const index = allVideos.findIndex(o => o.id == v.id)
allVideos[index] = v
} else {
2023-03-24 14:20:06 +00:00
const live = await metadata.getVideoMetadata(v.id)
2023-03-03 16:44:40 +00:00
allVideos.push({
...v,
deleted: live.error ? true : false
})
}
}))
await Promise.all(allVideos.map(async (v) => {
if (!v.archived) {
const video = await prisma.videos.findFirst({
where: {
id: v.id
},
select: {
id: true,
title: true,
thumbnail: true,
published: true,
archived: true
}
})
if (video) {
const index = allVideos.findIndex(o => o.id == v.id)
allVideos[index] = video
}
}
}))
allVideos.sort((a, b) => new Date(a.published) - new Date(b.published))
res.json({
name: playlist.name,
channel: playlist.uploader,
url: playlist.uploaderUrl,
avatar: playlist.uploaderAvatar,
videos: allVideos
})
}