feat: image upload / display
This commit is contained in:
@@ -41,11 +41,17 @@ router.post('/u', multer({
|
||||
})
|
||||
}
|
||||
|
||||
let folderPath = ''
|
||||
// Get folder Id
|
||||
let folderId = null
|
||||
try {
|
||||
const folderRaw = _.get(req, 'body.mediaUpload', false)
|
||||
if (folderRaw) {
|
||||
folderPath = _.get(JSON.parse(folderRaw), 'path', false)
|
||||
folderId = _.get(JSON.parse(folderRaw), 'folderId', null)
|
||||
if (folderId === 0) {
|
||||
folderId = null
|
||||
}
|
||||
} else {
|
||||
throw new Error('Missing File Metadata')
|
||||
}
|
||||
} catch (err) {
|
||||
return res.status(400).json({
|
||||
@@ -54,17 +60,34 @@ router.post('/u', multer({
|
||||
})
|
||||
}
|
||||
|
||||
if (!WIKI.auth.checkAccess(req.user, ['write:assets'], { path: `${folderPath}/${fileMeta.originalname}`})) {
|
||||
// Build folder hierarchy
|
||||
let hierarchy = []
|
||||
if (folderId) {
|
||||
try {
|
||||
hierarchy = await WIKI.models.assetFolders.getHierarchy(folderId)
|
||||
} catch (err) {
|
||||
return res.status(400).json({
|
||||
succeeded: false,
|
||||
message: 'Failed to fetch folder hierarchy.'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check if user can upload at path
|
||||
const assetPath = (folderId) ? opts.hierarchy.map(h => h.slug).join('/') + `/${fileMeta.originalname}` : fileMeta.originalname
|
||||
if (!WIKI.auth.checkAccess(req.user, ['write:assets'], { path: assetPath })) {
|
||||
return res.status(403).json({
|
||||
succeeded: false,
|
||||
message: 'You are not authorized to upload files to this folder.'
|
||||
})
|
||||
}
|
||||
|
||||
// Process upload file
|
||||
await WIKI.models.assets.upload({
|
||||
...fileMeta,
|
||||
originalname: sanitize(fileMeta.originalname).toLowerCase(),
|
||||
folder: folderPath,
|
||||
folderId: folderId,
|
||||
hierarchy,
|
||||
userId: req.user.id
|
||||
})
|
||||
res.send('ok')
|
||||
|
@@ -1,8 +1,8 @@
|
||||
const sanitize = require('sanitize-filename')
|
||||
const graphHelper = require('../../helpers/graph')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
const gql = require('graphql')
|
||||
|
||||
module.exports = {
|
||||
Query: {
|
||||
async assets() { return {} }
|
||||
@@ -13,7 +13,7 @@ module.exports = {
|
||||
AssetQuery: {
|
||||
async list(obj, args, context) {
|
||||
let cond = {
|
||||
folderId: null
|
||||
folderId: args.folderId === 0 ? null : args.folderId
|
||||
}
|
||||
if (args.kind !== 'ALL') {
|
||||
cond.kind = args.kind.toLowerCase()
|
||||
@@ -23,9 +23,40 @@ module.exports = {
|
||||
...a,
|
||||
kind: a.kind.toUpperCase()
|
||||
}))
|
||||
},
|
||||
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: {
|
||||
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({
|
||||
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)
|
||||
}
|
||||
}
|
||||
// deleteFile(obj, args) {
|
||||
// return WIKI.models.File.destroy({
|
||||
// where: {
|
||||
|
@@ -16,9 +16,13 @@ extend type Mutation {
|
||||
|
||||
type AssetQuery {
|
||||
list(
|
||||
root: String
|
||||
kind: AssetKind
|
||||
folderId: Int!
|
||||
kind: AssetKind!
|
||||
): [AssetItem] @auth(requires: ["manage:system", "read:assets"])
|
||||
|
||||
folders(
|
||||
parentFolderId: Int!
|
||||
): [AssetFolder] @auth(requires: ["manage:system", "read:assets"])
|
||||
}
|
||||
|
||||
# -----------------------------------------------
|
||||
@@ -26,9 +30,11 @@ type AssetQuery {
|
||||
# -----------------------------------------------
|
||||
|
||||
type AssetMutation {
|
||||
upload(
|
||||
data: Upload!
|
||||
): DefaultResponse
|
||||
createFolder(
|
||||
parentFolderId: Int!
|
||||
slug: String!
|
||||
name: String
|
||||
): DefaultResponse @auth(requires: ["manage:system", "write:assets"])
|
||||
}
|
||||
|
||||
# -----------------------------------------------
|
||||
@@ -51,6 +57,8 @@ type AssetItem {
|
||||
|
||||
type AssetFolder {
|
||||
id: Int!
|
||||
slug: String!
|
||||
name: String
|
||||
}
|
||||
|
||||
enum AssetKind {
|
||||
|
@@ -1,6 +1,10 @@
|
||||
const CustomError = require('custom-error-instance')
|
||||
|
||||
module.exports = {
|
||||
AssetFolderExists: CustomError('AssetFolderExists', {
|
||||
message: 'An asset folder with the same name already exists.',
|
||||
code: 2001
|
||||
}),
|
||||
AuthAccountBanned: CustomError('AuthAccountBanned', {
|
||||
message: 'Your account has been disabled.',
|
||||
code: 1016
|
||||
|
@@ -32,4 +32,12 @@ module.exports = class AssetFolder extends Model {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async getHierarchy(folderId) {
|
||||
return WIKI.models.knex.withRecursive('ancestors', qb => {
|
||||
qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).union(sqb => {
|
||||
sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id')
|
||||
})
|
||||
}).select('*').from('ancestors')
|
||||
}
|
||||
}
|
||||
|
@@ -67,7 +67,8 @@ module.exports = class Asset extends Model {
|
||||
|
||||
static async upload(opts) {
|
||||
const fileInfo = path.parse(opts.originalname)
|
||||
const fileHash = assetHelper.generateHash(`${opts.folder}/${opts.originalname}`)
|
||||
const folderPath = opts.hierarchy.map(h => h.slug).join('/')
|
||||
const fileHash = opts.folderId ? assetHelper.generateHash(`${folderPath}/${opts.originalname}`) : assetHelper.generateHash(opts.originalname)
|
||||
|
||||
// Create asset entry
|
||||
const asset = await WIKI.models.assets.query().insert({
|
||||
@@ -77,7 +78,7 @@ module.exports = class Asset extends Model {
|
||||
kind: _.startsWith(opts.mimetype, 'image/') ? 'image' : 'binary',
|
||||
mime: opts.mimetype,
|
||||
fileSize: opts.size,
|
||||
folderId: null,
|
||||
folderId: opts.folderId,
|
||||
authorId: opts.userId
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user