Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/graph/resolvers/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ module.exports = {
const path = parentPath ? `${parentPath}/${r.slug}` : r.slug
return WIKI.auth.checkAccess(context.req.user, ['read:assets'], { path })
})
},
async folderByPath(obj, args, context) {
const path = args.path.toLowerCase()
if (!WIKI.auth.checkAccess(context.req.user, ['read:assets'], { path })) {
return null
}
return WIKI.models.assetFolders.getFolderByPath(path)
}
},
AssetMutation: {
Expand Down
2 changes: 2 additions & 0 deletions server/graph/schemas/asset.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type AssetQuery {
folders(
parentFolderId: Int!
): [AssetFolder] @auth(requires: ["manage:system", "read:assets"])

folderByPath(path: String!): AssetFolder @auth(requires: ["manage:system", "read:assets"])
}

# -----------------------------------------------
Expand Down
28 changes: 28 additions & 0 deletions server/models/assetFolders.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,32 @@ module.exports = class AssetFolder extends Model {
})
return folders
}

/**
* Get asset folder by resolving its path from root
* @param {string} path Folder path, e.g. 'abc/def/ghi'
*/
static async getFolderByPath(path) {
const segments = String(path).split('/').filter(Boolean)
if (segments.length === 0) return null
const db = this.knex()
const targetPath = segments.join('/')
let results
if (WIKI.config.db.type === 'mssql') {
results = await db
.with('folder_path', (qb) => {
qb.select('af.*').select(db.raw('CAST(af.slug AS VARCHAR(MAX)) AS current_path')).from('assetFolders as af').whereNull('af.parentId').unionAll((uqb) => {
uqb.select('af.*').select(db.raw("CAST(fp.current_path + '/' + af.slug AS VARCHAR(MAX)) AS current_path")).from('assetFolders as af').join('folder_path as fp', 'af.parentId', 'fp.id')
})
}).select('*').from('folder_path').where('current_path', targetPath).limit(1)
} else {
results = await db
.withRecursive('folder_path', (qb) => {
qb.select('af.*').select(db.raw('af.slug::TEXT AS current_path')).from('assetFolders as af').whereNull('af.parentId').unionAll((uqb) => {
uqb.select('af.*').select(db.raw("fp.current_path || '/' || af.slug AS current_path")).from('assetFolders as af').join('folder_path as fp', 'af.parentId', 'fp.id')
}, true)
}).select('*').from('folder_path').where('current_path', targetPath).limit(1)
}
return results[0] || null
}
}