upload files to S3 storage

pull/18/head
tycrek 3 years ago
parent 09c7020b57
commit 9fc4668354
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -7,7 +7,7 @@ try {
}
// Load the config
const { host, port, domain, useSsl, resourceIdSize, gfyIdSize, resourceIdType, isProxied, diskFilePath, saveWithDate, saveAsOriginal } = require('./config.json');
const { host, port, domain, useSsl, resourceIdSize, gfyIdSize, resourceIdType, isProxied, diskFilePath, saveWithDate, saveAsOriginal, s3enabled, s3bucket } = require('./config.json');
//#region Imports
const fs = require('fs-extra');
@ -21,6 +21,7 @@ const { WebhookClient, MessageEmbed } = require('discord.js');
const OpenGraph = require('./ogp');
const Thumbnail = require('./thumbnails');
const Vibrant = require('./vibrant');
const uploadS3 = require('./s3');
const { path, saveData, log, verify, generateToken, generateId, formatBytes, randomHexColour, arrayEquals } = require('./utils');
//#endregion
@ -110,7 +111,12 @@ function startup() {
}));
// Upload file
app.post('/', upload.single('file'), (req, res) => {
!s3enabled
? app.post('/', upload.single('file'), ({ next }) => next())
: app.post('/', (req, res, next) => uploadS3(req, res, (error) => (error ? console.error(error) : log(`File uploaded to S3 [${s3bucket}]`), next())));
// Process uploaded file
app.post('/', (req, res) => {
// Prevent uploads from unauthorized clients
if (!verify(req, users)) return res.sendStatus(401);

@ -1,6 +1,7 @@
const Mustache = require('mustache');
const DateTime = require('luxon').DateTime;
const github = require('./package.json').homepage;
const { homepage, version } = require('./package.json');
const { s3enabled, s3endpoint, s3bucket } = require('./config.json');
const { formatBytes, randomHexColour } = require('./utils');
// https://ogp.me/
@ -38,17 +39,18 @@ class OpenGraph {
}
build() {
let resourceUrl = !s3enabled ? (this.http + this.domain + "/" + this.resourceId + (this.type.includes('video') ? '.mp4' : this.type.includes('gif') ? '.gif' : '')) : `https://${s3bucket}.${s3endpoint}/${this.filename}`;
return Mustache.render(html, {
github,
homepage,
version,
http: this.http,
domain: this.domain,
resourceId: this.resourceId,
resourceUrl,
ogtype: this.type.includes('video') ? 'video.other' : 'image',
type: this.type.includes('video') ? 'video' : 'image',
ext: this.type.includes('video') ? '.mp4' : this.type.includes('gif') ? '.gif' : '',
title: (this.title.length != 0) ? `<meta property="og:title" content="${this.title}">` : '',
description: (this.description.length != 0) ? `<meta property="og:description" content="${this.description}">` : '',
site: (this.author.length != 0) ? `<meta property="og:site_name" content="${this.author}">` : '',
@ -71,7 +73,7 @@ const html = `
<title>ass</title>
<!-- Open Graph (https://ogp.me/) -->
<meta property="og:type" content="{{{ogtype}}}">
<meta property="og:{{{type}}}" content="{{{http}}}{{{domain}}}/{{{resourceId}}}{{{ext}}}">
<meta property="og:{{{type}}}" content="{{{resourceUrl}}}">
{{{title}}}
{{{description}}}
{{{site}}}
@ -81,7 +83,7 @@ const html = `
<link rel="alternate" type="application/json+oembed" href="{{{http}}}{{{domain}}}/{{{resourceId}}}/oembed.json" title="oEmbed">
</head>
<body>
Open Graph response for <a href="{{{github}}}" target="_blank">ass</a>.
Open Graph response for <a href="{{{homepage}}}" target="_blank">ass</a> {{{version}}}
</body>
</html>
`;

20
s3.js

@ -0,0 +1,20 @@
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const { s3endpoint, s3bucket, s3accessKey, s3secretKey } = require('./config.json');
const s3 = new aws.S3({
endpoint: new aws.Endpoint(s3endpoint),
credentials: new aws.Credentials({ accessKeyId: s3accessKey, secretAccessKey: s3secretKey })
});
const upload = multer({
storage: multerS3({
s3: s3,
bucket: s3bucket,
acl: 'public-read',
key: (_req, file, cb) => cb(null, file.originalname)
})
}).single('file');
module.exports = upload;
Loading…
Cancel
Save