2022-03-22 18:19:46 +01:00
|
|
|
'use strict';
|
|
|
|
|
2022-04-12 16:43:06 +02:00
|
|
|
const uuid = require('uuid').v4;
|
2022-04-07 18:00:21 +02:00
|
|
|
const { keys, sortBy, omit } = require('lodash/fp');
|
2022-04-12 16:32:05 +02:00
|
|
|
const { joinBy } = require('@strapi/utils');
|
2022-03-22 18:19:46 +01:00
|
|
|
|
|
|
|
const folderModel = 'plugin::upload.folder';
|
|
|
|
|
|
|
|
const generateUID = () => uuid();
|
|
|
|
|
2022-04-12 16:32:05 +02:00
|
|
|
const setPathAndUID = async folder => {
|
2022-04-05 17:36:09 +02:00
|
|
|
const uid = generateUID();
|
2022-04-12 16:32:05 +02:00
|
|
|
let parentPath = '/';
|
2022-03-22 18:19:46 +01:00
|
|
|
if (folder.parent) {
|
|
|
|
const parentFolder = await strapi.entityService.findOne(folderModel, folder.parent);
|
2022-04-12 16:32:05 +02:00
|
|
|
parentPath = parentFolder.path;
|
2022-03-22 18:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(folder, {
|
2022-04-05 17:36:09 +02:00
|
|
|
uid,
|
2022-04-12 16:32:05 +02:00
|
|
|
path: joinBy('/', parentPath, uid),
|
2022-03-22 18:19:46 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-04-04 14:32:08 +02:00
|
|
|
const deleteByIds = async ids => {
|
|
|
|
const deletedFolders = [];
|
|
|
|
for (const id of ids) {
|
|
|
|
const deletedFolder = await strapi.entityService.delete(folderModel, id);
|
|
|
|
|
|
|
|
deletedFolders.push(deletedFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return deletedFolders;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a folder exists in database
|
|
|
|
* @param params query params to find the folder
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
const exists = async (params = {}) => {
|
|
|
|
const count = await strapi.query(folderModel).count({ where: params });
|
|
|
|
return count > 0;
|
|
|
|
};
|
|
|
|
|
2022-04-13 12:52:44 +02:00
|
|
|
const getStructure = async () => {
|
2022-04-07 18:00:21 +02:00
|
|
|
const joinTable = strapi.db.metadata.get('plugin::upload.folder').attributes.parent.joinTable;
|
|
|
|
const qb = strapi.db.queryBuilder('plugin::upload.folder');
|
|
|
|
const alias = qb.getAlias();
|
|
|
|
const folders = await qb
|
|
|
|
.select(['id', 'name', `${alias}.${joinTable.inverseJoinColumn.name} as parent`])
|
|
|
|
.join({
|
|
|
|
alias,
|
|
|
|
referencedTable: joinTable.name,
|
|
|
|
referencedColumn: joinTable.joinColumn.name,
|
|
|
|
rootColumn: joinTable.joinColumn.referencedColumn,
|
|
|
|
rootTable: qb.alias,
|
|
|
|
})
|
|
|
|
.execute({ mapResults: false });
|
|
|
|
|
|
|
|
const folderMap = folders.reduce((map, f) => {
|
|
|
|
f.children = [];
|
|
|
|
map[f.id] = f;
|
|
|
|
return map;
|
|
|
|
}, {});
|
|
|
|
folderMap.null = { children: [] };
|
|
|
|
|
|
|
|
for (const id of keys(omit('null', folderMap))) {
|
|
|
|
const parentId = folderMap[id].parent;
|
|
|
|
folderMap[parentId].children.push(folderMap[id]);
|
|
|
|
folderMap[parentId].children = sortBy('name', folderMap[parentId].children);
|
|
|
|
delete folderMap[id].parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return folderMap.null.children;
|
|
|
|
};
|
|
|
|
|
2022-03-22 18:19:46 +01:00
|
|
|
module.exports = {
|
2022-04-04 14:32:08 +02:00
|
|
|
exists,
|
|
|
|
deleteByIds,
|
2022-04-12 16:32:05 +02:00
|
|
|
setPathAndUID,
|
2022-04-13 12:52:44 +02:00
|
|
|
getStructure,
|
2022-03-22 18:19:46 +01:00
|
|
|
};
|