dobo
Version:
DBMS for Bajo Framework
29 lines (26 loc) • 880 B
JavaScript
import { mergeAttachmentInfo } from './_util.js'
const action = 'findAttachment'
async function findAttachment (...args) {
if (!this.options.attachment) return
if (args.length === 0) return this.action(action, ...args)
const [id, opts = {}] = args
const { fastGlob, fs } = this.app.lib
const dir = `${this.app.getPluginDataDir(this.plugin.ns)}/attachment/${this.name}/${id}`
if (!fs.existsSync(dir)) return []
const files = await fastGlob(`${dir}/**/*`)
const { fullPath, stats, mimeType } = opts
const recs = []
for (const f of files) {
const item = f.replace(dir, '')
let [, field, file] = item.split('/')
if (!file) {
file = field
field = null
}
const rec = { field, file }
await mergeAttachmentInfo.call(this, rec, f, { mimeType, fullPath, stats })
recs.push(rec)
}
return recs
}
export default findAttachment