From 42dcd03428f5c47bd9255f9b9ef4630134c82d43 Mon Sep 17 00:00:00 2001 From: diced Date: Mon, 9 Nov 2020 19:12:28 -0800 Subject: [PATCH] rich content --- src/index.ts | 75 ++++++++++++++++++----- src/lib/Config.ts | 1 + src/lib/api/controllers/RootController.ts | 21 +++++-- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4ddb3bd5d..bdb0628cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,19 +18,28 @@ import { join } from 'path'; import { ImagesController } from './lib/api/controllers/ImagesController'; import { URLSController } from './lib/api/controllers/URLSController'; import { checkVersion } from './lib/Util'; -import { readFileSync } from 'fs'; +import { existsSync, readFileSync } from 'fs'; import { Image } from './lib/entities/Image'; import { User } from './lib/entities/User'; import { Zipline } from './lib/entities/Zipline'; import { URL } from './lib/entities/URL'; const dev = process.env.NODE_ENV !== 'production'; -(async () => { if (await checkVersion()) Console.logger('Zipline').info('running an outdated version of zipline, please update soon!'); })(); +(async () => { + if (await checkVersion()) + Console.logger('Zipline').info( + 'running an outdated version of zipline, please update soon!' + ); +})(); console.log(` ${magenta(text('Zipline'))} -Version : ${blue(process.env.npm_package_version || readFileSync(join(process.cwd(), 'package.json'), 'utf8'))} +Version : ${blue( + process.env.npm_package_version || + JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf8')) + .version + )} GitHub : ${blue('https://github.com/ZiplineProject/zipline')} Issues : ${blue('https://github.com/ZiplineProject/zipline/issues')} Docs : ${blue('https://zipline.diced.wtf/')} @@ -109,6 +118,33 @@ server.get(`${config.urls.route}/:id`, async function ( return reply.redirect(urlId.url); }); +server.get(`${config.uploader.rich_content_route || '/a'}/:id`, async function ( + req: FastifyRequest<{ Params: { id: string } }>, + reply: FastifyReply +) { + if (!existsSync(join(config.uploader.directory, req.params.id))) { + await app.render404(req.raw, reply.raw); + return (reply.sent = true); + } + + return reply.type('text/html').send(` + + + + + + + + + +
+ +
+ + + `); +}); + server.register(fastifyMultipart); server.register(fastifyTypeorm, { @@ -144,19 +180,24 @@ server.register(fastifyStatic, { server.register(fastifyFavicon); -server.listen({ - port: config.core.port, - host: config.core.host -}, err => { - if (err) throw err; - const info = server.server.address() as AddressInfo; - - Console.logger('Server').info( - `server listening on ${bold( - `${green(info.address)}${reset(':')}${bold(green(info.port.toString()))}` - )}` - ); -}); +server.listen( + { + port: config.core.port, + host: config.core.host + }, + err => { + if (err) throw err; + const info = server.server.address() as AddressInfo; + + Console.logger('Server').info( + `server listening on ${bold( + `${green(info.address)}${reset(':')}${bold( + green(info.port.toString()) + )}` + )}` + ); + } +); server.addHook('preHandler', async (req, reply) => { if ( @@ -166,4 +207,4 @@ server.addHook('preHandler', async (req, reply) => { await app.render404(req.raw, reply.raw); return (reply.sent = true); } -}); \ No newline at end of file +}); diff --git a/src/lib/Config.ts b/src/lib/Config.ts index 9d87dacd2..05bd0ddd6 100644 --- a/src/lib/Config.ts +++ b/src/lib/Config.ts @@ -23,6 +23,7 @@ export interface ConfigMeta { export interface ConfigUploader { directory: string; route: string; + rich_content_route?: string; length: number; blacklisted: string[]; original: boolean; diff --git a/src/lib/api/controllers/RootController.ts b/src/lib/api/controllers/RootController.ts index 7f4a4351a..58db40238 100644 --- a/src/lib/api/controllers/RootController.ts +++ b/src/lib/api/controllers/RootController.ts @@ -23,7 +23,14 @@ const pump = promisify(pipeline); const config = Configuration.readConfig(); const rateLimiterConfig = config.core.ratelimiter - ? { config: { rateLimit: { max: config.core.ratelimiter.requests, timeWindow: config.core.ratelimiter.retry_after } } } + ? { + config: { + rateLimit: { + max: config.core.ratelimiter.requests, + timeWindow: config.core.ratelimiter.retry_after + } + } + } : {}; @Controller('/api') @@ -135,14 +142,18 @@ export class RootController { `image ${fileName}.${ext} was uploaded by ${user.username} (${user.id})` ); + const host = `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ + config.uploader.rich_content_route + ? config.uploader.rich_content_route + : config.uploader.route + }/`; + if (this.webhooks.events.includes(WebhookType.UPLOAD)) WebhookHelper.sendWebhook(this.webhooks.upload.content, { image, - host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.uploader.route}/` + host }); - reply.send( - `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.uploader.route}/${fileName}.${ext}` - ); + reply.send(host); } }