@things-factory/attachment-base 6.0.7 → 6.0.18

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": "@things-factory/attachment-base",
3
- "version": "6.0.7",
3
+ "version": "6.0.18",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -27,12 +27,12 @@
27
27
  "@aws-sdk/lib-storage": "^3.46.0",
28
28
  "@aws-sdk/s3-presigned-post": "^3.46.0",
29
29
  "@koa/multer": "^3.0.0",
30
- "@things-factory/auth-base": "^6.0.7",
30
+ "@things-factory/auth-base": "^6.0.18",
31
31
  "@things-factory/env": "^6.0.7",
32
- "@things-factory/shell": "^6.0.7",
32
+ "@things-factory/shell": "^6.0.18",
33
33
  "memfs": "^3.0.1",
34
34
  "mime": "^2.4.4",
35
35
  "multer": "^1.4.5-lts.1"
36
36
  },
37
- "gitHead": "5af6d8613bd6eab6feaa69df942ef3ddfae764fe"
37
+ "gitHead": "2e3e970ad20b2d7b67653ce04341bdf150ae7ebc"
38
38
  }
package/server/routes.ts CHANGED
@@ -6,6 +6,8 @@ import { ATTACHMENT_PATH, STORAGE } from './attachment-const'
6
6
  const multer = require('@koa/multer')
7
7
  const upload = multer() // note you can pass `multer` options here
8
8
 
9
+ const { Readable } = require('stream')
10
+
9
11
  // process.on('bootstrap-module-domain-private-route' as any, (app, routes) => {
10
12
  process.on('bootstrap-module-global-public-route' as any, (app, routes) => {
11
13
  // TODO make this secure
@@ -13,11 +15,16 @@ process.on('bootstrap-module-global-public-route' as any, (app, routes) => {
13
15
  await STORAGE.sendFile(context, context.params.attachment, next)
14
16
  })
15
17
 
16
- routes.post(`/${ATTACHMENT_PATH}`, async (context, next) => {
17
- const { files } = await upload(context.request)
18
+ routes.post(`/${ATTACHMENT_PATH}`, upload.any(), async (context, next) => {
19
+ const files = context.files
18
20
 
19
21
  const result: { id: string; path: string; size: number }[] = await Promise.all(
20
- files.map(file => STORAGE.uploadFile({ stream: file, filename: file.filename }))
22
+ files.map(file =>
23
+ STORAGE.uploadFile({
24
+ stream: Readable.from(file.buffer),
25
+ filename: Buffer.from(file.originalname, 'latin1').toString('utf-8') /* Because busboy uses latin1 encoding */
26
+ })
27
+ )
21
28
  )
22
29
 
23
30
  context.status = 200
@@ -96,8 +96,9 @@ export class AttachmentMutation {
96
96
 
97
97
  export async function createAttachment(_: any, { attachment }, context: ResolverContext): Promise<Attachment> {
98
98
  const { file, category, refType = '', refBy, description } = attachment
99
- const { createReadStream, filename, mimetype, encoding } = await file
99
+ var { createReadStream, filename, mimetype, encoding } = await file
100
100
  const stream = createReadStream()
101
+ filename = Buffer.from(filename, 'latin1').toString('utf-8') /* Because busboy uses latin1 encoding */
101
102
 
102
103
  const { id, path, size } = await STORAGE.uploadFile({ stream, filename })
103
104
  const { domain, user, tx } = context.state
@@ -29,9 +29,10 @@ if (STORAGE && STORAGE.type == 'file') {
29
29
  fs.unlinkSync(path)
30
30
  reject(error)
31
31
  })
32
+ .on('data', chunk => {
33
+ size += chunk.length
34
+ })
32
35
  .pipe(fs.createWriteStream(path))
33
- .on('error', error => reject(error))
34
- .on('data', chunk => (size += chunk.length))
35
36
  .on('finish', () => resolve({ id, path: relativePath, size }))
36
37
  )
37
38
  }