wikijs-fork/server/graph/resolvers/asset.js

203 lines
6.3 KiB
JavaScript
Raw Normal View History

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')
/* global WIKI */
module.exports = {
Query: {
async assets() { return {} }
},
Mutation: {
async assets() { return {} }
},
AssetQuery: {
async list(obj, args, context) {
let cond = {
2019-05-21 20:45:27 +00:00
folderId: args.folderId === 0 ? null : args.folderId
}
if (args.kind !== 'ALL') {
cond.kind = args.kind.toLowerCase()
}
const result = await WIKI.models.assets.query().where(cond)
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
}
},
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
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
2019-10-07 04:06:47 +00:00
const assetSourcePath = (asset.folderId) ? hierarchy.map(h => h.slug).join('/') + `/${asset.filename}` : asset.filename
2019-05-26 01:38:29 +00:00
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()
2019-10-07 04:06:47 +00:00
// Rename in Storage
await WIKI.models.storage.assetEvent({
event: 'renamed',
asset: {
...asset,
path: assetSourcePath,
destinationPath: assetTargetPath,
moveAuthorId: context.req.user.id,
moveAuthorName: context.req.user.name,
moveAuthorEmail: context.req.user.email
2019-10-07 04:06:47 +00:00
}
})
2019-05-26 01:38:29 +00:00
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 = await asset.getAssetPath()
2019-05-26 01:38:29 +00:00
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()
2019-10-07 04:06:47 +00:00
// Delete from Storage
await WIKI.models.storage.assetEvent({
event: 'deleted',
asset: {
...asset,
path: assetPath,
authorId: context.req.user.id,
authorName: context.req.user.name,
authorEmail: context.req.user.email
}
2019-10-07 04:06:47 +00:00
})
2019-05-26 01:38:29 +00:00
return {
responseResult: graphHelper.generateSuccess('Asset has been deleted successfully.')
}
} else {
throw new WIKI.Error.AssetInvalid()
}
} catch (err) {
return graphHelper.generateError(err)
}
2019-07-06 21:06:42 +00:00
},
/**
* Flush Temporary Uploads
*/
async flushTempUploads(obj, args, context) {
try {
await WIKI.models.assets.flushTempUploads()
return {
responseResult: graphHelper.generateSuccess('Temporary Uploads have been flushed successfully.')
}
} catch (err) {
return graphHelper.generateError(err)
}
2019-05-21 20:45:27 +00:00
}
}
// File: {
// folder(fl) {
// return fl.getFolder()
// }
// }
}