mirror of
https://github.com/strapi/strapi.git
synced 2025-11-19 11:41:49 +00:00
chore: create util to check if folder is the same as another one or a child one
This commit is contained in:
parent
a7b8ad0fd5
commit
087ff3765b
8
packages/core/upload/server/controllers/utils/folders.js
Normal file
8
packages/core/upload/server/controllers/utils/folders.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const isFolderOrChild = (folderOrChild, folder) =>
|
||||||
|
folderOrChild.path === folder.path || folderOrChild.path.startsWith(`${folder.path}/`);
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
isFolderOrChild,
|
||||||
|
};
|
||||||
@ -4,6 +4,7 @@ const { intersection, map, isEmpty } = require('lodash/fp');
|
|||||||
const { yup, validateYupSchema } = require('@strapi/utils');
|
const { yup, validateYupSchema } = require('@strapi/utils');
|
||||||
const { FOLDER_MODEL_UID } = require('../../../constants');
|
const { FOLDER_MODEL_UID } = require('../../../constants');
|
||||||
const { folderExists } = require('./utils');
|
const { folderExists } = require('./utils');
|
||||||
|
const { isFolderOrChild } = require('../../utils/folders');
|
||||||
|
|
||||||
const validateDeleteManyFoldersFilesSchema = yup
|
const validateDeleteManyFoldersFilesSchema = yup
|
||||||
.object()
|
.object()
|
||||||
@ -75,11 +76,7 @@ const validateMoveFoldersNotInsideThemselvesSchema = yup
|
|||||||
});
|
});
|
||||||
|
|
||||||
const unmovableFoldersNames = folders
|
const unmovableFoldersNames = folders
|
||||||
.filter(
|
.filter((folder) => isFolderOrChild(destinationFolder, folder))
|
||||||
(folder) =>
|
|
||||||
destinationFolder.path === folder.path ||
|
|
||||||
destinationFolder.path.startsWith(`${folder.path}/`)
|
|
||||||
)
|
|
||||||
.map((f) => f.name);
|
.map((f) => f.name);
|
||||||
if (unmovableFoldersNames.length > 0) {
|
if (unmovableFoldersNames.length > 0) {
|
||||||
return this.createError({
|
return this.createError({
|
||||||
|
|||||||
@ -5,6 +5,7 @@ const { yup, validateYupSchema } = require('@strapi/utils');
|
|||||||
const { getService } = require('../../../utils');
|
const { getService } = require('../../../utils');
|
||||||
const { FOLDER_MODEL_UID } = require('../../../constants');
|
const { FOLDER_MODEL_UID } = require('../../../constants');
|
||||||
const { folderExists } = require('./utils');
|
const { folderExists } = require('./utils');
|
||||||
|
const { isFolderOrChild } = require('../../utils/folders');
|
||||||
|
|
||||||
const NO_SLASH_REGEX = /^[^/]+$/;
|
const NO_SLASH_REGEX = /^[^/]+$/;
|
||||||
const NO_SPACES_AROUND = /^(?! ).+(?<! )$/;
|
const NO_SPACES_AROUND = /^(?! ).+(?<! )$/;
|
||||||
@ -78,11 +79,7 @@ const validateUpdateFolderSchema = (id) =>
|
|||||||
|
|
||||||
if (!destinationFolder || !currentFolder) return true;
|
if (!destinationFolder || !currentFolder) return true;
|
||||||
|
|
||||||
const isUnMovable =
|
return !isFolderOrChild(destinationFolder, currentFolder);
|
||||||
destinationFolder.path === currentFolder.path ||
|
|
||||||
destinationFolder.path.startsWith(`${currentFolder.path}/`);
|
|
||||||
|
|
||||||
return !isUnMovable;
|
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user