1
0
Fork 0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2026-05-26 12:32:25 +00:00

Proxy video thumbnails through selected instance

This commit is contained in:
Cadence Ember 2020-10-18 21:51:09 +13:00
parent 2030f623a0
commit a96d97c9d8
No known key found for this signature in database
GPG key ID: BC1C2C61CF521B17
9 changed files with 22 additions and 16 deletions

View file

@ -41,7 +41,8 @@ module.exports = [
const id = url.searchParams.get("v")
const user = getUser(req)
const settings = user.getSettingsOrDefaults()
const outURL = `${settings.instance}/api/v1/videos/${id}`
const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
try {
const video = await fetch(outURL).then(res => res.json())
if (!video) throw new Error("The instance returned null.")
@ -57,23 +58,23 @@ module.exports = [
}
}
const subscribed = user.isSubscribed(video.authorId)
return render(200, "pug/video.pug", {video, subscribed})
return render(200, "pug/video.pug", {video, subscribed, instanceOrigin})
} catch (e) {
let message = pug.render("pre= error", {error: e.stack || e.toString()})
if (e instanceof fetch.FetchError) {
const template = `
p The selected instance, #[code= instance], did not respond correctly.
p The selected instance, #[code= instanceOrigin], did not respond correctly.
p Requested URL: #[a(href=url)= url]
`
message = pug.render(template, {instance: settings.instance, url: outURL})
message = pug.render(template, {instanceOrigin, url: outURL})
} else if (e instanceof InstanceError) {
const template = `
p #[strong= error.message]
if error.identifier
p #[code= error.identifier]
p That error was generated by #[code= instance].
p That error was generated by #[code= instanceOrigin].
`
message = pug.render(template, {instance: settings.instance, error: e})
message = pug.render(template, {instanceOrigin, error: e})
}
return render(500, "pug/video.pug", {video: {videoId: id}, error: true, message})
}