A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
chore: move server handlers
Christian Fei25a351c71215
diff --git a/lib/server.js b/lib/server.jsindex fe9b968..d19eca8 100644--- a/lib/server.js+++ b/lib/server.js@@ -60,9 +60,9 @@ export function createServer ({repo, connections = []}) {if (url.pathname === '/api/reclaim-disk-space' && req.method === 'POST')return reclaimDiskSpaceHandler(req, res, repo, connections)if (url.pathname.match(/\/videos\/.*/) && req.method === 'GET')- return watchVideoHandler(req, res)+ return watchVideoHandler(req, res, repo)if (url.pathname.match(/\/captions\/.*/) && req.method === 'GET')- return captionsHandler(req, res)+ return captionsHandler(req, res, repo)/* client */if (url.pathname === '/main.css')@@ -95,276 +95,275 @@ export function createServer ({repo, connections = []}) {return fileHandler('client/components/video-filters.js', 'application/javascript')(req, res)return fileHandler('client/index.html', 'text/html')(req, res)- })-- function fileHandler (filePath, contentType) {- return (req, res) => {- res.writeHead(200, { 'Content-Type': contentType })- res.end(fs.readFileSync(filePath, 'utf8'))- }- }+ })+}- async function getBody (req) {- return new Promise((resolve, reject) => {- let body = ''- req.on('data', chunk => {- body += chunk.toString()- })- req.on('end', () => resolve(body))- req.on('error', reject)+async function getBody (req) {+ return new Promise((resolve, reject) => {+ let body = ''+ req.on('data', chunk => {+ body += chunk.toString()})+ req.on('end', () => resolve(body))+ req.on('error', reject)+ })+}++function getQuery (req) {+ return req.url.indexOf('?') >= 0+ ? querystring.parse(req.url.substring(req.url.indexOf('?') + 1))+ : {}+}++function fileHandler (filePath, contentType) {+ return (req, res) => {+ res.writeHead(200, { 'Content-Type': contentType })+ res.end(fs.readFileSync(filePath, 'utf8'))}+}- function getQuery (req) {- return req.url.indexOf('?') >= 0- ? querystring.parse(req.url.substring(req.url.indexOf('?') + 1))- : {}+async function addChannelHandler(req, res, repo, connections = []) {+ const body = await getBody(req)+ let { name } = JSON.parse(body)+ name = name.trim()++ if (repo.channelExists(name)) {+ res.writeHead(409, { 'Content-Type': 'text/plain' })+ return res.end('Channel already added')}- async function addChannelHandler(req, res, repo, connections = []) {- const body = await getBody(req)- let { name } = JSON.parse(body)- name = name.trim()-- if (repo.channelExists(name)) {- res.writeHead(409, { 'Content-Type': 'text/plain' })- return res.end('Channel already added')- }+ const videos = await updateAndPersistVideosForChannel(name, repo)+ if (Array.isArray(videos)) {+ repo.addChannel(name)+ broadcastSSE(JSON.stringify({type: 'new-videos', name, videos}), connections)+ res.writeHead(201, { 'Content-Type': 'text/plain' })+ return res.end('Channel added')+ }- const videos = await updateAndPersistVideosForChannel(name, repo)- if (Array.isArray(videos)) {- repo.addChannel(name)- broadcastSSE(JSON.stringify({type: 'new-videos', name, videos}), connections)- res.writeHead(201, { 'Content-Type': 'text/plain' })- return res.end('Channel added')- }+ res.writeHead(404, { 'Content-Type': 'text/plain' })+ return res.end('Channel not found')+}- res.writeHead(404, { 'Content-Type': 'text/plain' })- return res.end('Channel not found')- }+async function getChannelHandler(req, res, repo, connections = []) {+ const channels = repo.getChannels()+ res.writeHead(200, { 'Content-Type': 'application/json' })+ return res.end(JSON.stringify(channels))+}- async function getChannelHandler(req, res, repo, connections = []) {- const channels = repo.getChannels()- res.writeHead(200, { 'Content-Type': 'application/json' })- return res.end(JSON.stringify(channels))+async function downloadVideoHandler(req, res, repo, connections = []) {+ const body = await getBody(req)+ let { id, external } = JSON.parse(body)++ if (isUnsupportedUrl(id)) {+ console.log('unsupported url', id)+ res.writeHead(400)+ return res.end()}+ if (isYouTubeUrl(id)) id = extractIdFromUrl(id)- async function downloadVideoHandler(req, res, repo, connections = []) {- const body = await getBody(req)- let { id, external } = JSON.parse(body)-- if (isUnsupportedUrl(id)) {- console.log('unsupported url', id)- res.writeHead(400)- return res.end()- }- if (isYouTubeUrl(id)) id = extractIdFromUrl(id)+ downloadVideo(id, repo, (line) =>+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))+ .then(() => {+ broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id, downloaded: true}), connections)+ if (external) broadcastSSE(JSON.stringify({type: 'new-videos', videos: [repo.getVideo(id)]}), connections)+ })+ .catch((error) =>+ broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.stderr}), connections))- downloadVideo(id, repo, (line) =>- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))- .then(() => {- broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id, downloaded: true}), connections)- if (external) broadcastSSE(JSON.stringify({type: 'new-videos', videos: [repo.getVideo(id)]}), connections)- })- .catch((error) =>- broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.stderr}), connections))+ res.writeHead(200, { 'Content-Type': 'text/plain' })+ res.end('Download started')+}- res.writeHead(200, { 'Content-Type': 'text/plain' })- res.end('Download started')- }+async function summarizeVideoHandler(req, res, repo, connections = [], llmSettings = {}) {+ const body = await getBody(req)+ const { id } = JSON.parse(body)++ summarizeVideo(id, repo, llmSettings, (line) =>+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))+ .then(({summary, transcript}) =>+ broadcastSSE(JSON.stringify({type: 'summary', summary, transcript, videoId: id}), connections))+ .catch((error) => {+ broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.message}), connections)+ broadcastSSE(JSON.stringify({type: 'summary-error', videoId: id}), connections)+ })- async function summarizeVideoHandler(req, res, repo, connections = [], llmSettings = {}) {- const body = await getBody(req)- const { id } = JSON.parse(body)-- summarizeVideo(id, repo, llmSettings, (line) =>- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))- .then(({summary, transcript}) =>- broadcastSSE(JSON.stringify({type: 'summary', summary, transcript, videoId: id}), connections))- .catch((error) => {- broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.message}), connections)- broadcastSSE(JSON.stringify({type: 'summary-error', videoId: id}), connections)- })+ res.writeHead(200, { 'Content-Type': 'text/plain' })+ res.end('Download started')+}- res.writeHead(200, { 'Content-Type': 'text/plain' })- res.end('Download started')- }+async function ignoreVideoHandler(req, res, repo, connections = []) {+ const body = await getBody(req)+ let { id } = JSON.parse(body)+ const ignored = repo.toggleIgnoreVideo(id)+ broadcastSSE(JSON.stringify({type: 'ignored', videoId: id, ignored}), connections)+ res.writeHead(200, { 'Content-Type': 'application/json' })+ return res.end(JSON.stringify(ignored))+}- async function ignoreVideoHandler(req, res, repo, connections = []) {- const body = await getBody(req)- let { id } = JSON.parse(body)- const ignored = repo.toggleIgnoreVideo(id)- broadcastSSE(JSON.stringify({type: 'ignored', videoId: id, ignored}), connections)- res.writeHead(200, { 'Content-Type': 'application/json' })- return res.end(JSON.stringify(ignored))- }+async function deleteVideoHandler(req, res, repo, connections = []) {+ const body = await getBody(req)+ let { id } = JSON.parse(body)+ repo.deleteVideo(id)+ broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id, downloaded: false}), connections)+ res.writeHead(200)+ res.end()+}- async function deleteVideoHandler(req, res, repo, connections = []) {- const body = await getBody(req)- let { id } = JSON.parse(body)- repo.deleteVideo(id)- broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id, downloaded: false}), connections)- res.writeHead(200)- res.end()+function watchVideoHandler(req, res, repo) {+ const id = req.url.replace('/videos/', '')+ const video = repo.getVideo(id)+ const location = video.location || `./data/videos/${id}.mp4`+ const contentType = video.format ? `video/${video.format}` : "video/mp4"+ if (!fs.existsSync(location)) {+ res.writeHead(404, { 'Content-Type': 'text/plain' })+ res.end('Video not found')+ return}-- function watchVideoHandler(req, res) {- const id = req.url.replace('/videos/', '')- const video = repo.getVideo(id)- const location = video.location || `./data/videos/${id}.mp4`- const contentType = video.format ? `video/${video.format}` : "video/mp4"- if (!fs.existsSync(location)) {- res.writeHead(404, { 'Content-Type': 'text/plain' })- res.end('Video not found')- return- }- /*https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js*/- const options = {}-- let start- let end-- const range = req.headers.range- if (range) {- const bytesPrefix = "bytes="- if (range.startsWith(bytesPrefix)) {- const bytesRange = range.substring(bytesPrefix.length)- const parts = bytesRange.split("-")- if (parts.length === 2) {- const rangeStart = parts[0] && parts[0].trim()- if (rangeStart && rangeStart.length > 0) {- options.start = start = parseInt(rangeStart)- }- const rangeEnd = parts[1] && parts[1].trim()- if (rangeEnd && rangeEnd.length > 0) {- options.end = end = parseInt(rangeEnd)- }+ /*https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js*/+ const options = {}++ let start+ let end++ const range = req.headers.range+ if (range) {+ const bytesPrefix = "bytes="+ if (range.startsWith(bytesPrefix)) {+ const bytesRange = range.substring(bytesPrefix.length)+ const parts = bytesRange.split("-")+ if (parts.length === 2) {+ const rangeStart = parts[0] && parts[0].trim()+ if (rangeStart && rangeStart.length > 0) {+ options.start = start = parseInt(rangeStart)+ }+ const rangeEnd = parts[1] && parts[1].trim()+ if (rangeEnd && rangeEnd.length > 0) {+ options.end = end = parseInt(rangeEnd)}}}- res.setHeader("content-type", contentType)- // bust cache- res.setHeader('cache-control', 'public, max-age=0')- const stat = fs.statSync(location)+ }+ res.setHeader("content-type", contentType)+ // bust cache+ res.setHeader('cache-control', 'public, max-age=0')+ const stat = fs.statSync(location)- let contentLength = stat.size+ let contentLength = stat.size- if (req.method === "HEAD") {- res.statusCode = 200- res.setHeader("accept-ranges", "bytes")- res.setHeader("content-length", contentLength)- res.end()+ if (req.method === "HEAD") {+ res.statusCode = 200+ res.setHeader("accept-ranges", "bytes")+ res.setHeader("content-length", contentLength)+ res.end()+ }+ else {+ let retrievedLength = contentLength;+ if (start !== undefined && end !== undefined) {+ retrievedLength = end - start + 1;+ } else if (start !== undefined) {+ retrievedLength = contentLength - start;}- else {- let retrievedLength = contentLength;- if (start !== undefined && end !== undefined) {- retrievedLength = end - start + 1;- } else if (start !== undefined) {- retrievedLength = contentLength - start;- }- res.statusCode = start !== undefined || end !== undefined ? 206 : 200+ res.statusCode = start !== undefined || end !== undefined ? 206 : 200- res.setHeader("content-length", retrievedLength)-- if (range !== undefined) {- res.setHeader("content-range", `bytes ${start || 0}-${end || (contentLength-1)}/${contentLength}`)- res.setHeader("accept-ranges", "bytes")- }+ res.setHeader("content-length", retrievedLength)- const fileStream = fs.createReadStream(location, options)- fileStream.on("error", error => {- console.log(`Error reading file ${location}.`)- console.log(error)- res.writeHead(500)- res.end()- })-- fileStream.pipe(res)+ if (range !== undefined) {+ res.setHeader("content-range", `bytes ${start || 0}-${end || (contentLength-1)}/${contentLength}`)+ res.setHeader("accept-ranges", "bytes")}- }- function allVideosHandler(req, res) {- const videos = repo.getVideos()- res.writeHead(200, { 'Content-Type': 'application/json' })- res.end(JSON.stringify(videos))+ const fileStream = fs.createReadStream(location, options)+ fileStream.on("error", error => {+ console.log(`Error reading file ${location}.`)+ console.log(error)+ res.writeHead(500)+ res.end()+ })++ fileStream.pipe(res)}+}- function getVideoQualityHandler(req, res) {- const videoQuality = repo.getVideoQuality()- res.writeHead(200, { 'Content-Type': 'application/json' })- res.end(JSON.stringify(videoQuality))- }+function allVideosHandler(req, res, repo) {+ const videos = repo.getVideos()+ res.writeHead(200, { 'Content-Type': 'application/json' })+ res.end(JSON.stringify(videos))+}- async function setVideoQualityHandler(req, res) {- const body = await getBody(req)- const videoQuality = JSON.parse(body)+function getVideoQualityHandler(req, res, repo) {+ const videoQuality = repo.getVideoQuality()+ res.writeHead(200, { 'Content-Type': 'application/json' })+ res.end(JSON.stringify(videoQuality))+}- const newQuality = repo.setVideoQuality(videoQuality)- if (!newQuality) {- res.writeHead(400)- return res.end()- }- res.writeHead(200, { 'Content-Type': 'application/json' })- res.end(JSON.stringify(newQuality))+async function setVideoQualityHandler(req, res, repo) {+ const body = await getBody(req)+ const videoQuality = JSON.parse(body)++ const newQuality = repo.setVideoQuality(videoQuality)+ if (!newQuality) {+ res.writeHead(400)+ return res.end()}+ res.writeHead(200, { 'Content-Type': 'application/json' })+ res.end(JSON.stringify(newQuality))+}- function diskUsageHandler(req, res) {- const onlyIgnored = getQuery(req).onlyIgnored === 'true'- const videos = repo.getVideos()+function diskUsageHandler(req, res, repo) {+ const onlyIgnored = getQuery(req).onlyIgnored === 'true'+ const videos = repo.getVideos()- const filterFn = onlyIgnored- ? video => video.downloaded && video.ignored- : video => video.downloaded-- const diskSpaceUsed = videos.filter(filterFn)- .reduce((total, video) => {- try {- return total + fs.statSync(`./data/videos/${video.id}.mp4`).size / Math.pow(10, 9)- } catch (err) {- return total- }- }, 0)- res.writeHead(200, { 'Content-Type': 'text/plain' })- res.end(diskSpaceUsed.toFixed(3) + 'GB')- }+ const filterFn = onlyIgnored+ ? video => video.downloaded && video.ignored+ : video => video.downloaded- async function reclaimDiskSpaceHandler(req, res) {- const body = await getBody(req)- const {onlyIgnored} = JSON.parse(body)- const videos = repo.getVideos()-- const filterFn = onlyIgnored- ? video => video.downloaded && video.ignored- : video => video.downloaded-- videos.filter(filterFn)- .forEach((video) => {- try {- const files = fs.globSync(`./data/videos/${video.id}*`)- files.forEach(file => fs.unlinkSync(file))- repo.updateVideo(video.id, {downloaded: false})- } catch (err) {- console.error(err.message)- }- })- repo.saveVideos()- res.writeHead(200)- res.end()- }+ const diskSpaceUsed = videos.filter(filterFn)+ .reduce((total, video) => {+ try {+ return total + fs.statSync(`./data/videos/${video.id}.mp4`).size / Math.pow(10, 9)+ } catch (err) {+ return total+ }+ }, 0)+ res.writeHead(200, { 'Content-Type': 'text/plain' })+ res.end(diskSpaceUsed.toFixed(3) + 'GB')+}- function captionsHandler(req, res) {- // return vtt file of video- const captionsPath = './data' + req.url.replace('captions', 'videos') + '.en.vtt'- console.log(captionsPath)- if (!fs.existsSync(captionsPath)) {- res.writeHead(404)- res.end()- } else {- const fileStream = fs.createReadStream(captionsPath)- fileStream.pipe(res)+async function reclaimDiskSpaceHandler(req, res, repo) {+ const body = await getBody(req)+ const {onlyIgnored} = JSON.parse(body)+ const videos = repo.getVideos()++ const filterFn = onlyIgnored+ ? video => video.downloaded && video.ignored+ : video => video.downloaded++ videos.filter(filterFn)+ .forEach((video) => {+ try {+ const files = fs.globSync(`./data/videos/${video.id}*`)+ files.forEach(file => fs.unlinkSync(file))+ repo.updateVideo(video.id, {downloaded: false})+ } catch (err) {+ console.error(err.message)}- }+ })+ repo.saveVideos()+ res.writeHead(200)+ res.end()+}+function captionsHandler(req, res) {+ // return vtt file of video+ const captionsPath = './data' + req.url.replace('captions', 'videos') + '.en.vtt'+ console.log(captionsPath)+ if (!fs.existsSync(captionsPath)) {+ res.writeHead(404)+ res.end()+ } else {+ const fileStream = fs.createReadStream(captionsPath)+ fileStream.pipe(res)+ }}