|
|
@ -1,9 +1,7 @@
|
|
|
|
import { Model } from 'objection'
|
|
|
|
import { Model } from 'objection'
|
|
|
|
import { differenceWith, dropRight, last, nth } from 'lodash-es'
|
|
|
|
import { differenceWith, dropRight, last, nth } from 'lodash-es'
|
|
|
|
import {
|
|
|
|
import {
|
|
|
|
decodeFolderPath,
|
|
|
|
|
|
|
|
decodeTreePath,
|
|
|
|
decodeTreePath,
|
|
|
|
encodeFolderPath,
|
|
|
|
|
|
|
|
encodeTreePath,
|
|
|
|
encodeTreePath,
|
|
|
|
generateHash
|
|
|
|
generateHash
|
|
|
|
} from '../helpers/common.mjs'
|
|
|
|
} from '../helpers/common.mjs'
|
|
|
@ -85,7 +83,7 @@ export class Tree extends Model {
|
|
|
|
const parentPath = encodeTreePath(path)
|
|
|
|
const parentPath = encodeTreePath(path)
|
|
|
|
const parentPathParts = parentPath.split('.')
|
|
|
|
const parentPathParts = parentPath.split('.')
|
|
|
|
const parentFilter = {
|
|
|
|
const parentFilter = {
|
|
|
|
folderPath: encodeFolderPath(dropRight(parentPathParts).join('.')),
|
|
|
|
folderPath: dropRight(parentPathParts).join('.'),
|
|
|
|
fileName: last(parentPathParts)
|
|
|
|
fileName: last(parentPathParts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const parent = await WIKI.db.knex('tree').where({
|
|
|
|
const parent = await WIKI.db.knex('tree').where({
|
|
|
@ -143,7 +141,7 @@ export class Tree extends Model {
|
|
|
|
|
|
|
|
|
|
|
|
const pageEntry = await WIKI.db.knex('tree').insert({
|
|
|
|
const pageEntry = await WIKI.db.knex('tree').insert({
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
folderPath: encodeFolderPath(folderPath),
|
|
|
|
folderPath,
|
|
|
|
fileName,
|
|
|
|
fileName,
|
|
|
|
type: 'page',
|
|
|
|
type: 'page',
|
|
|
|
title,
|
|
|
|
title,
|
|
|
@ -191,7 +189,7 @@ export class Tree extends Model {
|
|
|
|
|
|
|
|
|
|
|
|
const assetEntry = await WIKI.db.knex('tree').insert({
|
|
|
|
const assetEntry = await WIKI.db.knex('tree').insert({
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
folderPath: encodeFolderPath(folderPath),
|
|
|
|
folderPath,
|
|
|
|
fileName,
|
|
|
|
fileName,
|
|
|
|
type: 'asset',
|
|
|
|
type: 'asset',
|
|
|
|
title,
|
|
|
|
title,
|
|
|
@ -231,7 +229,7 @@ export class Tree extends Model {
|
|
|
|
WIKI.logger.debug(`Creating new folder ${pathName}...`)
|
|
|
|
WIKI.logger.debug(`Creating new folder ${pathName}...`)
|
|
|
|
const parentPathParts = parentPath.split('.')
|
|
|
|
const parentPathParts = parentPath.split('.')
|
|
|
|
const parentFilter = {
|
|
|
|
const parentFilter = {
|
|
|
|
folderPath: encodeFolderPath(dropRight(parentPathParts).join('.')),
|
|
|
|
folderPath: dropRight(parentPathParts).join('.'),
|
|
|
|
fileName: last(parentPathParts)
|
|
|
|
fileName: last(parentPathParts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -242,7 +240,7 @@ export class Tree extends Model {
|
|
|
|
if (!parent) {
|
|
|
|
if (!parent) {
|
|
|
|
throw new Error('ERR_FOLDER_PARENT_INVALID')
|
|
|
|
throw new Error('ERR_FOLDER_PARENT_INVALID')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parentPath = parent.folderPath ? `${decodeFolderPath(parent.folderPath)}.${parent.fileName}` : parent.fileName
|
|
|
|
parentPath = parent.folderPath ? `${parent.folderPath}.${parent.fileName}` : parent.fileName
|
|
|
|
} else if (parentPath) {
|
|
|
|
} else if (parentPath) {
|
|
|
|
parent = await WIKI.db.knex('tree').where(parentFilter).first()
|
|
|
|
parent = await WIKI.db.knex('tree').where(parentFilter).first()
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -253,7 +251,7 @@ export class Tree extends Model {
|
|
|
|
const existingFolder = await WIKI.db.knex('tree').select('id').where({
|
|
|
|
const existingFolder = await WIKI.db.knex('tree').select('id').where({
|
|
|
|
siteId,
|
|
|
|
siteId,
|
|
|
|
locale,
|
|
|
|
locale,
|
|
|
|
folderPath: encodeFolderPath(parentPath),
|
|
|
|
folderPath: parentPath,
|
|
|
|
fileName: pathName,
|
|
|
|
fileName: pathName,
|
|
|
|
type: 'folder'
|
|
|
|
type: 'folder'
|
|
|
|
}).first()
|
|
|
|
}).first()
|
|
|
@ -268,7 +266,7 @@ export class Tree extends Model {
|
|
|
|
const parentPathParts = parentPath.split('.')
|
|
|
|
const parentPathParts = parentPath.split('.')
|
|
|
|
for (let i = 1; i <= parentPathParts.length; i++) {
|
|
|
|
for (let i = 1; i <= parentPathParts.length; i++) {
|
|
|
|
const ancestor = {
|
|
|
|
const ancestor = {
|
|
|
|
folderPath: encodeFolderPath(dropRight(parentPathParts, i).join('.')),
|
|
|
|
folderPath: dropRight(parentPathParts, i).join('.'),
|
|
|
|
fileName: nth(parentPathParts, i * -1)
|
|
|
|
fileName: nth(parentPathParts, i * -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectedAncestors.push(ancestor)
|
|
|
|
expectedAncestors.push(ancestor)
|
|
|
@ -303,7 +301,7 @@ export class Tree extends Model {
|
|
|
|
// Create folder
|
|
|
|
// Create folder
|
|
|
|
const fullPath = parentPath ? `${decodeTreePath(parentPath)}/${pathName}` : pathName
|
|
|
|
const fullPath = parentPath ? `${decodeTreePath(parentPath)}/${pathName}` : pathName
|
|
|
|
const folder = await WIKI.db.knex('tree').insert({
|
|
|
|
const folder = await WIKI.db.knex('tree').insert({
|
|
|
|
folderPath: encodeFolderPath(parentPath),
|
|
|
|
folderPath: parentPath,
|
|
|
|
fileName: pathName,
|
|
|
|
fileName: pathName,
|
|
|
|
type: 'folder',
|
|
|
|
type: 'folder',
|
|
|
|
title,
|
|
|
|
title,
|
|
|
@ -372,8 +370,8 @@ export class Tree extends Model {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build new paths
|
|
|
|
// Build new paths
|
|
|
|
const oldFolderPath = encodeFolderPath(folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName)
|
|
|
|
const oldFolderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
|
|
|
|
const newFolderPath = encodeFolderPath(folder.folderPath ? `${folder.folderPath}.${pathName}` : pathName)
|
|
|
|
const newFolderPath = folder.folderPath ? `${folder.folderPath}.${pathName}` : pathName
|
|
|
|
|
|
|
|
|
|
|
|
// Update children nodes
|
|
|
|
// Update children nodes
|
|
|
|
WIKI.logger.debug(`Updating parent path of children nodes from ${oldFolderPath} to ${newFolderPath} ...`)
|
|
|
|
WIKI.logger.debug(`Updating parent path of children nodes from ${oldFolderPath} to ${newFolderPath} ...`)
|
|
|
@ -385,7 +383,7 @@ export class Tree extends Model {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Rename the folder itself
|
|
|
|
// Rename the folder itself
|
|
|
|
const fullPath = folder.folderPath ? `${decodeFolderPath(folder.folderPath)}/${pathName}` : pathName
|
|
|
|
const fullPath = folder.folderPath ? `${folder.folderPath}/${pathName}` : pathName
|
|
|
|
await WIKI.db.knex('tree').where('id', folder.id).update({
|
|
|
|
await WIKI.db.knex('tree').where('id', folder.id).update({
|
|
|
|
fileName: pathName,
|
|
|
|
fileName: pathName,
|
|
|
|
title,
|
|
|
|
title,
|
|
|
@ -416,7 +414,7 @@ export class Tree extends Model {
|
|
|
|
WIKI.logger.debug(`Deleting folder ${folder.id} at path ${folderPath}...`)
|
|
|
|
WIKI.logger.debug(`Deleting folder ${folder.id} at path ${folderPath}...`)
|
|
|
|
|
|
|
|
|
|
|
|
// Delete all children
|
|
|
|
// Delete all children
|
|
|
|
const deletedNodes = await WIKI.db.knex('tree').where('folderPath', '<@', encodeFolderPath(folderPath)).del().returning(['id', 'type'])
|
|
|
|
const deletedNodes = await WIKI.db.knex('tree').where('folderPath', '<@', folderPath).del().returning(['id', 'type'])
|
|
|
|
|
|
|
|
|
|
|
|
// Delete folders
|
|
|
|
// Delete folders
|
|
|
|
const deletedFolders = deletedNodes.filter(n => n.type === 'folder').map(n => n.id)
|
|
|
|
const deletedFolders = deletedNodes.filter(n => n.type === 'folder').map(n => n.id)
|
|
|
@ -447,7 +445,7 @@ export class Tree extends Model {
|
|
|
|
if (folder.folderPath) {
|
|
|
|
if (folder.folderPath) {
|
|
|
|
const parentPathParts = folder.folderPath.split('.')
|
|
|
|
const parentPathParts = folder.folderPath.split('.')
|
|
|
|
const parent = await WIKI.db.knex('tree').where({
|
|
|
|
const parent = await WIKI.db.knex('tree').where({
|
|
|
|
folderPath: encodeFolderPath(dropRight(parentPathParts).join('.')),
|
|
|
|
folderPath: dropRight(parentPathParts).join('.'),
|
|
|
|
fileName: last(parentPathParts)
|
|
|
|
fileName: last(parentPathParts)
|
|
|
|
}).first()
|
|
|
|
}).first()
|
|
|
|
await WIKI.db.knex('tree').where('id', parent.id).update({
|
|
|
|
await WIKI.db.knex('tree').where('id', parent.id).update({
|
|
|
|