idea-aws 4.4.14 → 4.4.16

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,6 +1,6 @@
1
1
  import { SignedURL } from 'idea-toolbox';
2
2
  import { DynamoDB } from './dynamoDB';
3
- import { S3 } from './s3';
3
+ import { S3, SignedURLOptions } from './s3';
4
4
  /**
5
5
  * A custom class that takes advantage of DynamoDB and S3 to easily manage attachments.
6
6
  */
@@ -8,20 +8,42 @@ export declare class Attachments {
8
8
  protected ddb: DynamoDB;
9
9
  protected s3: S3;
10
10
  /**
11
- * The bucket where from to retrieve the attachments. Fallback to IDEA's default one.
11
+ * The bucket where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
12
12
  */
13
- protected S3_ATTACHMENTS_BUCKET: string;
13
+ bucket: string;
14
14
  /**
15
- * The prefix for attachment IDs. Fallback to IDEA's default one.
15
+ * The folder where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
16
16
  */
17
- protected IUID_ATTACHMENTS_PREFIX: string;
18
- constructor(ddb: DynamoDB, s3: S3);
17
+ folder: string;
19
18
  /**
20
- * Get a signedURL to put an attachment.
19
+ * The default prefix for attachment IDs.
21
20
  */
22
- put(project: string, teamId: string): Promise<SignedURL>;
21
+ prefix: string;
22
+ constructor(ddb: DynamoDB, s3: S3, options?: AttachmentsInitOptions);
23
23
  /**
24
- * Get a signedURL to retrieve an attachment.
24
+ * Get a `SignedURL` to upload an attachment.
25
25
  */
26
- get(attachmentId: string): Promise<SignedURL>;
26
+ put(options?: AttachmentsOptions): Promise<SignedURL>;
27
+ /**
28
+ * Get a `SignedURL` to download an attachment.
29
+ */
30
+ get(attachmentId: string, options?: AttachmentsOptions): Promise<SignedURL>;
31
+ }
32
+ /**
33
+ * Options when creating a new instance of Attachments.
34
+ */
35
+ export interface AttachmentsInitOptions {
36
+ /**
37
+ * Whether to enable the compatibility mode to older versions (temporary, it will be removed).
38
+ */
39
+ compatibility?: 'v1';
40
+ }
41
+ /**
42
+ * Options when preparing for the upload/download of an attachment.
43
+ */
44
+ export interface AttachmentsOptions extends SignedURLOptions {
45
+ /**
46
+ * The attachment ID prefix, if different from the internal attribute `prefix`.
47
+ */
48
+ prefix?: string;
27
49
  }
@@ -1,37 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Attachments = void 0;
4
+ const genericController_1 = require("./genericController");
4
5
  /**
5
6
  * A custom class that takes advantage of DynamoDB and S3 to easily manage attachments.
6
7
  */
7
8
  class Attachments {
8
- constructor(ddb, s3) {
9
+ constructor(ddb, s3, options = {}) {
9
10
  this.ddb = ddb;
10
11
  this.s3 = s3;
11
12
  /**
12
- * The bucket where from to retrieve the attachments. Fallback to IDEA's default one.
13
+ * The bucket where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
13
14
  */
14
- this.S3_ATTACHMENTS_BUCKET = process.env.S3_ATTACHMENTS_BUCKET ?? 'idea-attachments';
15
+ this.bucket = process.env.S3_BUCKET_MEDIA;
15
16
  /**
16
- * The prefix for attachment IDs. Fallback to IDEA's default one.
17
+ * The folder where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
17
18
  */
18
- this.IUID_ATTACHMENTS_PREFIX = process.env.IUID_ATTACHMENTS_PREFIX ?? 'ATT';
19
+ this.folder = process.env.S3_ATTACHMENTS_FOLDER;
20
+ /**
21
+ * The default prefix for attachment IDs.
22
+ */
23
+ this.prefix = process.env.PROJECT ? process.env.PROJECT.concat('-attachment') : 'attachment';
24
+ if (options.compatibility === 'v1') {
25
+ this.bucket = process.env.S3_ATTACHMENTS_BUCKET || 'idea-attachments';
26
+ this.folder = null;
27
+ this.prefix = 'ATT';
28
+ }
19
29
  }
20
30
  /**
21
- * Get a signedURL to put an attachment.
31
+ * Get a `SignedURL` to upload an attachment.
22
32
  */
23
- async put(project, teamId) {
24
- const attachmentIdPrefix = this.IUID_ATTACHMENTS_PREFIX.concat('_', project, '_', teamId);
25
- const attachmentId = await this.ddb.IUNID(attachmentIdPrefix);
26
- const signedURL = await this.s3.signedURLPut(this.S3_ATTACHMENTS_BUCKET, attachmentId);
33
+ async put(options = {}) {
34
+ const attachmentId = await this.ddb.IUNID(options.prefix || this.prefix);
35
+ const key = this.folder ? `${this.folder}/${attachmentId}` : attachmentId;
36
+ const signedURL = await this.s3.signedURLPut(this.bucket, key);
27
37
  signedURL.id = attachmentId;
28
38
  return signedURL;
29
39
  }
30
40
  /**
31
- * Get a signedURL to retrieve an attachment.
41
+ * Get a `SignedURL` to download an attachment.
32
42
  */
33
- async get(attachmentId) {
34
- const signedURL = await this.s3.signedURLGet(this.S3_ATTACHMENTS_BUCKET, attachmentId);
43
+ async get(attachmentId, options = {}) {
44
+ if (!attachmentId)
45
+ throw new genericController_1.HandledError('Missing attachment ID');
46
+ if (!attachmentId.startsWith(options.prefix || this.prefix))
47
+ throw new genericController_1.HandledError('Not found');
48
+ const key = this.folder ? `${this.folder}/${attachmentId}` : attachmentId;
49
+ const signedURL = await this.s3.signedURLGet(this.bucket, key);
35
50
  signedURL.id = attachmentId;
36
51
  return signedURL;
37
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "4.4.14",
3
+ "version": "4.4.16",
4
4
  "description": "AWS wrappers to use in IDEA's back-ends",
5
5
  "license": "MPL-2.0",
6
6
  "main": "dist/index.js",