@things-factory/attachment-base 5.0.7 → 6.0.0-alpha.0

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.
Files changed (37) hide show
  1. package/dist-server/attachment-const.js +2 -1
  2. package/dist-server/attachment-const.js.map +1 -1
  3. package/dist-server/awb-storage-s3.js +42 -0
  4. package/dist-server/awb-storage-s3.js.map +1 -0
  5. package/dist-server/index.js +6 -18
  6. package/dist-server/index.js.map +1 -1
  7. package/dist-server/migrations/index.js.map +1 -1
  8. package/dist-server/routes.js.map +1 -1
  9. package/dist-server/service/attachment/attachment-mutation.js +63 -75
  10. package/dist-server/service/attachment/attachment-mutation.js.map +1 -1
  11. package/dist-server/service/attachment/attachment-query.js +36 -48
  12. package/dist-server/service/attachment/attachment-query.js.map +1 -1
  13. package/dist-server/service/attachment/attachment-types.js +40 -52
  14. package/dist-server/service/attachment/attachment-types.js.map +1 -1
  15. package/dist-server/service/attachment/attachment.js +41 -50
  16. package/dist-server/service/attachment/attachment.js.map +1 -1
  17. package/dist-server/service/attachment/index.js.map +1 -1
  18. package/dist-server/service/index.js +3 -16
  19. package/dist-server/service/index.js.map +1 -1
  20. package/dist-server/storage-file.js +3 -25
  21. package/dist-server/storage-file.js.map +1 -1
  22. package/dist-server/storage-s3.js.map +1 -1
  23. package/dist-server/tsconfig.tsbuildinfo +1 -0
  24. package/dist-server/util/index.js +5 -0
  25. package/dist-server/util/index.js.map +1 -0
  26. package/dist-server/util/upload-awb.js +12 -0
  27. package/dist-server/util/upload-awb.js.map +1 -0
  28. package/package.json +5 -5
  29. package/server/attachment-const.ts +1 -0
  30. package/server/awb-storage-s3.ts +44 -0
  31. package/server/index.ts +1 -0
  32. package/server/service/attachment/attachment-mutation.ts +25 -19
  33. package/server/service/attachment/attachment-query.ts +9 -8
  34. package/server/service/attachment/attachment-types.ts +1 -1
  35. package/server/service/attachment/attachment.ts +2 -2
  36. package/server/util/index.ts +1 -0
  37. package/server/util/upload-awb.ts +11 -0
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./upload-awb"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../server/util/index.ts"],"names":[],"mappings":";;;AAAA,uDAA4B","sourcesContent":["export * from './upload-awb'"]}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uploadAwb = void 0;
4
+ require("../awb-storage-s3");
5
+ const attachment_const_1 = require("../attachment-const");
6
+ async function uploadAwb(param) {
7
+ const { content, title } = param;
8
+ let result = await attachment_const_1.AWBSTORAGE.uploadFile({ stream: content, filename: title });
9
+ return result;
10
+ }
11
+ exports.uploadAwb = uploadAwb;
12
+ //# sourceMappingURL=upload-awb.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-awb.js","sourceRoot":"","sources":["../../server/util/upload-awb.ts"],"names":[],"mappings":";;;AAAA,6BAA0B;AAE1B,0DAAgD;AAEzC,KAAK,UAAU,SAAS,CAAC,KAAU;IACxC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,KAAK,CAAA;IAEhC,IAAI,MAAM,GAAG,MAAM,6BAAU,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAA;IAE9E,OAAO,MAAM,CAAA;AACf,CAAC;AAND,8BAMC","sourcesContent":["import '../awb-storage-s3'\n\nimport { AWBSTORAGE } from '../attachment-const'\n\nexport async function uploadAwb(param: any) {\n const { content, title } = param\n\n let result = await AWBSTORAGE.uploadFile({ stream: content, filename: title })\n\n return result\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/attachment-base",
3
- "version": "5.0.7",
3
+ "version": "6.0.0-alpha.0",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -28,12 +28,12 @@
28
28
  "@aws-sdk/lib-storage": "^3.46.0",
29
29
  "@aws-sdk/s3-presigned-post": "^3.46.0",
30
30
  "@koa/multer": "^3.0.0",
31
- "@things-factory/auth-base": "^5.0.7",
32
- "@things-factory/env": "^5.0.0",
33
- "@things-factory/shell": "^5.0.7",
31
+ "@things-factory/auth-base": "^6.0.0-alpha.0",
32
+ "@things-factory/env": "^6.0.0-alpha.0",
33
+ "@things-factory/shell": "^6.0.0-alpha.0",
34
34
  "memfs": "^3.0.1",
35
35
  "mime": "^2.4.4",
