diff --git a/base/static_routes.mjs b/base/static_routes.mjs index ebf8e27..7ce8d01 100644 --- a/base/static_routes.mjs +++ b/base/static_routes.mjs @@ -6,7 +6,7 @@ export default class StaticRoutes { } register(server) { - server.flaska.get('/health', this.health.bind(this)) + server.flaska.get('/api/health', this.health.bind(this)) } health(ctx) { diff --git a/discord_embed/api/post.mjs b/discord_embed/api/post.mjs index 839d239..401161c 100644 --- a/discord_embed/api/post.mjs +++ b/discord_embed/api/post.mjs @@ -16,6 +16,40 @@ export default class IndexPost { server.flaska.post('/', [ server.formidable({ maxFileSize: 8 * 1024 * 1024, }), ], this.createNewLink.bind(this)) + server.flaska.get('/video/:id', this.videoRedirect.bind(this)) + } + + async videoRedirect(ctx) { + try { + let id = AlphabeticID.decode(ctx.params.id) + let videoLink = null + if (id) { + let res = await ctx.db.safeCallProc('discord_embed.link_get', [id - 3843]) + if (res.first.length) { + videoLink = res.first[0].video_link + } else { + ctx.status = 404 + } + } + + if (videoLink) { + ctx.status = 302 + ctx.headers['Location'] = videoLink + ctx.type = 'application/octet-stream' + ctx.body = ` +Redirecting +Click here if it doesn't redirect +` + return + } else { + ctx.status = 404 + ctx.state.error = 'Video not found.' + } + } catch (err) { + ctx.log.error(err, 'Unable to fetch resource ' + ctx.url.slice(1)) + ctx.state.error = 'Unknown error while fetching link.' + } + return this.serve.serveIndex(ctx) } hasErrors(ctx, hasMedia) { @@ -96,16 +130,13 @@ export default class IndexPost { if (!error) { try { - let id = 'health' - while (id === 'health') { - let params = [ - ctx.state.video, - ctx.state.image, - ctx.req.ip, - ] - let res = await ctx.db.safeCallProc('discord_embed.link_add', params) - id = AlphabeticID.encode(res.first[0].id + 3843) - } + let params = [ + ctx.state.video, + ctx.state.image, + ctx.req.ip, + ] + let res = await ctx.db.safeCallProc('discord_embed.link_add', params) + id = AlphabeticID.encode(res.first[0].id + 3843) redirect = `${this.frontend}/${id}` } catch (err) { diff --git a/discord_embed/api/serve.mjs b/discord_embed/api/serve.mjs index 7b646e2..331f4ea 100644 --- a/discord_embed/api/serve.mjs +++ b/discord_embed/api/serve.mjs @@ -37,8 +37,8 @@ export default class ServeHandler extends Parent { if (id) { let res = await ctx.db.safeCallProc('discord_embed.link_get', [id - 3843]) if (res.first.length) { - videoLink = res.first[0].video_link - imageLink = res.first[0].image_link + videoLink = encodeURIComponent(res.first[0].video_link) + imageLink = encodeURIComponent(res.first[0].image_link) } else { ctx.status = 404 } diff --git a/discord_embed/package.json b/discord_embed/package.json index cd3dc77..4a218f5 100644 --- a/discord_embed/package.json +++ b/discord_embed/package.json @@ -1,6 +1,6 @@ { "name": "discord_embed", - "version": "1.0.4", + "version": "1.0.5", "port": 4120, "description": "AV1 discord server embed helper", "main": "index.js", diff --git a/discord_embed/public/index.html b/discord_embed/public/index.html index 547738f..fe08a53 100644 --- a/discord_embed/public/index.html +++ b/discord_embed/public/index.html @@ -2,7 +2,7 @@ - Discord Embedder from AV1 server 1.0.4 + Discord Embedder from AV1 server 1.0.5 {{ if (imageLink) { }} @@ -226,7 +226,7 @@ video { if (isExample) { generateurl.innerText = baseSite + '?v=