dobo
Version:
DBMS for Bajo Framework
41 lines (37 loc) • 1.51 kB
JavaScript
import path from 'path'
const action = 'listAttachment'
async function listAttachment (...args) {
if (!this.options.attachment || !this.app.waibu) return []
if (args.length === 0) return this.action(action, ...args)
const [params = {}, opts = {}] = args
const { map, kebabCase } = this.app.lib._
const { importPkg } = this.app.bajo
const mime = await importPkg('waibu:mime')
const { fastGlob } = this.app.lib
const { id = '*', field = '*', file = '*', type } = params
const { uriEncoded = true } = opts
const root = `${this.app.getPluginDataDir('dobo')}/attachment`
let pattern = `${root}/${this.name}/${id}/${field}/${file}`
if (type === 'image') pattern += '.{jpg,jpeg,gif,png,webp,avif,svg}'
else if (type === 'video') pattern += '.{mp4,m4v,webm,mov,qt,mkv,ogg,ogv}'
else if (type) pattern += `.${type}`
if (uriEncoded) pattern = pattern.split('/').map(p => decodeURI(p)).join('/')
return map(await fastGlob(pattern), f => {
const mimeType = mime.getType(path.extname(f)) ?? ''
const fullPath = f.replace(root, '')
const row = {
file: f,
fileName: path.basename(fullPath),
fullPath,
mimeType,
params: { model: this.name, id, field, file }
}
if (this.app.waibuMpa) {
const { routePath } = this.app.waibu
const [, _model, _id, _field, _file] = fullPath.split('/')
row.url = routePath(`dobo:/attachment/${kebabCase(_model)}/${_id}/${_field}/${_file}`)
}
return row
})
}
export default listAttachment