36
36
  "multer": "^1.4.5-lts.1"
37
37
  },
38
- "gitHead": "1c7c7618e2e3c9ef8b07d730c3df84c8bc01b35f"
38
+ "gitHead": "e5b9823ab3ebc5d6ae5bcb4b5aa60c1306e4dd7e"
39
39
  }
@@ -1,4 +1,5 @@
1
1
  import { config } from '@things-factory/env'
2
2
 
3
3
  export var STORAGE: any = config.get('storage')
4
+ export var AWBSTORAGE: any = config.get('awbFileStorage')
4
5
  export const ATTACHMENT_PATH: string = config.get('attachmentPath', 'attachment')
@@ -0,0 +1,44 @@
1
+ import { S3Client } from '@aws-sdk/client-s3'
2
+ import { Upload } from '@aws-sdk/lib-storage'
3
+ import { logger } from '@things-factory/env'
4
+
5
+ import { AWBSTORAGE } from './attachment-const'
6
+
7
+ if (AWBSTORAGE && AWBSTORAGE.type == 's3') {
8
+ const client = new S3Client({
9
+ credentials: {
10
+ accessKeyId: AWBSTORAGE.accessKeyId,
11
+ secretAccessKey: AWBSTORAGE.secretAccessKey
12
+ },
13
+ region: AWBSTORAGE.region
14
+ })
15
+
16
+ /* upload file */
17
+ AWBSTORAGE.uploadFile = async ({ stream, filename }) => {
18
+ const upload = new Upload({
19
+ client,
20
+ params: {
21
+ Bucket: AWBSTORAGE.bucketName,
22
+ Key: `${filename}.pdf`,
23
+ Body: stream,
24
+ ContentType: 'application/pdf'
25
+ }
26
+ })
27
+
28
+ let result
29
+ let url
30
+ try {
31
+ result = (await upload.done()) as any
32
+ url = `https://${AWBSTORAGE.bucketName}.s3.${AWBSTORAGE.region}.amazonaws.com/${filename}.pdf`
33
+ } catch (e) {
34
+ console.log(e)
35
+ }
36
+
37
+ return {
38
+ result,
39
+ url
40
+ }
41
+ }
42
+
43
+ logger.info('operato-awb: S3 Bucket Storage is Ready.')
44
+ }
package/server/index.ts CHANGED
@@ -13,3 +13,4 @@ export {
13
13
  import './routes'
14
14
 
15
15
  export * from './attachment-const'
16
+ export * from './util'
@@ -2,9 +2,10 @@ import type { FileUpload } from 'graphql-upload/GraphQLUpload.js'
2
2
  import GraphQLUpload from 'graphql-upload/GraphQLUpload.js'
3
3
  import promisesAll from 'promises-all'
4
4
  import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
5
- import { getRepository, In } from 'typeorm'
5
+ import { In } from 'typeorm'
6
6
 
7
7
  import { logger } from '@things-factory/env'
8
+ import { getRepository } from '@things-factory/shell'
8
9
 
9
10
  import { STORAGE } from '../../attachment-const'
10
11
  import { Attachment } from './attachment'
@@ -16,7 +17,7 @@ export class AttachmentMutation {
16
17
  @Mutation(returns => Attachment)
17
18
  async createAttachment(
18
19
  @Arg('attachment', type => NewAttachment) attachment: NewAttachment,
19
- @Ctx() context: any
20
+ @Ctx() context: ResolverContext
20
21
  ): Promise<Attachment> {
21
22
  return await createAttachment(null, { attachment }, context)
22
23
  }
@@ -25,7 +26,7 @@ export class AttachmentMutation {
25
26
  @Mutation(returns => [Attachment])
26
27
  async createAttachments(
27
28
  @Arg('attachments', type => [NewAttachment]) attachments: NewAttachment[],
28
- @Ctx() context: any
29
+ @Ctx() context: ResolverContext
29
30
  ): Promise<Attachment[]> {
30
31
  return await createAttachments(null, { attachments }, context)
31
32
  }
@@ -35,11 +36,11 @@ export class AttachmentMutation {
35
36
  async updateAttachment(
36
37
  @Arg('id') id: string,
37
38
  @Arg('patch', type => AttachmentPatch) patch: AttachmentPatch,
38
- @Ctx() context: any
39
+ @Ctx() context: ResolverContext
39
40
  ): Promise<Attachment> {
40
41
  const attachment = await getRepository(Attachment).findOne({
41
42
  where: {
42
- domain: context.state.domain,
43
+ domain: { id: context.state.domain.id },
43
44
  id
44
45
  }
45
46
  })
@@ -53,7 +54,7 @@ export class AttachmentMutation {
53
54
 
54
55
  @Directive('@transaction')
55
56
  @Mutation(returns => Boolean)
56
- async deleteAttachment(@Arg('id') id: string, @Ctx() context: any): Promise<boolean> {
57
+ async deleteAttachment(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<boolean> {
57
58
  return await deleteAttachment(null, { id }, context)
58
59
  }
59
60
 
@@ -61,14 +62,17 @@ export class AttachmentMutation {
61
62
  @Mutation(returns => Boolean)
62
63
  async deleteAttachmentsByRef(
63
64
  @Arg('refBys', type => [String]) refBys: string[],
64
- @Ctx() context: any
65
+ @Ctx() context: ResolverContext
65
66
  ): Promise<boolean> {
66
67
  return await deleteAttachmentsByRef(null, { refBys }, context)
67
68
  }
68
69
 
69
70
  @Directive('@transaction')
70
71
  @Mutation(returns => Attachment)
71
- async singleUpload(@Arg('file', type => GraphQLUpload) file: FileUpload, @Ctx() context: any): Promise<Attachment> {
72
+ async singleUpload(
73
+ @Arg('file', type => GraphQLUpload) file: FileUpload,
74
+ @Ctx() context: ResolverContext
75
+ ): Promise<Attachment> {
72
76
  return await singleUpload(null, { file }, context)
73
77
  }
74
78
 
@@ -76,7 +80,7 @@ export class AttachmentMutation {
76
80
  @Mutation(returns => [Attachment])
77
81
  async multipleUpload(
78
82
  @Arg('files', type => [GraphQLUpload]) files: FileUpload[],
79
- @Ctx() context: any
83
+ @Ctx() context: ResolverContext
80
84
  ): Promise<Attachment[]> {
81
85
  return await multipleUpload(null, { files }, context)
82
86
  }
@@ -84,13 +88,13 @@ export class AttachmentMutation {
84
88
  @Mutation(returns => UploadURL)
85
89
  async generateUploadURL(
86
90
  @Arg('type', type => String) type: string,
87
- @Ctx() context: any
91
+ @Ctx() context: ResolverContext
88
92
  ): Promise<{ url: string; fields: { [key: string]: string } }> {
89
93
  return await generateUploadURL(null, { type }, context)
90
94
  }
91
95
  }
92
96
 
93
- export async function createAttachment(_: any, { attachment }, context: any): Promise<Attachment> {
97
+ export async function createAttachment(_: any, { attachment }, context: ResolverContext): Promise<Attachment> {
94
98
  const { file, category, refType = '', refBy, description } = attachment
95
99
  const { createReadStream, filename, mimetype, encoding } = await file
96
100
  const stream = createReadStream()
@@ -115,7 +119,7 @@ export async function createAttachment(_: any, { attachment }, context: any): Pr
115
119
  })
116
120
  }
117
121
 
118
- export async function createAttachments(_: any, { attachments }, context: any): Promise<Attachment[]> {
122
+ export async function createAttachments(_: any, { attachments }, context: ResolverContext): Promise<Attachment[]> {
119
123
  const { resolve, reject } = await promisesAll.all(
120
124
  attachments.map(attachment => createAttachment(_, { attachment }, context))
121
125
  )
@@ -129,10 +133,12 @@ export async function createAttachments(_: any, { attachments }, context: any):
129
133
  return resolve
130
134
  }
131
135
 
132
- export async function deleteAttachment(_: any, { id }, context: any): Promise<boolean> {
136
+ export async function deleteAttachment(_: any, { id }, context: ResolverContext): Promise<boolean> {
137
+ const { domain } = context.state
138
+
133
139
  const repository = getRepository(Attachment)
134
140
  const attachment = await repository.findOne({
135
- where: { domain: context.state.domain, id }
141
+ where: { domain: { id: domain.id }, id }
136
142
  })
137
143
 
138
144
  if (attachment) {
@@ -144,12 +150,12 @@ export async function deleteAttachment(_: any, { id }, context: any): Promise<bo
144
150
  }
145
151
  }
146
152
 
147
- export async function deleteAttachmentsByRef(_: any, { refBys }, context: any): Promise<boolean> {
153
+ export async function deleteAttachmentsByRef(_: any, { refBys }, context: ResolverContext): Promise<boolean> {
148
154
  const { domain } = context.state
149
155
 
150
156
  const repository = getRepository(Attachment)
151
157
  const attachments = await repository.find({
152
- where: { domain, refBy: In(refBys) }
158
+ where: { domain: { id: domain.id }, refBy: In(refBys) }
153
159
  })
154
160
 
155
161
  //remove attachment from repo
@@ -174,15 +180,15 @@ export async function deleteAttachmentsByRef(_: any, { refBys }, context: any):
174
180
  export async function generateUploadURL(
175
181
  _: any,
176
182
  { type },
177
- context: any
183
+ context: ResolverContext
178
184
  ): Promise<{ url: string; fields: { [key: string]: string } }> {
179
185
  return await STORAGE.generateUploadURL(type)
180
186
  }
181
187
 
182
- export async function singleUpload(_: any, { file }, context: any): Promise<Attachment> {
188
+ export async function singleUpload(_: any, { file }, context: ResolverContext): Promise<Attachment> {
183
189
  return await createAttachment(null, { attachment: { file } }, context)
184
190
  }
185
191
 
186
- export async function multipleUpload(_: any, { files }, context: any): Promise<Attachment[]> {
192
+ export async function multipleUpload(_: any, { files }, context: ResolverContext): Promise<Attachment[]> {
187
193
  return await createAttachments(null, { attachments: { files } }, context)
188
194
  }
@@ -1,8 +1,7 @@
1
1
  import { Arg, Args, Ctx, FieldResolver, Query, Resolver, Root } from 'type-graphql'
2
- import { getRepository } from 'typeorm'
3
2
 
4
3
  import { User } from '@things-factory/auth-base'
5
- import { Domain, getQueryBuilderFromListParams, ListParam } from '@things-factory/shell'
4
+ import { Domain, getQueryBuilderFromListParams, getRepository, ListParam } from '@things-factory/shell'
6
5
 
7
6
  import { AttachmentList } from '../'
8
7
  import { Attachment } from './attachment'
@@ -10,7 +9,7 @@ import { Attachment } from './attachment'
10
9
  @Resolver(Attachment)
11
10
  export class AttachmentQuery {
12
11
  @Query(returns => AttachmentList)
13
- async attachments(@Ctx() context: any, @Args() params: ListParam): Promise<AttachmentList> {
12
+ async attachments(@Ctx() context: ResolverContext, @Args() params: ListParam): Promise<AttachmentList> {
14
13
  const { domain } = context.state
15
14
 
16
15
  const queryBuilder = getQueryBuilderFromListParams({
@@ -26,30 +25,32 @@ export class AttachmentQuery {
26
25
  }
27
26
 
28
27
  @Query(returns => Attachment)
29
- async attachment(@Arg('id') id: string, @Ctx() context: any): Promise<Attachment> {
28
+ async attachment(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<Attachment> {
29
+ const { domain } = context.state
30
+
30
31
  return await getRepository(Attachment).findOne({
31
- where: { domain: context.state.domain, id },
32
+ where: { domain: { id: domain.id }, id },
32
33
  relations: ['domain', 'creator', 'updater']
33
34
  })
34
35
  }
35
36
 
36
37
  @FieldResolver(type => Domain)
37
38
  async domain(@Root() attachment: Attachment) {
38
- return await getRepository(Domain).findOne({
39
+ return await getRepository(Domain).findOneBy({
39
40
  id: attachment.domainId
40
41
  })
41
42
  }
42
43
 
43
44
  @FieldResolver(type => User)
44
45
  async updater(@Root() attachment: Attachment): Promise<User> {
45
- return await getRepository(User).findOne({
46
+ return await getRepository(User).findOneBy({
46
47
  id: attachment.updaterId
47
48
  })
48
49
  }
49
50
 
50
51
  @FieldResolver(type => User)
51
52
  async creator(@Root() attachment: Attachment): Promise<User> {
52
- return await getRepository(User).findOne({
53
+ return await getRepository(User).findOneBy({
53
54
  id: attachment.creatorId
54
55
  })
55
56
  }
@@ -21,7 +21,7 @@ export class UploadURL {
21
21
  url: string
22
22
 
23
23
  @Field(type => ScalarAny)
24
- fields: ScalarAny
24
+ fields: any
25
25
  }
26
26
 
27
27
  @InputType()
@@ -61,11 +61,11 @@ export class Attachment {
61
61
 
62
62
  @Column({ nullable: true, default: '' })
63
63
  @Field({ nullable: true })
64
- refType: String = ''
64
+ refType: string = ''
65
65
 
66
66
  @Column({ nullable: true })
67
67
  @Field({ nullable: true })
68
- refBy: String
68
+ refBy: string
69
69
 
70
70
  @Column()
71
71
  @Field()
@@ -0,0 +1 @@
1
+ export * from './upload-awb'
@@ -0,0 +1,11 @@
1
+ import '../awb-storage-s3'
2
+
3
+ import { AWBSTORAGE } from '../attachment-const'
4
+
5
+ export async function uploadAwb(param: any) {
6
+ const { content, title } = param
7
+
8
+ let result = await AWBSTORAGE.uploadFile({ stream: content, filename: title })
9
+
10
+ return result
11
+ }