dobo 1.1.5 → 1.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,9 @@
1
1
  import path from 'path'
2
2
 
3
3
  async function addFixture (name, { spinner } = {}) {
4
- const { resolvePath, readConfig, eachPlugins } = this.app.bajo
5
- const { isEmpty, isArray } = this.lib._
4
+ const { resolvePath, readConfig, eachPlugins, getPluginDataDir } = this.app.bajo
5
+ const { isEmpty, isArray, isString } = this.lib._
6
+ const { fs } = this.lib
6
7
  const { schema, connection } = this.getInfo(name)
7
8
  if (connection.proxy) {
8
9
  this.log.warn('proxiedConnBound%s', schema.name)
@@ -37,7 +38,19 @@ async function addFixture (name, { spinner } = {}) {
37
38
  }
38
39
  if (v === null) item[k] = undefined
39
40
  }
40
- await this.recordCreate(schema.name, item, { force: true })
41
+ const resp = await this.recordCreate(schema.name, item, { force: true })
42
+ if (isArray(item._attachments) && item._attachments.length > 0) {
43
+ for (let att of item._attachments) {
44
+ if (isString(att)) att = { field: 'file', file: att }
45
+ const fname = path.basename(att.file)
46
+ if (fs.existsSync(att.file)) {
47
+ const dest = `${getPluginDataDir(schema.ns)}/${resp.id}/${att.field}/${fname}`
48
+ try {
49
+ fs.copySync(att.file, dest)
50
+ } catch (err) {}
51
+ }
52
+ }
53
+ }
41
54
  result.success++
42
55
  if (spinner) spinner.setText('recordsAdded%s%d%d', schema.name, result.success, items.length)
43
56
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dobo",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "Database ORM/ODM for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,20 +1,24 @@
1
1
  import path from 'path'
2
2
 
3
3
  async function attachment (req, reply) {
4
- const { isString } = this.lib._
4
+ const { isString, isEmpty } = this.lib._
5
5
  const { importPkg, getPluginDataDir, pascalCase } = this.app.bajo
6
6
  const { routePath } = this.app.waibu
7
7
  const mime = await importPkg('waibu:mime')
8
- const { fs } = this.lib
9
- const file = `${getPluginDataDir('dobo')}/attachment/${pascalCase(req.params.model)}/${req.params.id}/${req.params.field}/${req.params.file}`
10
- const mimeType = mime.getType(path.extname(file))
8
+ const { fs, fastGlob } = this.lib
9
+ let file = `${getPluginDataDir('dobo')}/attachment/${pascalCase(req.params.model)}/${req.params.id}/${req.params.field}/${req.params.file}`
10
+ if (path.basename(file) === '_first') {
11
+ const files = await fastGlob(`${path.dirname(file)}/*`)
12
+ if (files.length > 0) file = files[0]
13
+ }
14
+ const mimeType = mime.getType(path.extname(file)) ?? ''
11
15
  if (!fs.existsSync(file)) {
12
16
  if (!req.query.notfound) throw this.error('_notFound', { noContent: true })
13
17
  const [, ext] = mimeType.split('/')
14
- const replacer = isString(req.query.notfound) ? req.query.notfound : `waibuStatic.asset:/not-found.${ext}`
18
+ const replacer = isString(req.query.notfound) ? req.query.notfound : `waibuStatic.asset:/not-found.${ext ?? 'png'}`
15
19
  return reply.redirectTo(routePath(replacer))
16
20
  }
17
- reply.header('Content-Type', mimeType)
21
+ if (!isEmpty(mimeType)) reply.header('Content-Type', mimeType)
18
22
  const stream = fs.createReadStream(file)
19
23
  reply.send(stream)
20
24
  return reply