From 4dafc79cb74d901bb9454f78277298f020543bb5 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 02:55:05 +0900 Subject: fix authorization --- src/api/utils/Util.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/api/utils') diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index b8d960d..80bffd5 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -206,7 +206,15 @@ class Util { } } - static isAuthorized(req) { + static async isAuthorized(req) { + if (req.headers.token) { + if (!this.options.canApiKey) return false; + const user = await db.table('users').where({ apiKey: req.headers.token }).first(); + if (!user) return false; + if (!user.enabled) return false; + return true; + } + if (!req.headers.authorization) return false; const token = req.headers.authorization.split(' ')[1]; if (!token) return false; -- cgit v1.2.3 From 5f58431409e1a4e875cd8121cfe9dc47cfecc65e Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 02:57:24 +0900 Subject: Fix authorization --- src/api/utils/Util.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/api/utils') diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 80bffd5..7f6dd22 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -208,10 +208,8 @@ class Util { static async isAuthorized(req) { if (req.headers.token) { - if (!this.options.canApiKey) return false; const user = await db.table('users').where({ apiKey: req.headers.token }).first(); - if (!user) return false; - if (!user.enabled) return false; + if (!user || !user.enabled) return false; return true; } -- cgit v1.2.3 From 407fb8bcc31cd69394a2444db53b710cc2dc4d55 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 03:05:12 +0900 Subject: Fix authorization --- src/api/utils/Util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/utils') diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 7f6dd22..91ab663 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -210,7 +210,7 @@ class Util { if (req.headers.token) { const user = await db.table('users').where({ apiKey: req.headers.token }).first(); if (!user || !user.enabled) return false; - return true; + return user; } if (!req.headers.authorization) return false; -- cgit v1.2.3 From 6dd7500084bf7306f66e8f65367b90f1049e3f15 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 03:35:08 +0900 Subject: Fix deleting files without thumb --- src/api/utils/Util.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/api/utils') diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 91ab663..4674dde 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -154,8 +154,6 @@ class Util { const thumbName = this.getFileThumbnail(filename); try { await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename)); - await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', thumbName)); - await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', thumbName)); if (deleteFromDB) { await db.table('files').where('name', filename).delete(); } @@ -163,6 +161,14 @@ class Util { log.error(`There was an error removing the file < ${filename} >`); log.error(error); } + + try { + await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', thumbName)); + await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', thumbName)); + } catch (error) { + log.error(`There was an error removing the thumbs for file < ${filename} >`); + log.error(error); + } } static async deleteAllFilesFromAlbum(id) { -- cgit v1.2.3 From b70a75da1af00e932319b3ba24ccde860a6c7f48 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 03:37:52 +0900 Subject: Fix for real --- src/api/utils/Util.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/api/utils') diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 4674dde..885228f 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -154,6 +154,13 @@ class Util { const thumbName = this.getFileThumbnail(filename); try { await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename)); + if (thumbName) { + const thumb = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', thumbName); + const thumbSquare = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', thumbName); + if (await jetpack.existsAsync(thumb)) jetpack.removeAsync(thumb); + if (await jetpack.existsAsync(thumbSquare)) jetpack.removeAsync(thumbSquare); + } + if (deleteFromDB) { await db.table('files').where('name', filename).delete(); } @@ -161,14 +168,6 @@ class Util { log.error(`There was an error removing the file < ${filename} >`); log.error(error); } - - try { - await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', thumbName)); - await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', thumbName)); - } catch (error) { - log.error(`There was an error removing the thumbs for file < ${filename} >`); - log.error(error); - } } static async deleteAllFilesFromAlbum(id) { -- cgit v1.2.3 From d644b21d431c03263aa7191fe54a984aac96f979 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 05:50:47 +0900 Subject: Make thumbnails webp (bye bye safari) --- src/api/utils/Util.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/api/utils') diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 885228f..a4af81e 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -36,7 +36,7 @@ class Util { static generateThumbnails(filename) { const ext = path.extname(filename).toLowerCase(); - const output = `${filename.slice(0, -ext.length)}.png`; + const output = `${filename.slice(0, -ext.length)}.webp`; if (imageExtensions.includes(ext)) return this.generateThumbnailForImage(filename, output); if (videoExtensions.includes(ext)) return this.generateThumbnailForVideo(filename); return null; @@ -46,11 +46,11 @@ class Util { const file = await jetpack.readAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename), 'buffer'); await sharp(file) .resize(64, 64) - .toFormat('png') + .toFormat('webp') .toFile(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', output)); await sharp(file) .resize(225, null) - .toFormat('png') + .toFormat('webp') .toFile(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', output)); } @@ -76,8 +76,9 @@ class Util { static getFileThumbnail(filename) { if (!filename) return null; const ext = path.extname(filename).toLowerCase(); - if (!imageExtensions.includes(ext) && !videoExtensions.includes(ext)) return null; - return `${filename.slice(0, -ext.length)}.png`; + const extension = imageExtensions.includes(ext) ? 'webp' : videoExtensions.includes(ext) ? 'png' : null; + if (!extension) return null; + return `${filename.slice(0, -ext.length)}.${extension}`; } static constructFilePublicLink(file) { -- cgit v1.2.3