Better applink icon support

This commit is contained in:
Johannes Zellner
2022-07-07 14:11:14 +02:00
parent 741c21b368
commit f43fd21929
3 changed files with 28 additions and 5 deletions

View File

@@ -5,7 +5,8 @@ exports = module.exports = {
add,
get,
update,
remove
remove,
getIcon
};
const assert = require('assert'),
@@ -31,7 +32,7 @@ function postProcess(result) {
if (result.accessRestriction && !result.accessRestriction.users) result.accessRestriction.users = [];
delete result.accessRestrictionJson;
result.icon = result.icon ? result.icon.toString() : null;
result.icon = result.icon ? result.icon : null;
}
async function list() {
@@ -59,9 +60,10 @@ async function add(applink) {
if (dom.window.document.querySelector('link[rel="icon"]')) {
const favicon = dom.window.document.querySelector('link[rel="icon"]').href ;
const [error, response] = await safe(superagent.get(favicon));
if (error || !response.text) throw new BoxError(BoxError.BAD_FIELD, 'cannot fetch applink icon');
if (error) throw new BoxError(BoxError.BAD_FIELD, 'cannot fetch applink icon');
applink.icon = response.text;
if (response.ok && response.headers['content-type'] === 'image/png') applink.icon = response.body;
else debug(`Failed to fetch icon ${response.status}`);
} else {
debug(`Cannot fetch icon for ${applink.upstreamUri}`);
}
@@ -116,3 +118,13 @@ async function remove(applinkId) {
const result = await database.query(`DELETE FROM applinks WHERE id = ?`, [ applinkId ]);
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Applink not found');
}
async function getIcon(applinkId) {
assert.strictEqual(typeof applinkId, 'string');
debug(`getIcon: ${applinkId}`);
const applink = await get(applinkId);
return applink.icon;
}