dobo 1.1.4 → 1.1.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dobo",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Database ORM/ODM for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  async function sanitizeBody ({ body = {}, schema = {}, partial, strict, extFields = [] }) {
2
2
  const { isSet, dayjs, callHandler } = this.app.bajo
3
- const { has, isString, isNumber, concat } = this.lib._
3
+ const { has, isString, isNumber, concat, isNaN } = this.lib._
4
4
  const result = {}
5
5
  for (const p of concat(schema.properties, extFields)) {
6
6
  if (partial && !has(body, p.name)) continue
@@ -48,14 +48,15 @@ async function sanitizeBody ({ body = {}, schema = {}, partial, strict, extField
48
48
  }
49
49
  }
50
50
  } else {
51
- if (p.default) {
51
+ if (isSet(p.default)) {
52
52
  result[p.name] = p.default
53
53
  if (isString(p.default) && p.default.startsWith('handler:')) {
54
54
  const [, ...args] = p.default.split(':')
55
55
  if (args.length > 0) result[p.name] = await callHandler(args.join(':'))
56
56
  } else {
57
- if (['float', 'double'].includes(p.type)) result[p.name] = parseFloat(result[p.name]) || null
58
- if (['integer', 'smallint'].includes(p.type)) result[p.name] = parseInt(result[p.name]) || null
57
+ if (['float', 'double'].includes(p.type)) result[p.name] = parseFloat(result[p.name])
58
+ if (['integer', 'smallint'].includes(p.type)) result[p.name] = parseInt(result[p.name])
59
+ if (isNaN(result[p.name])) result[p.name] = null
59
60
  }
60
61
  }
61
62
  }
@@ -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