const Mustache = require('mustache'); const DateTime = require('luxon').DateTime; const { homepage, version } = require('./package.json'); const { s3enabled, s3endpoint, s3bucket } = require('./config.json'); const { formatBytes, randomHexColour } = require('./utils'); // https://ogp.me/ class OpenGraph { http; domain; resourceId; filename; type; size; timestamp; vibrant; title; description; author; color; constructor(http, domain, resourceId, { originalname, mimetype, size, timestamp, opengraph, vibrant }) { this.http = http; this.domain = domain; this.resourceId = resourceId; 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 = !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, { 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', 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'), DateTime.fromMillis(this.timestamp).toLocaleString(DateTime.DATETIME_MED)); } getBuildColor() { return this.color === '&random' ? randomHexColour() : this.color === '&vibrant' ? this.vibrant : this.color; } } const html = ` ass {{{title}}} {{{description}}} {{{site}}} {{{color}}} {{{card}}} Open Graph response for ass {{{version}}} `; module.exports = OpenGraph;