idea-aws 4.4.14 → 4.4.15
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/dist/src/attachments.d.ts +32 -10
- package/dist/src/attachments.js +28 -13
- package/package.json +1 -1
|
@@ -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
|
-
|
|
13
|
+
bucket: string;
|
|
14
14
|
/**
|
|
15
|
-
* The
|
|
15
|
+
* The folder where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
|
|
16
16
|
*/
|
|
17
|
-
|
|
18
|
-
constructor(ddb: DynamoDB, s3: S3);
|
|
17
|
+
folder: string;
|
|
19
18
|
/**
|
|
20
|
-
*
|
|
19
|
+
* The default prefix for attachment IDs.
|
|
21
20
|
*/
|
|
22
|
-
|
|
21
|
+
prefix: string;
|
|
22
|
+
constructor(ddb: DynamoDB, s3: S3, options?: AttachmentsInitOptions);
|
|
23
23
|
/**
|
|
24
|
-
* Get a
|
|
24
|
+
* Get a `SignedURL` to upload an attachment.
|
|
25
25
|
*/
|
|
26
|
-
|
|
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
|
}
|
package/dist/src/attachments.js
CHANGED
|
@@ -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.
|
|
15
|
+
this.bucket = process.env.S3_BUCKET_MEDIA;
|
|
15
16
|
/**
|
|
16
|
-
* The
|
|
17
|
+
* The folder where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
|
|
17
18
|
*/
|
|
18
|
-
this.
|
|
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('_ATT') : 'ATT';
|
|
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
|
|
31
|
+
* Get a `SignedURL` to upload an attachment.
|
|
22
32
|
*/
|
|
23
|
-
async put(
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const signedURL = await this.s3.signedURLPut(this.
|
|
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
|
|
41
|
+
* Get a `SignedURL` to download an attachment.
|
|
32
42
|
*/
|
|
33
|
-
async get(attachmentId) {
|
|
34
|
-
|
|
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
|
}
|