added custom embeds via http headers

pull/15/head
tycrek 3 years ago
parent a997a137d3
commit f936d14e18
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -87,6 +87,14 @@ function startup() {
// Get the uploaded time in milliseconds
req.file.timestamp = DateTime.now().toMillis();
// Attach any embed overrides, if necessary
req.file.opengraph = {
title: req.headers['x-ass-og-title'],
description: req.headers['x-ass-og-description'],
author: req.headers['x-ass-og-author'],
color: req.headers['x-ass-og-color']
};
// Save the file information
let resourceId = generateId(generator, resourceIdSize, req.file.originalname);
data[resourceId.split('.')[0]] = req.file;

@ -1,5 +1,6 @@
const Mustache = require('mustache');
const github = require('./package.json').homepage;
const { formatBytes } = require('./utils');
//
class OpenGraph {
@ -10,14 +11,14 @@ class OpenGraph {
filename;
type;
size;
timestamp;
title = '';
description = '';
author = '';
color = '';
showSize = false;
title;
description;
author;
color;
constructor(http, domain, resourceId, { originalname, mimetype, size }) {
constructor(http, domain, resourceId, { originalname, mimetype, size, timestamp, opengraph }) {
this.http = http;
this.domain = domain;
this.resourceId = resourceId;
@ -25,31 +26,12 @@ class OpenGraph {
this.type = mimetype;
this.filename = originalname;
this.size = size;
}
setTitle(title) {
this.title = title;
return this;
}
setDescription(description) {
this.description = description;
return this;
}
setAuthor(author) {
this.author = author;
return this;
}
setColor(color) {
this.color = color;
return this;
}
this.timestamp = timestamp;
setShowSize(showSize) {
this.showSize = showSize;
return this;
this.title = opengraph.title || '';
this.description = opengraph.description || '';
this.author = opengraph.author || '';
this.color = opengraph.color || '';
}
build() {
@ -69,7 +51,10 @@ class OpenGraph {
site: (this.author.length != 0) ? `<meta property="og:site_name" content="${this.author}">` : '',
color: (this.color.length != 0) ? `<meta name="theme-color" content="${this.color}">` : '',
card: !this.type.includes('video') ? `<meta name="twitter:card" content="summary_large_image">` : '',
});
})
.replace(new RegExp('&size', 'g'), formatBytes(this.size))
.replace(new RegExp('&filename', 'g'), this.filename)
.replace(new RegExp('&timestamp', 'g'), this.timestamp);
}
}

@ -20,5 +20,11 @@ module.exports = {
generateId: (mode, lenth, originalName) =>
(mode == idModes.zws) ? zwsGen(lenth)
: (mode == idModes.r) ? randomGen(lenth)
: originalName
}
: originalName,
formatBytes: (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(decimals < 0 ? 0 : decimals)) + ' ' + sizes[i];
}
}

Loading…
Cancel
Save