2019-05-26 01:38:29 +00:00
|
|
|
const _ = require('lodash')
|
2019-05-21 20:45:27 +00:00
|
|
|
const sanitize = require('sanitize-filename')
|
|
|
|
const graphHelper = require('../../helpers/graph')
|
2019-05-26 01:38:29 +00:00
|
|
|
const assetHelper = require('../../helpers/asset')
|
2019-05-13 05:15:27 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Query: {
|
|
|
|
async assets() { return {} }
|
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
async assets() { return {} }
|
|
|
|
},
|
|
|
|
AssetQuery: {
|
|
|
|
async list(obj, args, context) {
|
2019-05-18 06:27:30 +00:00
|
|
|
let cond = {
|
2019-05-21 20:45:27 +00:00
|
|
|
folderId: args.folderId === 0 ? null : args.folderId
|
2019-05-18 06:27:30 +00:00
|
|
|
}
|
|
|
|
if (args.kind !== 'ALL') {
|
|
|
|
cond.kind = args.kind.toLowerCase()
|
|
|
|
}
|
|
|
|
const result = await WIKI.models.assets.query().where(cond)
|
2019-05-13 05:15:27 +00:00
|
|
|
return result.map(a => ({
|
|
|
|
...a,
|
|
|
|
kind: a.kind.toUpperCase()
|
|
|
|
}))
|
2019-05-21 20:45:27 +00:00
|
|
|
},
|
|
|
|
async folders(obj, args, context) {
|
|
|
|
const result = await WIKI.models.assetFolders.query().where({
|
|
|
|
parentId: args.parentFolderId === 0 ? null : args.parentFolderId
|
|
|
|
})
|
|
|
|
// TODO: Filter by page rules
|
|
|
|
return result
|
2019-05-13 05:15:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
AssetMutation: {
|
2019-05-26 01:38:29 +00:00
|
|
|
/**
|
|
|
|
* Create New Asset Folder
|
|
|
|
*/
|
2019-05-21 20:45:27 +00:00
|
|
|
async createFolder(obj, args, context) {
|
|
|
|
try {
|
|
|
|
const folderSlug = sanitize(args.slug).toLowerCase()
|
|
|
|
const parentFolderId = args.parentFolderId === 0 ? null : args.parentFolderId
|
2019-06-03 02:18:11 +00:00
|
|
|
const result = await WIKI.models.assetFolders.query().where({
|
2019-05-21 20:45:27 +00:00
|
|
|
parentId: parentFolderId,
|
|
|
|
slug: folderSlug
|
|
|
|
}).first()
|
|
|
|
if (!result) {
|
|
|
|
await WIKI.models.assetFolders.query().insert({
|
|
|
|
slug: folderSlug,
|
|
|
|
name: folderSlug,
|
|
|
|
parentId: parentFolderId
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Asset Folder has been created successfully.')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WIKI.Error.AssetFolderExists()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-05-26 01:38:29 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Rename an Asset
|
|
|
|
*/
|
|
|
|
async renameAsset(obj, args, context) {
|
|
|
|
try {
|
|
|
|
const filename = sanitize(args.filename).toLowerCase()
|
|
|
|
|
|
|
|
const asset = await WIKI.models.assets.query().findById(args.id)
|
|
|
|
if (asset) {
|
|
|
|
// Check for extension mismatch
|
|
|
|
if (!_.endsWith(filename, asset.ext)) {
|
|
|
|
throw new WIKI.Error.AssetRenameInvalidExt()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for non-dot files changing to dotfile
|
|
|
|
if (asset.ext.length > 0 && filename.length - asset.ext.length < 1) {
|
|
|
|
throw new WIKI.Error.AssetRenameInvalid()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for collision
|
|
|
|
const assetCollision = await WIKI.models.assets.query().where({
|
|
|
|
filename,
|
|
|
|
folderId: asset.folderId
|
|
|
|
}).first()
|
|
|
|
if (assetCollision) {
|
|
|
|
throw new WIKI.Error.AssetRenameCollision()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get asset folder path
|
|
|
|
let hierarchy = []
|
|
|
|
if (asset.folderId) {
|
|
|
|
hierarchy = await WIKI.models.assetFolders.getHierarchy(asset.folderId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check source asset permissions
|
|
|
|
const assetSourcePath = (asset.folderId) ? hierarchy.map(h => h.slug).join('/') + `/${filename}` : filename
|
|
|
|
if (!WIKI.auth.checkAccess(context.req.user, ['manage:assets'], { path: assetSourcePath })) {
|
|
|
|
throw new WIKI.Error.AssetRenameForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check target asset permissions
|
|
|
|
const assetTargetPath = (asset.folderId) ? hierarchy.map(h => h.slug).join('/') + `/${filename}` : filename
|
|
|
|
if (!WIKI.auth.checkAccess(context.req.user, ['write:assets'], { path: assetTargetPath })) {
|
|
|
|
throw new WIKI.Error.AssetRenameTargetForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update filename + hash
|
|
|
|
const fileHash = assetHelper.generateHash(assetTargetPath)
|
|
|
|
await WIKI.models.assets.query().patch({
|
|
|
|
filename: filename,
|
|
|
|
hash: fileHash
|
|
|
|
}).findById(args.id)
|
|
|
|
|
|
|
|
// Delete old asset cache
|
|
|
|
await asset.deleteAssetCache()
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Asset has been renamed successfully.')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WIKI.Error.AssetInvalid()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Delete an Asset
|
|
|
|
*/
|
|
|
|
async deleteAsset(obj, args, context) {
|
|
|
|
try {
|
|
|
|
const asset = await WIKI.models.assets.query().findById(args.id)
|
|
|
|
if (asset) {
|
|
|
|
// Check permissions
|
|
|
|
const assetPath = asset.getAssetPath()
|
|
|
|
if (!WIKI.auth.checkAccess(context.req.user, ['manage:assets'], { path: assetPath })) {
|
|
|
|
throw new WIKI.Error.AssetDeleteForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
await WIKI.models.knex('assetData').where('id', args.id).del()
|
|
|
|
await WIKI.models.assets.query().deleteById(args.id)
|
|
|
|
await asset.deleteAssetCache()
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Asset has been deleted successfully.')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WIKI.Error.AssetInvalid()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-05-21 20:45:27 +00:00
|
|
|
}
|
2019-05-13 05:15:27 +00:00
|
|
|
}
|
|
|
|
// File: {
|
|
|
|
// folder(fl) {
|
|
|
|
// return fl.getFolder()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|