fix: #7444 Re-factor handling of og:image tags (#7463)

* fix: display proper site logo or og-image with sizes in head

* fix: refactor og:image logic, #7444

- Updated logic to set additional og:image tags based on more
  factors
- logo.png fallback

* feat: save image sizes on post upload, re: #7444

* fix: awaiting addTags in topic controller

* fix: pass strings to meta tags object

* fix: sending absolute image url to meta tag

* fix: removed unneeded async and requiring sync db

* feat: upgrade to calculate image sizes for all post uploads tracked

* fix: tests
This commit is contained in:
Julian Lam
2019-03-18 12:09:10 -04:00
committed by GitHub
parent 745a9589e9
commit 697a6597f3
7 changed files with 190 additions and 59 deletions

View File

@@ -1,17 +1,21 @@
'use strict';
var async = require('async');
var nconf = require('nconf');
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
var util = require('util');
var winston = require('winston');
var db = require('../database');
const image = require('../image');
module.exports = function (Posts) {
Posts.uploads = {};
const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
const pathPrefix = path.join(__dirname, '../../public/uploads/files');
const pathPrefix = path.join(nconf.get('upload_path'), 'files');
const searchRegex = /\/assets\/uploads\/files\/([^\s")]+\.?[\w]*)/g;
Posts.uploads.sync = function (pid, callback) {
@@ -52,6 +56,16 @@ module.exports = function (Posts) {
db.getSortedSetRange('post:' + pid + ':uploads', 0, -1, callback);
};
Posts.uploads.listWithSizes = async function (pid) {
const paths = await Posts.async.uploads.list(pid);
const sizes = await db.async.getObjects(paths.map(path => 'upload:' + md5(path)));
return sizes.map((sizeObj, idx) => {
sizeObj.name = paths[idx];
return sizeObj;
});
};
Posts.uploads.isOrphan = function (filePath, callback) {
// Returns bool indicating whether a file is still CURRENTLY included in any posts
db.sortedSetCard('upload:' + md5(filePath) + ':pids', function (err, length) {
@@ -91,6 +105,9 @@ module.exports = function (Posts) {
let methods = [async.apply(db.sortedSetAdd.bind(db), 'post:' + pid + ':uploads', scores, filePaths)];
methods = methods.concat(filePaths.map(path => async.apply(db.sortedSetAdd.bind(db), 'upload:' + md5(path) + ':pids', now, pid)));
methods = methods.concat(async function () {
await Posts.uploads.saveSize(filePaths);
});
async.parallel(methods, function (err) {
// Strictly return only err
callback(err);
@@ -112,4 +129,35 @@ module.exports = function (Posts) {
callback(err);
});
};
Posts.uploads.saveSize = async (filePaths) => {
const getSize = util.promisify(image.size);
const sizes = await Promise.all(filePaths.map(async function (fileName) {
try {
return await getSize(path.join(pathPrefix, fileName));
} catch (e) {
// Error returned by getSize, do not save size in database
return null;
}
}));
const methods = filePaths.map((filePath, idx) => {
if (!sizes[idx]) {
return null;
}
winston.verbose('[posts/uploads/' + filePath + '] Saving size');
return async.apply(db.setObject, 'upload:' + md5(filePath), {
width: sizes[idx].width,
height: sizes[idx].height,
});
}).filter(Boolean);
async.parallel(methods, function (err) {
if (err) {
winston.error('[posts/uploads] Error while saving post upload sizes: ', err.message);
} else {
winston.verbose('[posts/uploads] Finished saving post upload sizes.');
}
});
};
};