Proxy video thumbnails through selected instance
This commit is contained in:
parent
2030f623a0
commit
a96d97c9d8
|
@ -11,7 +11,8 @@ module.exports = [
|
||||||
const settings = user.getSettingsOrDefaults()
|
const settings = user.getSettingsOrDefaults()
|
||||||
const data = await fetchChannel(id, settings.instance)
|
const data = await fetchChannel(id, settings.instance)
|
||||||
const subscribed = user.isSubscribed(id)
|
const subscribed = user.isSubscribed(id)
|
||||||
return render(200, "pug/channel.pug", {data, subscribed})
|
const instanceOrigin = settings.instance
|
||||||
|
return render(200, "pug/channel.pug", {data, subscribed, instanceOrigin})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
const fetch = require("node-fetch")
|
const fetch = require("node-fetch")
|
||||||
const {render} = require("pinski/plugins")
|
const {render} = require("pinski/plugins")
|
||||||
|
const {getUser} = require("../utils/getuser")
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
{
|
{
|
||||||
route: "/(?:search|results)", methods: ["GET"], code: async ({url}) => {
|
route: "/(?:search|results)", methods: ["GET"], code: async ({req, url}) => {
|
||||||
const query = url.searchParams.get("q") || url.searchParams.get("search_query")
|
const query = url.searchParams.get("q") || url.searchParams.get("search_query")
|
||||||
const fetchURL = new URL("http://localhost:3000/api/v1/search")
|
const fetchURL = new URL("http://localhost:3000/api/v1/search")
|
||||||
fetchURL.searchParams.set("q", query)
|
fetchURL.searchParams.set("q", query)
|
||||||
const results = await fetch(fetchURL.toString()).then(res => res.json())
|
const results = await fetch(fetchURL.toString()).then(res => res.json())
|
||||||
return render(200, "pug/search.pug", {query, results})
|
const instanceOrigin = getUser(req).getSettingsOrDefaults().instance
|
||||||
|
return render(200, "pug/search.pug", {query, results, instanceOrigin})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -33,7 +33,8 @@ module.exports = [
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return render(200, "pug/subscriptions.pug", {hasSubscriptions, videos, channels, refreshed, timeToPastText})
|
const instanceOrigin = user.getSettingsOrDefaults().instance
|
||||||
|
return render(200, "pug/subscriptions.pug", {hasSubscriptions, videos, channels, refreshed, timeToPastText, instanceOrigin})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
13
api/video.js
13
api/video.js
|
@ -41,7 +41,8 @@ module.exports = [
|
||||||
const id = url.searchParams.get("v")
|
const id = url.searchParams.get("v")
|
||||||
const user = getUser(req)
|
const user = getUser(req)
|
||||||
const settings = user.getSettingsOrDefaults()
|
const settings = user.getSettingsOrDefaults()
|
||||||
const outURL = `${settings.instance}/api/v1/videos/${id}`
|
const instanceOrigin = settings.instance
|
||||||
|
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
|
||||||
try {
|
try {
|
||||||
const video = await fetch(outURL).then(res => res.json())
|
const video = await fetch(outURL).then(res => res.json())
|
||||||
if (!video) throw new Error("The instance returned null.")
|
if (!video) throw new Error("The instance returned null.")
|
||||||
|
@ -57,23 +58,23 @@ module.exports = [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const subscribed = user.isSubscribed(video.authorId)
|
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) {
|
} catch (e) {
|
||||||
let message = pug.render("pre= error", {error: e.stack || e.toString()})
|
let message = pug.render("pre= error", {error: e.stack || e.toString()})
|
||||||
if (e instanceof fetch.FetchError) {
|
if (e instanceof fetch.FetchError) {
|
||||||
const template = `
|
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]
|
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) {
|
} else if (e instanceof InstanceError) {
|
||||||
const template = `
|
const template = `
|
||||||
p #[strong= error.message]
|
p #[strong= error.message]
|
||||||
if error.identifier
|
if error.identifier
|
||||||
p #[code= 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})
|
return render(500, "pug/video.pug", {video: {videoId: id}, error: true, message})
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,4 +29,4 @@ block content
|
||||||
.videos
|
.videos
|
||||||
each video in data.latestVideos
|
each video in data.latestVideos
|
||||||
.channel-video
|
.channel-video
|
||||||
+video_list_item(video)
|
+video_list_item(video, instanceOrigin)
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
mixin video_list_item(video)
|
mixin video_list_item(video, instanceOrigin)
|
||||||
|
- if (!instanceOrigin) instanceOrigin = "https://i.ytimg.com"
|
||||||
- let link = `/watch?v=${video.videoId}`
|
- let link = `/watch?v=${video.videoId}`
|
||||||
a(href=link tabindex="-1").thumbnail
|
a(href=link tabindex="-1").thumbnail
|
||||||
img(src=`https://i.ytimg.com/vi/${video.videoId}/mqdefault.jpg` width=320 height=180 alt="").image
|
img(src=`${instanceOrigin}/vi/${video.videoId}/mqdefault.jpg` width=320 height=180 alt="").image
|
||||||
if video.second__lengthText != undefined
|
if video.second__lengthText != undefined
|
||||||
span.duration= video.second__lengthText
|
span.duration= video.second__lengthText
|
||||||
.info
|
.info
|
||||||
|
|
|
@ -9,4 +9,4 @@ block content
|
||||||
main.search-page
|
main.search-page
|
||||||
each result in results
|
each result in results
|
||||||
.search-result
|
.search-result
|
||||||
+video_list_item(result)
|
+video_list_item(result, instanceOrigin)
|
||||||
|
|
|
@ -29,7 +29,7 @@ block content
|
||||||
|
|
||||||
each video in videos
|
each video in videos
|
||||||
.subscriptions-video
|
.subscriptions-video
|
||||||
+video_list_item(video)
|
+video_list_item(video, instanceOrigin)
|
||||||
else
|
else
|
||||||
.no-subscriptions
|
.no-subscriptions
|
||||||
h2 You have no subscriptions.
|
h2 You have no subscriptions.
|
||||||
|
|
|
@ -67,7 +67,7 @@ block content
|
||||||
h2.related-header Related videos
|
h2.related-header Related videos
|
||||||
each r in video.recommendedVideos
|
each r in video.recommendedVideos
|
||||||
.related-video
|
.related-video
|
||||||
+video_list_item(r)
|
+video_list_item(r, instanceOrigin)
|
||||||
|
|
||||||
else
|
else
|
||||||
//- error
|
//- error
|
||||||
|
|
Loading…
Reference in New Issue