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-26 11:23:07 +02:00
|
|
|
const { keys, sortBy, omit, map } = require('lodash/fp');
|
2022-04-12 16:32:05 +02:00
|
|
|
const { joinBy } = require('@strapi/utils');
|
2022-04-26 11:23:07 +02:00
|
|
|
const { getService } = require('../utils');
|
2022-03-22 18:19:46 +01:00
|
|
|
|
|
|
|
const folderModel = 'plugin::upload.folder';
|
2022-04-26 11:23:07 +02:00
|
|
|
const fileModel = 'plugin::upload.file';
|
2022-03-22 18:19:46 +01:00
|
|
|
|
|
|
|
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-26 11:23:07 +02:00
|
|
|
/**
|
|
|
|
* Recursively delete folders and included files
|
|
|
|
* @param ids ids of the folders to delete
|
|
|
|
* @returns {Promise<Object[]>}
|
|
|
|
*/
|
|
|
|
const deleteByIds = async (ids = []) => {
|
|
|
|
const folders = await strapi.db.query(folderModel).findMany({ where: { id: { $in: ids } } });
|
|
|
|
if (folders.length === 0) {
|
|
|
|
return [];
|
2022-04-04 14:32:08 +02:00
|
|
|
}
|
|
|
|
|
2022-04-26 11:23:07 +02:00
|
|
|
const pathsToDelete = map('path', folders);
|
|
|
|
|
|
|
|
// delete files
|
|
|
|
const filesToDelete = await strapi.db.query(fileModel).findMany({
|
|
|
|
where: {
|
|
|
|
$or: pathsToDelete.map(path => ({ folderPath: { $startsWith: path } })),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(filesToDelete.map(file => getService('upload').remove(file)));
|
|
|
|
|
|
|
|
// delete folders
|
|
|
|
await strapi.db.query(folderModel).deleteMany({
|
|
|
|
where: {
|
|
|
|
$or: pathsToDelete.map(path => ({ path: { $startsWith: path } })),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return folders;
|
2022-04-04 14:32:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
};
|