mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
384 lines
13 KiB
384 lines
13 KiB
2 years ago
|
import _ from 'lodash-es'
|
||
|
import sanitize from 'sanitize-filename'
|
||
|
import { generateError, generateSuccess } from '../../helpers/graph.mjs'
|
||
|
import path from 'node:path'
|
||
|
import fs from 'fs-extra'
|
||
|
import { v4 as uuid } from 'uuid'
|
||
|
import { pipeline } from 'node:stream/promises'
|
||
|
|
||
|
export default {
|
||
6 years ago
|
Query: {
|
||
2 years ago
|
async assetById(obj, args, context) {
|
||
|
return null
|
||
6 years ago
|
}
|
||
|
},
|
||
3 years ago
|
Mutation: {
|
||
6 years ago
|
/**
|
||
|
* Rename an Asset
|
||
|
*/
|
||
|
async renameAsset(obj, args, context) {
|
||
|
try {
|
||
|
const filename = sanitize(args.filename).toLowerCase()
|
||
|
|
||
2 years ago
|
const asset = await WIKI.db.assets.query().findById(args.id)
|
||
6 years ago
|
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
|
||
2 years ago
|
const assetCollision = await WIKI.db.assets.query().where({
|
||
6 years ago
|
filename,
|
||
|
folderId: asset.folderId
|
||
|
}).first()
|
||
|
if (assetCollision) {
|
||
|
throw new WIKI.Error.AssetRenameCollision()
|
||
|
}
|
||
|
|
||
|
// Get asset folder path
|
||
|
let hierarchy = []
|
||
|
if (asset.folderId) {
|
||
2 years ago
|
hierarchy = await WIKI.db.assetFolders.getHierarchy(asset.folderId)
|
||
6 years ago
|
}
|
||
|
|
||
|
// Check source asset permissions
|
||
5 years ago
|
const assetSourcePath = (asset.folderId) ? hierarchy.map(h => h.slug).join('/') + `/${asset.filename}` : asset.filename
|
||
6 years ago
|
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
|
||
2 years ago
|
const fileHash = '' // assetHelper.generateHash(assetTargetPath)
|
||
2 years ago
|
await WIKI.db.assets.query().patch({
|
||
6 years ago
|
filename: filename,
|
||
|
hash: fileHash
|
||
|
}).findById(args.id)
|
||
|
|
||
|
// Delete old asset cache
|
||
|
await asset.deleteAssetCache()
|
||
|
|
||
5 years ago
|
// Rename in Storage
|
||
2 years ago
|
await WIKI.db.storage.assetEvent({
|
||
5 years ago
|
event: 'renamed',
|
||
|
asset: {
|
||
|
...asset,
|
||
5 years ago
|
path: assetSourcePath,
|
||
|
destinationPath: assetTargetPath,
|
||
|
moveAuthorId: context.req.user.id,
|
||
|
moveAuthorName: context.req.user.name,
|
||
|
moveAuthorEmail: context.req.user.email
|
||
5 years ago
|
}
|
||
|
})
|
||
|
|
||
6 years ago
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Asset has been renamed successfully.')
|
||
6 years ago
|
}
|
||
|
} else {
|
||
|
throw new WIKI.Error.AssetInvalid()
|
||
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
6 years ago
|
}
|
||
|
},
|
||
|
/**
|
||
|
* Delete an Asset
|
||
|
*/
|
||
|
async deleteAsset(obj, args, context) {
|
||
|
try {
|
||
2 years ago
|
const asset = await WIKI.db.assets.query().findById(args.id)
|
||
6 years ago
|
if (asset) {
|
||
|
// Check permissions
|
||
5 years ago
|
const assetPath = await asset.getAssetPath()
|
||
6 years ago
|
if (!WIKI.auth.checkAccess(context.req.user, ['manage:assets'], { path: assetPath })) {
|
||
|
throw new WIKI.Error.AssetDeleteForbidden()
|
||
|
}
|
||
|
|
||
2 years ago
|
await WIKI.db.knex('assetData').where('id', args.id).del()
|
||
|
await WIKI.db.assets.query().deleteById(args.id)
|
||
6 years ago
|
await asset.deleteAssetCache()
|
||
|
|
||
5 years ago
|
// Delete from Storage
|
||
2 years ago
|
await WIKI.db.storage.assetEvent({
|
||
5 years ago
|
event: 'deleted',
|
||
5 years ago
|
asset: {
|
||
|
...asset,
|
||
|
path: assetPath,
|
||
|
authorId: context.req.user.id,
|
||
|
authorName: context.req.user.name,
|
||
|
authorEmail: context.req.user.email
|
||
|
}
|
||
5 years ago
|
})
|
||
|
|
||
6 years ago
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Asset has been deleted successfully.')
|
||
6 years ago
|
}
|
||
|
} else {
|
||
|
throw new WIKI.Error.AssetInvalid()
|
||
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
6 years ago
|
}
|
||
6 years ago
|
},
|
||
2 years ago
|
/**
|
||
|
* Upload Assets
|
||
|
*/
|
||
|
async uploadAssets(obj, args, context) {
|
||
|
try {
|
||
2 years ago
|
// -> Get Folder
|
||
2 years ago
|
let folder = {}
|
||
|
if (args.folderId || args.folderPath) {
|
||
|
// Get Folder by ID
|
||
|
folder = await WIKI.db.tree.getFolder({ id: args.folderId })
|
||
|
if (!folder) {
|
||
|
throw new Error('ERR_INVALID_FOLDER_ID')
|
||
|
}
|
||
|
} else if (args.folderPath) {
|
||
|
// Get Folder by Path
|
||
|
if (!args.locale) {
|
||
|
throw new Error('ERR_MISSING_LOCALE')
|
||
|
} else if (!args.siteId) {
|
||
|
throw new Error('ERR_MISSING_SITE_ID')
|
||
|
}
|
||
|
folder = await WIKI.db.tree.getFolder({
|
||
|
path: args.folderPath,
|
||
|
locale: args.locale,
|
||
|
siteId: args.siteId,
|
||
|
createIfMissing: true
|
||
|
})
|
||
|
if (!folder) {
|
||
|
throw new Error('ERR_INVALID_FOLDER_PATH')
|
||
|
}
|
||
|
} else {
|
||
|
// Use Root Folder
|
||
|
if (!args.locale) {
|
||
|
throw new Error('ERR_MISSING_LOCALE')
|
||
|
} else if (!args.siteId) {
|
||
|
throw new Error('ERR_MISSING_SITE_ID')
|
||
|
}
|
||
|
folder = {
|
||
|
folderPath: '',
|
||
|
fileName: '',
|
||
|
localeCode: args.locale,
|
||
|
siteId: args.siteId
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
// -> Get Site
|
||
|
const site = await WIKI.db.sites.query().findById(folder.siteId)
|
||
|
if (!site) {
|
||
|
throw new Error('ERR_INVALID_SITE_ID')
|
||
|
}
|
||
|
|
||
2 years ago
|
// -> Get Storage Targets
|
||
|
const storageTargets = await WIKI.db.storage.getTargets({ siteId: folder.siteId, enabledOnly: true })
|
||
|
|
||
|
// -> Process Assets
|
||
2 years ago
|
const results = await Promise.allSettled(args.files.map(async fl => {
|
||
|
const { filename, mimetype, createReadStream } = await fl
|
||
2 years ago
|
const sanitizedFilename = sanitize(filename).toLowerCase().trim()
|
||
|
|
||
|
WIKI.logger.debug(`Processing asset upload ${sanitizedFilename} of type ${mimetype}...`)
|
||
|
|
||
|
// Parse file extension
|
||
|
if (sanitizedFilename.indexOf('.') <= 0) {
|
||
|
throw new Error('ERR_ASSET_DOTFILE_NOTALLOWED')
|
||
|
}
|
||
|
const fileExt = _.last(sanitizedFilename.split('.')).toLowerCase()
|
||
|
|
||
|
// Determine asset kind
|
||
|
let fileKind = 'other'
|
||
|
switch (fileExt) {
|
||
|
case 'jpg':
|
||
|
case 'jpeg':
|
||
|
case 'png':
|
||
|
case 'webp':
|
||
|
case 'gif':
|
||
|
case 'tiff':
|
||
|
case 'svg':
|
||
|
fileKind = 'image'
|
||
|
break
|
||
|
case 'pdf':
|
||
|
case 'docx':
|
||
|
case 'xlsx':
|
||
|
case 'pptx':
|
||
|
case 'odt':
|
||
|
case 'epub':
|
||
|
case 'csv':
|
||
|
case 'md':
|
||
|
case 'txt':
|
||
|
case 'adoc':
|
||
|
case 'rtf':
|
||
|
case 'wdp':
|
||
|
case 'xps':
|
||
|
case 'ods':
|
||
|
fileKind = 'document'
|
||
|
break
|
||
|
}
|
||
|
|
||
|
// Save to temp disk
|
||
|
const tempFileId = uuid()
|
||
|
const tempFilePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `uploads/${tempFileId}.dat`)
|
||
|
WIKI.logger.debug(`Writing asset upload ${sanitizedFilename} to temp disk...`)
|
||
|
await pipeline(
|
||
|
createReadStream(),
|
||
|
fs.createWriteStream(tempFilePath)
|
||
|
)
|
||
|
WIKI.logger.debug(`Querying asset ${sanitizedFilename} file size...`)
|
||
|
const tempFileStat = await fs.stat(tempFilePath)
|
||
2 years ago
|
|
||
2 years ago
|
// Format filename
|
||
2 years ago
|
const formattedFilename = site.config.uploads.normalizeFilename ? sanitizedFilename.replaceAll(' ', '-') : sanitizedFilename
|
||
2 years ago
|
|
||
2 years ago
|
// Save asset to DB
|
||
2 years ago
|
WIKI.logger.debug(`Saving asset ${sanitizedFilename} metadata to DB...`)
|
||
|
const assetRaw = await WIKI.db.knex('assets').insert({
|
||
|
fileName: formattedFilename,
|
||
|
fileExt,
|
||
|
kind: fileKind,
|
||
|
mimeType: mimetype,
|
||
|
fileSize: Math.round(tempFileStat.size),
|
||
|
meta: {},
|
||
|
previewState: fileKind === 'image' ? 'pending' : 'none',
|
||
|
authorId: context.req.user.id,
|
||
|
siteId: folder.siteId
|
||
|
}).returning('*')
|
||
2 years ago
|
|
||
2 years ago
|
const asset = assetRaw[0]
|
||
2 years ago
|
|
||
|
// Add to tree
|
||
2 years ago
|
await WIKI.db.tree.addAsset({
|
||
2 years ago
|
id: asset.id,
|
||
2 years ago
|
parentPath: folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName,
|
||
2 years ago
|
fileName: formattedFilename,
|
||
2 years ago
|
title: formattedFilename,
|
||
|
locale: folder.localeCode,
|
||
|
siteId: folder.siteId,
|
||
|
meta: {
|
||
|
authorId: asset.authorId,
|
||
|
creatorId: asset.creatorId,
|
||
|
fileSize: asset.fileSize,
|
||
|
fileExt,
|
||
|
mimeType: mimetype,
|
||
|
ownerId: asset.ownerId
|
||
|
}
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
2 years ago
|
// Save to storage targets
|
||
|
const storageInfo = {}
|
||
|
const failedStorage = []
|
||
|
await Promise.allSettled(storageTargets.map(async storageTarget => {
|
||
|
WIKI.logger.debug(`Saving asset ${sanitizedFilename} to storage target ${storageTarget.module} (${storageTarget.id})...`)
|
||
|
try {
|
||
|
const strInfo = await WIKI.storage.modules[storageTarget.module].assetUploaded({
|
||
|
asset,
|
||
|
createReadStream,
|
||
|
storageTarget,
|
||
|
tempFilePath
|
||
|
})
|
||
|
storageInfo[storageTarget.id] = strInfo ?? true
|
||
|
} catch (err) {
|
||
|
WIKI.logger.warn(`Failed to save asset ${sanitizedFilename} to storage target ${storageTarget.module} (${storageTarget.id}):`)
|
||
|
WIKI.logger.warn(err)
|
||
|
failedStorage.push({
|
||
|
storageId: storageTarget.id,
|
||
|
storageModule: storageTarget.module,
|
||
|
fileId: asset.id,
|
||
|
fileName: formattedFilename
|
||
|
})
|
||
|
}
|
||
|
}))
|
||
|
|
||
|
// Save Storage Info to DB
|
||
|
await WIKI.db.knex('assets').where({ id: asset.id }).update({ storageInfo })
|
||
|
|
||
2 years ago
|
// Create thumbnail
|
||
2 years ago
|
if (fileKind === 'image') {
|
||
2 years ago
|
if (!WIKI.extensions.ext.sharp.isInstalled) {
|
||
|
WIKI.logger.warn('Cannot generate asset thumbnail because the Sharp extension is not installed.')
|
||
|
} else {
|
||
2 years ago
|
WIKI.logger.debug(`Generating thumbnail of asset ${sanitizedFilename}...`)
|
||
2 years ago
|
const previewDestPath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `uploads/${tempFileId}-thumb.webp`)
|
||
2 years ago
|
// -> Resize
|
||
|
await WIKI.extensions.ext.sharp.resize({
|
||
2 years ago
|
format: 'webp',
|
||
2 years ago
|
inputStream: createReadStream(),
|
||
2 years ago
|
outputPath: previewDestPath,
|
||
|
width: 320,
|
||
|
height: 200,
|
||
|
fit: 'inside'
|
||
|
})
|
||
|
// -> Save to DB
|
||
|
await WIKI.db.knex('assets').where({
|
||
|
id: asset.id
|
||
|
}).update({
|
||
|
preview: await fs.readFile(previewDestPath),
|
||
|
previewState: 'ready'
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
|
// -> Delete
|
||
|
await fs.remove(previewDestPath)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
WIKI.logger.debug(`Removing asset ${sanitizedFilename} temp file...`)
|
||
|
await fs.remove(tempFilePath)
|
||
|
|
||
|
WIKI.logger.debug(`Processed asset ${sanitizedFilename} successfully.`)
|
||
|
return failedStorage
|
||
2 years ago
|
}))
|
||
2 years ago
|
|
||
|
// Return results
|
||
|
const failedResults = results.filter(r => r.status === 'rejected')
|
||
|
if (failedResults.length > 0) {
|
||
|
// -> One or more thrown errors
|
||
|
WIKI.logger.warn(`Failed to upload one or more assets:`)
|
||
|
for (const failedResult of failedResults) {
|
||
|
WIKI.logger.warn(failedResult.reason)
|
||
|
}
|
||
|
throw new Error('ERR_UPLOAD_FAILED')
|
||
|
} else {
|
||
|
const failedSaveTargets = results.map(r => r.value).filter(r => r.length > 0)
|
||
|
if (failedSaveTargets.length > 0) {
|
||
|
// -> One or more storage target save errors
|
||
|
WIKI.logger.warn('Failed to save one or more assets to storage targets.')
|
||
|
throw new Error('ERR_UPLOAD_TARGET_FAILED')
|
||
|
} else {
|
||
|
WIKI.logger.debug('Asset(s) uploaded successfully.')
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('Asset(s) uploaded successfully')
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
WIKI.logger.warn(err)
|
||
2 years ago
|
return generateError(err)
|
||
2 years ago
|
}
|
||
|
},
|
||
6 years ago
|
/**
|
||
|
* Flush Temporary Uploads
|
||
|
*/
|
||
|
async flushTempUploads(obj, args, context) {
|
||
|
try {
|
||
2 years ago
|
await WIKI.db.assets.flushTempUploads()
|
||
6 years ago
|
return {
|
||
2 years ago
|
responseResult: generateSuccess('Temporary Uploads have been flushed successfully.')
|
||
6 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
}
|