diff --git a/ass.js b/ass.js index ecaee33..731a6cc 100755 --- a/ass.js +++ b/ass.js @@ -20,12 +20,11 @@ const fetch = require('node-fetch'); const marked = require('marked'); const { DateTime } = require('luxon'); const { WebhookClient, MessageEmbed } = require('discord.js'); -const OpenGraph = require('./ogp'); const Thumbnail = require('./thumbnails'); const Vibrant = require('./vibrant'); const Hash = require('./hash'); const { uploadLocal, uploadS3, deleteS3 } = require('./storage'); -const { path, saveData, log, verify, getTrueHttp, getTrueDomain, renameFile, generateToken, generateId, formatBytes, formatTimestamp, arrayEquals, getS3url, getDirectUrl, getResourceColor, downloadTempS3, sanitize } = require('./utils'); +const { path, saveData, log, verify, getTrueHttp, getTrueDomain, renameFile, generateToken, generateId, formatBytes, formatTimestamp, arrayEquals, getS3url, getDirectUrl, getSafeExt, getResourceColor, downloadTempS3, sanitize, replaceholder } = require('./utils'); const { CODE_NO_CONTENT, CODE_BAD_REQUEST, CODE_UNAUTHORIZED, CODE_NOT_FOUND } = require('./MagicNumbers.json'); //#endregion @@ -240,27 +239,30 @@ function startup() { app.get('/:resourceId', (req, res) => { const { resourceId } = req.ass; const fileData = data[resourceId]; - - const requiredItems = { - randomId: fileData.randomId, - originalname: escape(fileData.originalname), - mimetype: fileData.mimetype, - size: fileData.size, - timestamp: fileData.timestamp, - opengraph: fileData.opengraph, - vibrant: fileData.vibrant, - }; - - // If the client is a social bot (such as Discord or Instagram), send an Open Graph embed - if (req.useragent.isBot) res.type('html').send(new OpenGraph(getTrueHttp(), getTrueDomain(), resourceId, requiredItems).build()); - else res.render('view', { - title: requiredItems.originalname, + const isVideo = fileData.mimetype.includes('video'); + + // Build OpenGraph meta tags + const og = fileData.opengraph, ogs = ['']; + og.title && (ogs.push(``)); + og.description && (ogs.push(``)); + og.author && (ogs.push(``)); + og.color && (ogs.push(``)); + !isVideo && (ogs.push(``)); + + // Send the view to the client + res.render('view', { + isVideo, + title: escape(fileData.originalname), uploader: users[fileData.token].username, timestamp: formatTimestamp(fileData.timestamp), size: formatBytes(fileData.size), color: getResourceColor(fileData.opengraph.color || null, fileData.vibrant), - isVideo: fileData.mimetype.includes('video'), resourceAttr: { src: getDirectUrl(resourceId) }, + discordUrl: `${getDirectUrl(resourceId)}${getSafeExt(fileData.mimetype)}`, + oembedUrl: `${getTrueHttp()}${getTrueDomain()}/${resourceId}/oembed.json`, + ogtype: isVideo ? 'video.other' : 'image', + urlType: `og:${isVideo ? 'video' : 'image'}`, + opengraph: replaceholder(ogs.join('\n'), fileData) }); }); @@ -305,10 +307,10 @@ function startup() { res.type('json').send({ version: '1.0', type: mimetype.includes('video') ? 'video' : 'photo', - author_name: opengraph.author, author_url: opengraph.authorUrl, - provider_name: opengraph.provider, - provider_url: opengraph.providerUrl + provider_url: opengraph.providerUrl, + author_name: replaceholder(opengraph.author || '', data[resourceId]), + provider_name: replaceholder(opengraph.provider || '', data[resourceId]) }); }); diff --git a/ogp.js b/ogp.js deleted file mode 100644 index 4639c6b..0000000 --- a/ogp.js +++ /dev/null @@ -1,90 +0,0 @@ -const Mustache = require('mustache'); -const { homepage, version } = require('./package.json'); -const { formatBytes, getDirectUrl, getResourceColor, formatTimestamp, getSafeExt } = require('./utils'); - -// https://ogp.me/ -class OpenGraph { - http; - domain; - resourceId; - randomId; - - filename; - type; - size; - timestamp; - vibrant; - - title; - description; - author; - color; - - constructor(http, domain, resourceId, { randomId, originalname, mimetype, size, timestamp, opengraph, vibrant }) { - this.http = http; - this.domain = domain; - this.resourceId = resourceId; - this.randomId = randomId; - - this.type = mimetype; - this.filename = originalname; - this.size = size; - this.timestamp = timestamp; - this.vibrant = vibrant; - - this.title = opengraph.title || ''; - this.description = opengraph.description || ''; - this.author = opengraph.author || ''; - this.color = opengraph.color || ''; - } - - build() { - let resourceUrl = getDirectUrl(this.resourceId) + getSafeExt(this.type); - return Mustache.render(html, { - homepage, - version, - - http: this.http, - domain: this.domain, - resourceId: this.resourceId, - resourceUrl, - - media: `<${this.type.includes('video') ? 'video' : 'img'} src="${resourceUrl}" style="height: 50vh;">`, - ogtype: this.type.includes('video') ? 'video.other' : 'image', - type: this.type.includes('video') ? 'video' : 'image', - title: (this.title.length != 0) ? `` : '', - description: (this.description.length != 0) ? `` : '', - site: (this.author.length != 0) ? `` : '', - color: (this.color.length != 0) ? `` : '', - card: !this.type.includes('video') ? `` : '', - }) - .replace(new RegExp('&size', 'g'), formatBytes(this.size)) - .replace(new RegExp('&filename', 'g'), this.filename) - .replace(new RegExp('×tamp', 'g'), formatTimestamp(this.timestamp)); - } -} - -const html = ` - -
-