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

@@ -12,10 +12,13 @@ module.exports = {
},
AssetQuery: {
async list(obj, args, context) {
const result = await WIKI.models.assets.query().where({
folderId: null,
kind: args.kind.toLowerCase()
})
let cond = {
folderId: null
}
if (args.kind !== 'ALL') {
cond.kind = args.kind.toLowerCase()
}
const result = await WIKI.models.assets.query().where(cond)
return result.map(a => ({
...a,
kind: a.kind.toUpperCase()

View File

@@ -56,4 +56,5 @@ type AssetFolder {
enum AssetKind {
IMAGE
BINARY
ALL
}

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)
}
}
}