Skip to content

Commit

Permalink
rich content
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Nov 10, 2020
1 parent 353122c commit 42dcd03
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
75 changes: 58 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/')}
Expand Down Expand Up @@ -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(`
<html>
<head>
<meta property="theme-color" content="${config.meta.color}">
<meta property="og:title" content="${req.params.id}">
<meta property="og:url" content="${config.uploader.route}/${req.params.id}">
<meta property="og:image" content="${config.uploader.route}/${req.params.id}">
<meta property="twitter:card" content="summary_large_image">
</head>
<body>
<div style="text-align:center;vertical-align:middle;">
<img src="${config.uploader.route}/${req.params.id}" >
</div>
</body>
</html>
`);
});

server.register(fastifyMultipart);

server.register(fastifyTypeorm, {
Expand Down Expand Up @@ -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 (
Expand All @@ -166,4 +207,4 @@ server.addHook('preHandler', async (req, reply) => {
await app.render404(req.raw, reply.raw);
return (reply.sent = true);
}
});
});
1 change: 1 addition & 0 deletions src/lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ConfigMeta {
export interface ConfigUploader {
directory: string;
route: string;
rich_content_route?: string;
length: number;
blacklisted: string[];
original: boolean;
Expand Down
21 changes: 16 additions & 5 deletions src/lib/api/controllers/RootController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 42dcd03

Please sign in to comment.