|
|
@ -6,6 +6,7 @@ const path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const _ = require('lodash')
|
|
|
|
const _ = require('lodash')
|
|
|
|
const assetHelper = require('../helpers/asset')
|
|
|
|
const assetHelper = require('../helpers/asset')
|
|
|
|
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Users model
|
|
|
|
* Users model
|
|
|
@ -150,32 +151,53 @@ module.exports = class Asset extends Model {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async getAsset(assetPath, res) {
|
|
|
|
static async getAsset(assetPath, res) {
|
|
|
|
let assetExists = await WIKI.models.assets.getAssetFromCache(assetPath, res)
|
|
|
|
try {
|
|
|
|
if (!assetExists) {
|
|
|
|
const fileHash = assetHelper.generateHash(assetPath)
|
|
|
|
await WIKI.models.assets.getAssetFromDb(assetPath, res)
|
|
|
|
const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async getAssetFromCache(assetPath, res) {
|
|
|
|
static async getAssetFromCache(assetPath, cachePath, res) {
|
|
|
|
const fileHash = assetHelper.generateHash(assetPath)
|
|
|
|
try {
|
|
|
|
const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`)
|
|
|
|
await fs.access(cachePath, fs.constants.R_OK)
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return false
|
|
|
|
res.type(path.extname(assetPath))
|
|
|
|
}
|
|
|
|
res.sendFile(cachePath, { dotfiles: 'deny' }, err => {
|
|
|
|
const sendFile = Promise.promisify(res.sendFile, {context: res})
|
|
|
|
if (err) {
|
|
|
|
res.type(path.extname(assetPath))
|
|
|
|
resolve(false)
|
|
|
|
await sendFile(cachePath, { dotfiles: 'deny' })
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
resolve(true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async getAssetFromDb(assetPath, res) {
|
|
|
|
static async getAssetFromStorage(assetPath, res) {
|
|
|
|
const fileHash = assetHelper.generateHash(assetPath)
|
|
|
|
const localLocations = await WIKI.models.storage.getLocalLocations({
|
|
|
|
const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`)
|
|
|
|
asset: {
|
|
|
|
|
|
|
|
path: assetPath,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async getAssetFromDb(assetPath, fileHash, cachePath, res) {
|
|
|
|
const asset = await WIKI.models.assets.query().where('hash', fileHash).first()
|
|
|
|
const asset = await WIKI.models.assets.query().where('hash', fileHash).first()
|
|
|
|
if (asset) {
|
|
|
|
if (asset) {
|
|
|
|
const assetData = await WIKI.models.knex('assetData').where('id', asset.id).first()
|
|
|
|
const assetData = await WIKI.models.knex('assetData').where('id', asset.id).first()
|
|
|
|