|
|
@@ -175,6 +175,7 @@ module.exports = (crowi) => {
|
|
|
body('isSlackEnabled').if(value => value != null).isBoolean().withMessage('isSlackEnabled must be boolean'),
|
|
|
body('slackChannels').if(value => value != null).isString().withMessage('slackChannels must be string'),
|
|
|
body('pageTags').if(value => value != null).isArray().withMessage('pageTags must be array'),
|
|
|
+ body('shouldGeneratePath').optional().isBoolean().withMessage('shouldGeneratePath is must be boolean or undefined'),
|
|
|
],
|
|
|
renamePage: [
|
|
|
body('pageId').isMongoId().withMessage('pageId is required'),
|
|
|
@@ -238,6 +239,17 @@ module.exports = (crowi) => {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
+ async function generateUniquePath(basePath, index = 1) {
|
|
|
+ const Page = mongoose.model('Page');
|
|
|
+ const path = basePath + index;
|
|
|
+ const response = await Page.findByPath(path);
|
|
|
+ const isPathExists = response != null;
|
|
|
+ if (isPathExists) {
|
|
|
+ return generateUniquePath(basePath, index + 1);
|
|
|
+ }
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @swagger
|
|
|
*
|
|
|
@@ -266,9 +278,9 @@ module.exports = (crowi) => {
|
|
|
* type: array
|
|
|
* items:
|
|
|
* $ref: '#/components/schemas/Tag'
|
|
|
- * createFromPageTree:
|
|
|
+ * shouldGeneratePath:
|
|
|
* type: boolean
|
|
|
- * description: Whether the page was created from the page tree or not
|
|
|
+ * description: Determine whether a new path should be generated
|
|
|
* required:
|
|
|
* - body
|
|
|
* - path
|
|
|
@@ -295,7 +307,7 @@ module.exports = (crowi) => {
|
|
|
*/
|
|
|
router.post('/', accessTokenParser, loginRequiredStrictly, excludeReadOnlyUser, addActivity, validator.createPage, apiV3FormValidator, async(req, res) => {
|
|
|
const {
|
|
|
- body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags,
|
|
|
+ body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
|
|
|
} = req.body;
|
|
|
|
|
|
let { path } = req.body;
|
|
|
@@ -303,6 +315,22 @@ module.exports = (crowi) => {
|
|
|
// check whether path starts slash
|
|
|
path = addHeadingSlash(path);
|
|
|
|
|
|
+ if (shouldGeneratePath) {
|
|
|
+ try {
|
|
|
+ const rootPath = '/';
|
|
|
+ const defaultTitle = '/Untitled';
|
|
|
+ const basePath = path === rootPath ? defaultTitle : path + defaultTitle;
|
|
|
+ path = await generateUniquePath(basePath);
|
|
|
+
|
|
|
+ if (!isCreatablePage(path)) {
|
|
|
+ path = await generateUniquePath(defaultTitle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ return res.apiv3Err(new ErrorV3('Failed to generate unique path'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (!isCreatablePage(path)) {
|
|
|
return res.apiv3Err(`Could not use the path '${path}'`);
|
|
|
}
|