feat: serve asset from db + editor mobile improvements

This commit is contained in:
Nick
2019-05-18 02:27:30 -04:00
parent d27ffe7ca4
commit 965f0ad2cd
8 changed files with 168 additions and 88 deletions

View File

@@ -96,13 +96,9 @@ module.exports = class Asset extends Model {
}
static async getAsset(assetPath, res) {
let asset = await WIKI.models.assets.getAssetFromCache(assetPath, res)
if (!asset) {
// asset = await WIKI.models.assets.getAssetFromDb(assetPath, res)
// if (asset) {
// await WIKI.models.assets.saveAssetToCache(asset)
// }
res.sendStatus(404)
let assetExists = await WIKI.models.assets.getAssetFromCache(assetPath, res)
if (!assetExists) {
await WIKI.models.assets.getAssetFromDb(assetPath, res)
}
}
@@ -121,4 +117,19 @@ module.exports = class Asset extends Model {
})
})
}
static async getAssetFromDb(assetPath, res) {
const fileHash = assetHelper.generateHash(assetPath)
const cachePath = path.join(process.cwd(), `data/cache/${fileHash}.dat`)
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)
}
}
}