q-koa 8.5.4 → 8.6.1

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/core/app.js CHANGED
@@ -1835,7 +1835,7 @@ APP.getClientTypeByCtx = (ctx) => {
1835
1835
 
1836
1836
  APP.getConfig = (app) => ({
1837
1837
  async getObject(type) {
1838
- let configList = await app.cache.get('configList')
1838
+ let configList = app.cache.get('configList')
1839
1839
  if (!configList) {
1840
1840
  configList = await app.model.setting.findAll({
1841
1841
  raw: true,
@@ -10,6 +10,7 @@ const axios = require('axios')
10
10
  const util = require('util')
11
11
  const jwt = require('jsonwebtoken')
12
12
 
13
+ const { getImageType } = require('../../utils')
13
14
  const Email = require('../../services/email')
14
15
  const Geo = require('../../services/geo')
15
16
  const Express = require('../../services/express')
@@ -419,3 +420,57 @@ exports.express = async (ctx) => {
419
420
 
420
421
  ctx.SUCCESS(result)
421
422
  }
423
+
424
+ exports.urlToOss = async (ctx) => {
425
+ const { app, appName } = getAppByCtx(ctx)
426
+ const appConfig = getConfig(app)
427
+ const { is_dev } = await appConfig.getObject('base')
428
+ const { url } = ctx.request.body
429
+ const {
430
+ accessKeyId,
431
+ accessKeySecret,
432
+ bucket,
433
+ region,
434
+ cdn_host,
435
+ oss_host,
436
+ } = await appConfig.getObject('oss')
437
+
438
+ if (!url || ['aliyun', cdn_host, oss_host].some((i) => url.includes(i)))
439
+ return ctx.SUCCESS(url)
440
+
441
+ if (!(accessKeyId && accessKeySecret && bucket && region)) {
442
+ throw new Error('没有配置oss设置')
443
+ }
444
+
445
+ const client = new OSS({
446
+ accessKeyId,
447
+ accessKeySecret,
448
+ bucket,
449
+ region,
450
+ secure: true,
451
+ })
452
+ const buffer = await axios
453
+ .get(url, {
454
+ responseType: 'arraybuffer',
455
+ })
456
+ .then((res) => res.data)
457
+
458
+ const type = getImageType(buffer)
459
+ console.log('type', type)
460
+ const fileName = `${
461
+ String(new Date().getTime()).split('').reverse().join('') + Math.random()
462
+ }${type}`
463
+
464
+ const result = await client.put(fileName, buffer)
465
+
466
+ if (result && result.res && result.res.status === 200) {
467
+ if (oss_host && cdn_host) {
468
+ const url = result.url.replace(oss_host, cdn_host)
469
+ return ctx.SUCCESS(url)
470
+ } else {
471
+ return ctx.SUCCESS(result.url)
472
+ }
473
+ } else {
474
+ throw new Error('上传失败')
475
+ }
476
+ }
@@ -7,6 +7,7 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
7
7
  const appConfig = getConfig(app)
8
8
  const { version: cacheVersion } = await appConfig.getObject('base')
9
9
  const apiVersion = ctx.request.header.version
10
+ const config = ctx.request.header.config
10
11
 
11
12
  if (!(excludes instanceof Array)) {
12
13
  excludes = [excludes]
@@ -218,6 +219,14 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
218
219
  {}
219
220
  )
220
221
  if (obj.setting && apiVersion && cacheVersion) {
222
+ let target = null
223
+ if (config) {
224
+ const application = app.cache.get('application')
225
+ if (application) {
226
+ target = application.find((a) => a.config === config)
227
+ }
228
+ }
229
+
221
230
  return {
222
231
  ...obj,
223
232
  setting: obj.setting.map((i) => {
@@ -227,6 +236,17 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
227
236
  value: apiVersion === cacheVersion,
228
237
  }
229
238
  }
239
+
240
+ if (
241
+ target &&
242
+ target.hasOwnProperty(i.code) &&
243
+ i.hasOwnProperty('value')
244
+ ) {
245
+ return {
246
+ ...(i.toJSON ? i.toJSON() : i),
247
+ value: target[i.code],
248
+ }
249
+ }
230
250
  return i
231
251
  }),
232
252
  }
@@ -404,7 +404,6 @@ module.exports = class Singleton {
404
404
  .join('&')
405
405
  : '',
406
406
  }
407
- const creatuuid = uuid.v1()
408
407
 
409
408
  const bufferResult = await axios
410
409
  .post(url, payLoad, {
@@ -419,7 +418,7 @@ module.exports = class Singleton {
419
418
  if (bufferResult.errcode) {
420
419
  if (bufferResult.errcode === 40001) {
421
420
  cache.reset()
422
- return await this.createQR(options)
421
+ return await this.createQRBuffer(options)
423
422
  }
424
423
  throw new Error(bufferResult.errmsg)
425
424
  }
@@ -17,13 +17,28 @@ exports.getList = (config) => (type) => {
17
17
 
18
18
  exports.getObject = (config, app) => async (type) => {
19
19
  if (app) {
20
- const applicationList = await app.cache.get('application')
20
+ const applicationList = app.cache.get('application')
21
21
  if (
22
22
  applicationList &&
23
23
  applicationList.find((item) => item.config === type)
24
24
  ) {
25
25
  return applicationList.find((item) => item.config === type)
26
26
  }
27
+
28
+ if (!applicationList) {
29
+ if (app.model.application) {
30
+ const result = await app.model.application.findAll()
31
+
32
+ if (result.length > 0) {
33
+ app.cache.set('application', JSON.parse(JSON.stringify(result)))
34
+
35
+ const target = result.find((item) => item.config === type)
36
+ if (target) {
37
+ return target
38
+ }
39
+ }
40
+ }
41
+ }
27
42
  }
28
43
  const obj = config.find((i) => i.code === type)
29
44
  if (!obj) throw new Error(`找不到${type}相关设置`)
@@ -68,3 +83,60 @@ exports.safeSplit = (mainStr, str) => {
68
83
  }
69
84
  return ['', '']
70
85
  }
86
+
87
+ exports.getImageType = (fileBuffer) => {
88
+ // 将上文提到的 文件标识头 按 字节 整理到数组中
89
+ const imageBufferHeaders = [
90
+ { bufBegin: [0xff, 0xd8], bufEnd: [0xff, 0xd9], suffix: '.jpg' },
91
+ { bufBegin: [0x00, 0x00, 0x02, 0x00, 0x00], suffix: '.tga' },
92
+ { bufBegin: [0x00, 0x00, 0x10, 0x00, 0x00], suffix: '.rle' },
93
+ {
94
+ bufBegin: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
95
+ suffix: '.png',
96
+ },
97
+ { bufBegin: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], suffix: '.gif' },
98
+ { bufBegin: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], suffix: '.gif' },
99
+ { bufBegin: [0x42, 0x4d], suffix: '.bmp' },
100
+ { bufBegin: [0x0a], suffix: '.pcx' },
101
+ { bufBegin: [0x49, 0x49], suffix: '.tif' },
102
+ { bufBegin: [0x4d, 0x4d], suffix: '.tif' },
103
+ {
104
+ bufBegin: [0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x20],
105
+ suffix: '.ico',
106
+ },
107
+ {
108
+ bufBegin: [0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x20, 0x20],
109
+ suffix: '.cur',
110
+ },
111
+ { bufBegin: [0x46, 0x4f, 0x52, 0x4d], suffix: '.iff' },
112
+ { bufBegin: [0x52, 0x49, 0x46, 0x46], suffix: '.ani' },
113
+ ]
114
+ for (const imageBufferHeader of imageBufferHeaders) {
115
+ let isEqual
116
+ // 判断标识头前缀
117
+ if (imageBufferHeader.bufBegin) {
118
+ const buf = Buffer.from(imageBufferHeader.bufBegin)
119
+ isEqual = buf.equals(
120
+ //使用 buffer.slice 方法 对 buffer 以字节为单位切割
121
+ fileBuffer.slice(0, imageBufferHeader.bufBegin.length)
122
+ )
123
+ }
124
+ // 判断标识头后缀
125
+ if (isEqual && imageBufferHeader.bufEnd) {
126
+ const buf = Buffer.from(imageBufferHeader.bufEnd)
127
+ isEqual = buf.equals(fileBuffer.slice(-imageBufferHeader.bufEnd.length))
128
+ }
129
+ if (isEqual) {
130
+ if (
131
+ ['.ani', '.iff', '.cur', '.tif', '.pcx', '.rle', '.tga'].includes(
132
+ imageBufferHeader.suffix
133
+ )
134
+ ) {
135
+ return '.jpg'
136
+ }
137
+ return imageBufferHeader.suffix
138
+ }
139
+ }
140
+ // 未能识别到该文件类型
141
+ return ''
142
+ }
@@ -21,12 +21,8 @@ exports.miErrors = () => async (ctx, next) => {
21
21
  try {
22
22
  return await next()
23
23
  } catch (e) {
24
- // app.event.emit('message', {
25
- // type: 'error',
26
- // payload: {
27
- // message: e.message,
28
- // },
29
- // });
24
+ console.log(e.message)
25
+ console.log(e.stack)
30
26
  const errorData = {
31
27
  url: ctx.request.href,
32
28
  body: ctx.request.body,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "8.5.4",
3
+ "version": "8.6.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {