|
|
@ -17,7 +17,7 @@ const reTitle = /^[^<>"]+$/
|
|
|
|
* Tree model
|
|
|
|
* Tree model
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export class Tree extends Model {
|
|
|
|
export class Tree extends Model {
|
|
|
|
static get tableName() { return 'tree' }
|
|
|
|
static get tableName () { return 'tree' }
|
|
|
|
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
@ -25,22 +25,22 @@ export class Tree extends Model {
|
|
|
|
required: ['fileName'],
|
|
|
|
required: ['fileName'],
|
|
|
|
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
properties: {
|
|
|
|
id: {type: 'string'},
|
|
|
|
id: { type: 'string' },
|
|
|
|
folderPath: {type: 'string'},
|
|
|
|
folderPath: { type: 'string' },
|
|
|
|
fileName: {type: 'string'},
|
|
|
|
fileName: { type: 'string' },
|
|
|
|
type: {type: 'string'},
|
|
|
|
type: { type: 'string' },
|
|
|
|
title: {type: 'string'},
|
|
|
|
title: { type: 'string' },
|
|
|
|
createdAt: {type: 'string'},
|
|
|
|
createdAt: { type: 'string' },
|
|
|
|
updatedAt: {type: 'string'}
|
|
|
|
updatedAt: { type: 'string' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static get jsonAttributes() {
|
|
|
|
static get jsonAttributes () {
|
|
|
|
return ['meta']
|
|
|
|
return ['meta']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static get relationMappings() {
|
|
|
|
static get relationMappings () {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
site: {
|
|
|
|
site: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
@ -53,10 +53,11 @@ export class Tree extends Model {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$beforeUpdate() {
|
|
|
|
$beforeUpdate () {
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$beforeInsert() {
|
|
|
|
|
|
|
|
|
|
|
|
$beforeInsert () {
|
|
|
|
this.createdAt = new Date().toISOString()
|
|
|
|
this.createdAt = new Date().toISOString()
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -90,7 +91,7 @@ export class Tree extends Model {
|
|
|
|
const parent = await WIKI.db.knex('tree').where({
|
|
|
|
const parent = await WIKI.db.knex('tree').where({
|
|
|
|
...parentFilter,
|
|
|
|
...parentFilter,
|
|
|
|
type: 'folder',
|
|
|
|
type: 'folder',
|
|
|
|
locale: locale,
|
|
|
|
locale,
|
|
|
|
siteId
|
|
|
|
siteId
|
|
|
|
}).first()
|
|
|
|
}).first()
|
|
|
|
if (parent) {
|
|
|
|
if (parent) {
|
|
|
@ -123,16 +124,18 @@ export class Tree extends Model {
|
|
|
|
* @param {Object} [args.meta] - Extra metadata
|
|
|
|
* @param {Object} [args.meta] - Extra metadata
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static async addPage ({ id, parentId, parentPath, fileName, title, locale, siteId, tags = [], meta = {} }) {
|
|
|
|
static async addPage ({ id, parentId, parentPath, fileName, title, locale, siteId, tags = [], meta = {} }) {
|
|
|
|
const folder = (parentId || parentPath) ? await WIKI.db.tree.getFolder({
|
|
|
|
const folder = (parentId || parentPath)
|
|
|
|
id: parentId,
|
|
|
|
? await WIKI.db.tree.getFolder({
|
|
|
|
path: parentPath,
|
|
|
|
id: parentId,
|
|
|
|
locale,
|
|
|
|
path: parentPath,
|
|
|
|
siteId,
|
|
|
|
locale,
|
|
|
|
createIfMissing: true
|
|
|
|
siteId,
|
|
|
|
}) : {
|
|
|
|
createIfMissing: true
|
|
|
|
folderPath: '',
|
|
|
|
})
|
|
|
|
fileName: ''
|
|
|
|
: {
|
|
|
|
}
|
|
|
|
folderPath: '',
|
|
|
|
|
|
|
|
fileName: ''
|
|
|
|
|
|
|
|
}
|
|
|
|
const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
|
|
|
|
const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
|
|
|
|
const fullPath = folderPath ? `${folderPath}/${fileName}` : fileName
|
|
|
|
const fullPath = folderPath ? `${folderPath}/${fileName}` : fileName
|
|
|
|
|
|
|
|
|
|
|
@ -143,9 +146,9 @@ export class Tree extends Model {
|
|
|
|
folderPath: encodeFolderPath(folderPath),
|
|
|
|
folderPath: encodeFolderPath(folderPath),
|
|
|
|
fileName,
|
|
|
|
fileName,
|
|
|
|
type: 'page',
|
|
|
|
type: 'page',
|
|
|
|
title: title,
|
|
|
|
title,
|
|
|
|
hash: generateHash(fullPath),
|
|
|
|
hash: generateHash(fullPath),
|
|
|
|
locale: locale,
|
|
|
|
locale,
|
|
|
|
siteId,
|
|
|
|
siteId,
|
|
|
|
tags,
|
|
|
|
tags,
|
|
|
|
meta,
|
|
|
|
meta,
|
|
|
@ -169,16 +172,18 @@ export class Tree extends Model {
|
|
|
|
* @param {Object} [args.meta] - Extra metadata
|
|
|
|
* @param {Object} [args.meta] - Extra metadata
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static async addAsset ({ id, parentId, parentPath, fileName, title, locale, siteId, tags = [], meta = {} }) {
|
|
|
|
static async addAsset ({ id, parentId, parentPath, fileName, title, locale, siteId, tags = [], meta = {} }) {
|
|
|
|
const folder = (parentId || parentPath) ? await WIKI.db.tree.getFolder({
|
|
|
|
const folder = (parentId || parentPath)
|
|
|
|
id: parentId,
|
|
|
|
? await WIKI.db.tree.getFolder({
|
|
|
|
path: parentPath,
|
|
|
|
id: parentId,
|
|
|
|
locale,
|
|
|
|
path: parentPath,
|
|
|
|
siteId,
|
|
|
|
locale,
|
|
|
|
createIfMissing: true
|
|
|
|
siteId,
|
|
|
|
}) : {
|
|
|
|
createIfMissing: true
|
|
|
|
folderPath: '',
|
|
|
|
})
|
|
|
|
fileName: ''
|
|
|
|
: {
|
|
|
|
}
|
|
|
|
folderPath: '',
|
|
|
|
|
|
|
|
fileName: ''
|
|
|
|
|
|
|
|
}
|
|
|
|
const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
|
|
|
|
const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
|
|
|
|
const fullPath = folderPath ? `${folderPath}/${fileName}` : fileName
|
|
|
|
const fullPath = folderPath ? `${folderPath}/${fileName}` : fileName
|
|
|
|
|
|
|
|
|
|
|
@ -189,9 +194,9 @@ export class Tree extends Model {
|
|
|
|
folderPath: encodeFolderPath(folderPath),
|
|
|
|
folderPath: encodeFolderPath(folderPath),
|
|
|
|
fileName,
|
|
|
|
fileName,
|
|
|
|
type: 'asset',
|
|
|
|
type: 'asset',
|
|
|
|
title: title,
|
|
|
|
title,
|
|
|
|
hash: generateHash(fullPath),
|
|
|
|
hash: generateHash(fullPath),
|
|
|
|
locale: locale,
|
|
|
|
locale,
|
|
|
|
siteId,
|
|
|
|
siteId,
|
|
|
|
tags,
|
|
|
|
tags,
|
|
|
|
meta
|
|
|
|
meta
|
|
|
@ -246,8 +251,8 @@ export class Tree extends Model {
|
|
|
|
|
|
|
|
|
|
|
|
// Check for collision
|
|
|
|
// Check for collision
|
|
|
|
const existingFolder = await WIKI.db.knex('tree').select('id').where({
|
|
|
|
const existingFolder = await WIKI.db.knex('tree').select('id').where({
|
|
|
|
siteId: siteId,
|
|
|
|
siteId,
|
|
|
|
locale: locale,
|
|
|
|
locale,
|
|
|
|
folderPath: encodeFolderPath(parentPath),
|
|
|
|
folderPath: encodeFolderPath(parentPath),
|
|
|
|
fileName: pathName,
|
|
|
|
fileName: pathName,
|
|
|
|
type: 'folder'
|
|
|
|
type: 'folder'
|
|
|
@ -281,8 +286,8 @@ export class Tree extends Model {
|
|
|
|
type: 'folder',
|
|
|
|
type: 'folder',
|
|
|
|
title: ancestor.fileName,
|
|
|
|
title: ancestor.fileName,
|
|
|
|
hash: generateHash(newAncestorFullPath),
|
|
|
|
hash: generateHash(newAncestorFullPath),
|
|
|
|
locale: locale,
|
|
|
|
locale,
|
|
|
|
siteId: siteId,
|
|
|
|
siteId,
|
|
|
|
meta: {
|
|
|
|
meta: {
|
|
|
|
children: 1
|
|
|
|
children: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -301,10 +306,10 @@ export class Tree extends Model {
|
|
|
|
folderPath: encodeFolderPath(parentPath),
|
|
|
|
folderPath: encodeFolderPath(parentPath),
|
|
|
|
fileName: pathName,
|
|
|
|
fileName: pathName,
|
|
|
|
type: 'folder',
|
|
|
|
type: 'folder',
|
|
|
|
title: title,
|
|
|
|
title,
|
|
|
|
hash: generateHash(fullPath),
|
|
|
|
hash: generateHash(fullPath),
|
|
|
|
locale: locale,
|
|
|
|
locale,
|
|
|
|
siteId: siteId,
|
|
|
|
siteId,
|
|
|
|
meta: {
|
|
|
|
meta: {
|
|
|
|
children: 0
|
|
|
|
children: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -383,13 +388,13 @@ export class Tree extends Model {
|
|
|
|
const fullPath = folder.folderPath ? `${decodeFolderPath(folder.folderPath)}/${pathName}` : pathName
|
|
|
|
const fullPath = folder.folderPath ? `${decodeFolderPath(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,
|
|
|
|
title,
|
|
|
|
hash: generateHash(fullPath)
|
|
|
|
hash: generateHash(fullPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Update the folder title only
|
|
|
|
// Update the folder title only
|
|
|
|
await WIKI.db.knex('tree').where('id', folder.id).update({
|
|
|
|
await WIKI.db.knex('tree').where('id', folder.id).update({
|
|
|
|
title: title
|
|
|
|
title
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|