1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-22 07:37:29 +00:00

Refactor video fetcher to prefetch data

This commit is contained in:
Lomanic 2021-03-29 00:11:05 +02:00 committed by Cadence Ember
parent 1b580ae0b6
commit dbbe950832
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17

View File

@ -118,10 +118,8 @@ function rewriteVideoDescription(descriptionHtml, id) {
return descriptionHtml return descriptionHtml
} }
async function renderVideo(videoPromise, {user, settings, id, instanceOrigin}, locals = {}) { async function renderVideo(video, {user, settings, id, instanceOrigin}, locals = {}) {
try { try {
// resolve video
const video = await videoPromise
if (!video) throw new Error("The instance returned null.") if (!video) throw new Error("The instance returned null.")
if (video.error) throw new InstanceError(video.error, video.identifier) if (video.error) throw new InstanceError(video.error, video.identifier)
@ -216,19 +214,17 @@ module.exports = [
const t = url.searchParams.get("t") const t = url.searchParams.get("t")
let mediaFragment = converters.tToMediaFragment(t) let mediaFragment = converters.tToMediaFragment(t)
if (req.method === "GET") { if (req.method === "GET") {
if (!settings.local) { if (settings.local) {
const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
const videoPromise = request(outURL).then(res => res.json())
return renderVideo(videoPromise, {user, settings, id, instanceOrigin}, {mediaFragment})
} else {
return render(200, "pug/local-video.pug", {id}) return render(200, "pug/local-video.pug", {id})
} }
const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
const video = await request(outURL).then(res => res.json())
return renderVideo(video, {user, settings, id, instanceOrigin}, {mediaFragment})
} else { // req.method === "POST" } else { // req.method === "POST"
const video = JSON.parse(new URLSearchParams(body.toString()).get("video")) const video = JSON.parse(new URLSearchParams(body.toString()).get("video"))
const videoPromise = Promise.resolve(video)
const instanceOrigin = "http://localhost:3000" const instanceOrigin = "http://localhost:3000"
return renderVideo(videoPromise, {user, settings, id, instanceOrigin}, {mediaFragment}) return renderVideo(video, {user, settings, id, instanceOrigin}, {mediaFragment})
} }
} }
} }