From 1fecf380343d7dbd7e5e3186b59225cd80c5ad07 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 21 May 2019 20:58:04 -0400 Subject: [PATCH] fix: upload file hash order --- server/controllers/upload.js | 2 +- server/models/assetFolders.js | 14 +++++++++++--- server/models/assets.js | 3 +-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/controllers/upload.js b/server/controllers/upload.js index 72dd9a6a..22f83635 100644 --- a/server/controllers/upload.js +++ b/server/controllers/upload.js @@ -87,7 +87,7 @@ router.post('/u', multer({ ...fileMeta, originalname: sanitize(fileMeta.originalname).toLowerCase(), folderId: folderId, - hierarchy, + assetPath, userId: req.user.id }) res.send('ok') diff --git a/server/models/assetFolders.js b/server/models/assetFolders.js index 629bcbed..e2a32ee9 100644 --- a/server/models/assetFolders.js +++ b/server/models/assetFolders.js @@ -1,6 +1,7 @@ -/* global WIKI */ - const Model = require('objection').Model +const _ = require('lodash') + +/* global WIKI */ /** * Users model @@ -33,11 +34,18 @@ module.exports = class AssetFolder extends Model { } } + /** + * Get full folder hierarchy starting from specified folder to root + * + * @param {Number} folderId Id of the folder + */ static async getHierarchy(folderId) { - return WIKI.models.knex.withRecursive('ancestors', qb => { + const hier = await WIKI.models.knex.withRecursive('ancestors', qb => { qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).union(sqb => { sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id') }) }).select('*').from('ancestors') + // The ancestors are from children to grandparents, must reverse for correct path order. + return _.reverse(hier) } } diff --git a/server/models/assets.js b/server/models/assets.js index b2cace1b..56cba2a9 100644 --- a/server/models/assets.js +++ b/server/models/assets.js @@ -67,8 +67,7 @@ module.exports = class Asset extends Model { static async upload(opts) { const fileInfo = path.parse(opts.originalname) - const folderPath = opts.hierarchy.map(h => h.slug).join('/') - const fileHash = opts.folderId ? assetHelper.generateHash(`${folderPath}/${opts.originalname}`) : assetHelper.generateHash(opts.originalname) + const fileHash = assetHelper.generateHash(opts.assetPath) // Create asset entry const asset = await WIKI.models.assets.query().insert({