2019-05-13 05:15:27 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
const Model = require('objection').Model
|
|
|
|
const moment = require('moment')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const _ = require('lodash')
|
|
|
|
const assetHelper = require('../helpers/asset')
|
2020-07-12 16:19:01 +00:00
|
|
|
const Promise = require('bluebird')
|
2019-05-13 05:15:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Users model
|
|
|
|
*/
|
|
|
|
module.exports = class Asset extends Model {
|
|
|
|
static get tableName() { return 'assets' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
filename: {type: 'string'},
|
|
|
|
hash: {type: 'string'},
|
|
|
|
ext: {type: 'string'},
|
|
|
|
kind: {type: 'string'},
|
|
|
|
mime: {type: 'string'},
|
|
|
|
fileSize: {type: 'integer'},
|
|
|
|
metadata: {type: 'object'},
|
|
|
|
createdAt: {type: 'string'},
|
|
|
|
updatedAt: {type: 'string'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get relationMappings() {
|
|
|
|
return {
|
|
|
|
author: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./users'),
|
|
|
|
join: {
|
|
|
|
from: 'assets.authorId',
|
|
|
|
to: 'users.id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
folder: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./assetFolders'),
|
|
|
|
join: {
|
|
|
|
from: 'assets.folderId',
|
|
|
|
to: 'assetFolders.id'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async $beforeUpdate(opt, context) {
|
|
|
|
await super.$beforeUpdate(opt, context)
|
|
|
|
|
|
|
|
this.updatedAt = moment.utc().toISOString()
|
|
|
|
}
|
|
|
|
async $beforeInsert(context) {
|
|
|
|
await super.$beforeInsert(context)
|
|
|
|
|
|
|
|
this.createdAt = moment.utc().toISOString()
|
|
|
|
this.updatedAt = moment.utc().toISOString()
|
|
|
|
}
|
|
|
|
|
2019-05-26 01:38:29 +00:00
|
|
|
async getAssetPath() {
|
|
|
|
let hierarchy = []
|
|
|
|
if (this.folderId) {
|
|
|
|
hierarchy = await WIKI.models.assetFolders.getHierarchy(this.folderId)
|
|
|
|
}
|
|
|
|
return (this.folderId) ? hierarchy.map(h => h.slug).join('/') + `/${this.filename}` : this.filename
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAssetCache() {
|
2019-10-25 22:20:02 +00:00
|
|
|
await fs.remove(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${this.hash}.dat`))
|
2019-05-26 01:38:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 05:15:27 +00:00
|
|
|
static async upload(opts) {
|
|
|
|
const fileInfo = path.parse(opts.originalname)
|
2019-05-22 00:58:04 +00:00
|
|
|
const fileHash = assetHelper.generateHash(opts.assetPath)
|
2019-05-13 05:15:27 +00:00
|
|
|
|
2019-05-25 01:46:06 +00:00
|
|
|
// Check for existing asset
|
|
|
|
let asset = await WIKI.models.assets.query().where({
|
|
|
|
hash: fileHash,
|
|
|
|
folderId: opts.folderId
|
|
|
|
}).first()
|
|
|
|
|
|
|
|
// Build Object
|
|
|
|
let assetRow = {
|
2019-05-13 05:15:27 +00:00
|
|
|
filename: opts.originalname,
|
|
|
|
hash: fileHash,
|
|
|
|
ext: fileInfo.ext,
|
|
|
|
kind: _.startsWith(opts.mimetype, 'image/') ? 'image' : 'binary',
|
|
|
|
mime: opts.mimetype,
|
|
|
|
fileSize: opts.size,
|
2019-10-20 18:42:10 +00:00
|
|
|
folderId: opts.folderId
|
2019-05-25 01:46:06 +00:00
|
|
|
}
|
2019-05-13 05:15:27 +00:00
|
|
|
|
2021-12-18 02:41:23 +00:00
|
|
|
// Sanitize SVG contents
|
2021-12-25 00:36:30 +00:00
|
|
|
if (
|
|
|
|
WIKI.config.uploads.scanSVG &&
|
|
|
|
(
|
|
|
|
opts.mimetype.toLowerCase().startsWith('image/svg') ||
|
2021-12-26 22:29:45 +00:00
|
|
|
fileInfo.ext.toLowerCase() === '.svg'
|
2021-12-25 00:36:30 +00:00
|
|
|
)
|
|
|
|
) {
|
2021-12-18 02:41:23 +00:00
|
|
|
const svgSanitizeJob = await WIKI.scheduler.registerJob({
|
|
|
|
name: 'sanitize-svg',
|
|
|
|
immediate: true,
|
|
|
|
worker: true
|
|
|
|
}, opts.path)
|
|
|
|
await svgSanitizeJob.finished
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:15:27 +00:00
|
|
|
// Save asset data
|
|
|
|
try {
|
|
|
|
const fileBuffer = await fs.readFile(opts.path)
|
2019-05-25 01:46:06 +00:00
|
|
|
|
|
|
|
if (asset) {
|
|
|
|
// Patch existing asset
|
2019-10-20 18:42:10 +00:00
|
|
|
if (opts.mode === 'upload') {
|
|
|
|
assetRow.authorId = opts.user.id
|
|
|
|
}
|
2019-05-25 01:46:06 +00:00
|
|
|
await WIKI.models.assets.query().patch(assetRow).findById(asset.id)
|
|
|
|
await WIKI.models.knex('assetData').where({
|
|
|
|
id: asset.id
|
|
|
|
}).update({
|
|
|
|
data: fileBuffer
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Create asset entry
|
2019-10-20 18:42:10 +00:00
|
|
|
assetRow.authorId = opts.user.id
|
2019-05-25 01:46:06 +00:00
|
|
|
asset = await WIKI.models.assets.query().insert(assetRow)
|
|
|
|
await WIKI.models.knex('assetData').insert({
|
|
|
|
id: asset.id,
|
|
|
|
data: fileBuffer
|
|
|
|
})
|
|
|
|
}
|
2019-10-19 21:39:45 +00:00
|
|
|
|
|
|
|
// Move temp upload to cache
|
2019-10-20 18:42:10 +00:00
|
|
|
if (opts.mode === 'upload') {
|
2019-10-25 22:20:02 +00:00
|
|
|
await fs.move(opts.path, path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`), { overwrite: true })
|
2019-10-20 18:42:10 +00:00
|
|
|
} else {
|
2019-10-25 22:20:02 +00:00
|
|
|
await fs.copy(opts.path, path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`), { overwrite: true })
|
2019-10-20 18:42:10 +00:00
|
|
|
}
|
2019-10-19 21:39:45 +00:00
|
|
|
|
|
|
|
// Add to Storage
|
|
|
|
if (!opts.skipStorage) {
|
|
|
|
await WIKI.models.storage.assetEvent({
|
|
|
|
event: 'uploaded',
|
|
|
|
asset: {
|
|
|
|
...asset,
|
|
|
|
path: await asset.getAssetPath(),
|
|
|
|
data: fileBuffer,
|
|
|
|
authorId: opts.user.id,
|
|
|
|
authorName: opts.user.name,
|
|
|
|
authorEmail: opts.user.email
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-05-13 05:15:27 +00:00
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 04:53:19 +00:00
|
|
|
|
|
|
|
static async getAsset(assetPath, res) {
|
2020-07-12 16:19:01 +00:00
|
|
|
try {
|
2021-12-25 01:18:12 +00:00
|
|
|
const fileInfo = assetHelper.getPathInfo(assetPath)
|
2020-07-12 16:19:01 +00:00
|
|
|
const fileHash = assetHelper.generateHash(assetPath)
|
|
|
|
const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`)
|
2021-12-25 01:18:12 +00:00
|
|
|
|
|
|
|
// Force unsafe extensions to download
|
|
|
|
if (WIKI.config.uploads.forceDownload && !['.png', '.apng', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg'].includes(fileInfo.ext)) {
|
|
|
|
res.set('Content-disposition', 'attachment; filename=' + fileInfo.base)
|
|
|
|
}
|
|
|
|
|
2020-07-12 16:19:01 +00:00
|
|
|
if (await WIKI.models.assets.getAssetFromCache(assetPath, cachePath, res)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (await WIKI.models.assets.getAssetFromStorage(assetPath, res)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await WIKI.models.assets.getAssetFromDb(assetPath, fileHash, cachePath, res)
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === `ECONNABORTED` || err.code === `EPIPE`) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
res.sendStatus(500)
|
2019-05-14 04:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 16:19:01 +00:00
|
|
|
static async getAssetFromCache(assetPath, cachePath, res) {
|
|
|
|
try {
|
|
|
|
await fs.access(cachePath, fs.constants.R_OK)
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const sendFile = Promise.promisify(res.sendFile, {context: res})
|
|
|
|
res.type(path.extname(assetPath))
|
|
|
|
await sendFile(cachePath, { dotfiles: 'deny' })
|
|
|
|
return true
|
2019-05-14 04:53:19 +00:00
|
|
|
}
|
2019-05-18 06:27:30 +00:00
|
|
|
|
2020-07-12 16:19:01 +00:00
|
|
|
static async getAssetFromStorage(assetPath, res) {
|
|
|
|
const localLocations = await WIKI.models.storage.getLocalLocations({
|
|
|
|
asset: {
|
2020-08-22 23:37:49 +00:00
|
|
|
path: assetPath
|
2020-07-12 16:19:01 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
for (let location of _.filter(localLocations, location => Boolean(location.path))) {
|
|
|
|
const assetExists = await WIKI.models.assets.getAssetFromCache(assetPath, location.path, res)
|
|
|
|
if (assetExists) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-05-18 06:27:30 +00:00
|
|
|
|
2020-07-12 16:19:01 +00:00
|
|
|
static async getAssetFromDb(assetPath, fileHash, cachePath, res) {
|
2019-05-18 06:27:30 +00:00
|
|
|
const asset = await WIKI.models.assets.query().where('hash', fileHash).first()
|
|
|
|
if (asset) {
|
|
|
|
const assetData = await WIKI.models.knex('assetData').where('id', asset.id).first()
|
|
|
|
res.type(asset.ext)
|
|
|
|
res.send(assetData.data)
|
|
|
|
await fs.outputFile(cachePath, assetData.data)
|
|
|
|
} else {
|
|
|
|
res.sendStatus(404)
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 21:06:42 +00:00
|
|
|
|
|
|
|
static async flushTempUploads() {
|
2019-10-25 22:20:02 +00:00
|
|
|
return fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `uploads`))
|
2019-07-06 21:06:42 +00:00
|
|
|
}
|
2019-05-13 05:15:27 +00:00
|
|
|
}
|