diff --git a/ass.js b/ass.js index 97b82b3..67f8a7d 100755 --- a/ass.js +++ b/ass.js @@ -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); diff --git a/ogp.js b/ogp.js index 885d60c..dfc0e1b 100644 --- a/ogp.js +++ b/ogp.js @@ -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) ? `` : '', description: (this.description.length != 0) ? `` : '', site: (this.author.length != 0) ? `` : '', @@ -71,7 +73,7 @@ const html = ` ass - + {{{title}}} {{{description}}} {{{site}}} @@ -81,7 +83,7 @@ const html = ` - Open Graph response for ass. + Open Graph response for ass {{{version}}} `; diff --git a/s3.js b/s3.js new file mode 100644 index 0000000..4d9277f --- /dev/null +++ b/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;