idea-aws 4.0.14 → 4.1.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.
package/dist/src/s3.d.ts CHANGED
@@ -22,14 +22,12 @@ export declare class S3 {
22
22
  createDownloadURLFromData(data: BodyDataTypes, options?: CreateDownloadURLFromDataOptions): Promise<SignedURL>;
23
23
  /**
24
24
  * Get a signed URL to put a file on a S3 bucket.
25
- * @param expires seconds after which the signed URL expires
26
25
  */
27
- signedURLPut(bucket: string, key: string, expires?: number): Promise<SignedURL>;
26
+ signedURLPut(bucket: string, key: string, options?: SignedURLOptions): Promise<SignedURL>;
28
27
  /**
29
28
  * Get a signed URL to get a file on a S3 bucket.
30
- * @param expires seconds after which the signed URL expires
31
29
  */
32
- signedURLGet(bucket: string, key: string, expires?: number): Promise<SignedURL>;
30
+ signedURLGet(bucket: string, key: string, options?: SignedURLOptions): Promise<SignedURL>;
33
31
  /**
34
32
  * Make a copy of an object of the bucket.
35
33
  */
@@ -83,6 +81,25 @@ export interface CreateDownloadURLFromDataOptions {
83
81
  * Seconds to URL expiration; default: `180`.
84
82
  */
85
83
  secToExp?: number;
84
+ /**
85
+ * The suggested name for the file once it's downloaded/saved.
86
+ * Note: the string is cleaned to ensure maximum compatibility with every OS.
87
+ */
88
+ filename?: string;
89
+ }
90
+ /**
91
+ * Options for generating a signed URL.
92
+ */
93
+ export interface SignedURLOptions {
94
+ /**
95
+ * Seconds to URL expiration; default: `180` for GET, `300` for PUT.
96
+ */
97
+ secToExp?: number;
98
+ /**
99
+ * The suggested name for the file once it's downloaded/saved.
100
+ * Note: the string is cleaned to ensure maximum compatibility with every OS.
101
+ */
102
+ filename?: string;
86
103
  }
87
104
  /**
88
105
  * Options for copying an object.
@@ -117,6 +134,11 @@ export interface GetObjectOptions {
117
134
  * Enum: JSON; useful to cast the result.
118
135
  */
119
136
  type?: GetObjectTypes;
137
+ /**
138
+ * The suggested name for the file once it's downloaded/saved.
139
+ * Note: the string is cleaned to ensure maximum compatibility with every OS.
140
+ */
141
+ filename?: string;
120
142
  }
121
143
  /**
122
144
  * The managed types to convert objects coming from an S3 bucket.
@@ -153,6 +175,11 @@ export interface PutObjectOptions {
153
175
  * A set of metadata as attributes
154
176
  */
155
177
  metadata?: any;
178
+ /**
179
+ * The suggested name for the file once it's downloaded/saved.
180
+ * Note: the string is cleaned to ensure maximum compatibility with every OS.
181
+ */
182
+ filename?: string;
156
183
  }
157
184
  /**
158
185
  * Options for deleting an object.
@@ -180,3 +207,7 @@ export interface ListObjectsOptions {
180
207
  */
181
208
  prefix?: string;
182
209
  }
210
+ /**
211
+ * Clean a filename to be compatible with most OS.
212
+ */
213
+ export declare const cleanFilename: (filename: string) => string;
package/dist/src/s3.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.GetObjectTypes = exports.S3 = void 0;
26
+ exports.cleanFilename = exports.GetObjectTypes = exports.S3 = void 0;
27
27
  const AWSS3 = __importStar(require("@aws-sdk/client-s3"));
28
28
  const lib_storage_1 = require("@aws-sdk/lib-storage");
29
29
  const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
@@ -57,26 +57,28 @@ class S3 {
57
57
  const params = { Bucket: options.bucket, Key: options.key, Body: data, ContentType: options.contentType };
58
58
  const upload = new lib_storage_1.Upload({ client: this.s3, params });
59
59
  await upload.done();
60
- return this.signedURLGet(options.bucket, options.key, options.secToExp);
60
+ return this.signedURLGet(options.bucket, options.key, { secToExp: options.secToExp, filename: options.filename });
61
61
  }
62
62
  /**
63
63
  * Get a signed URL to put a file on a S3 bucket.
64
- * @param expires seconds after which the signed URL expires
65
64
  */
66
- async signedURLPut(bucket, key, expires) {
67
- const putCommand = new AWSS3.PutObjectCommand({ Bucket: bucket, Key: key });
68
- const expiresIn = expires || this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP;
69
- const url = await (0, s3_request_presigner_1.getSignedUrl)(this.s3, putCommand, { expiresIn });
65
+ async signedURLPut(bucket, key, options) {
66
+ const putParams = { Bucket: bucket, Key: key };
67
+ if (options.filename)
68
+ putParams.ContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
69
+ const expiresIn = options.secToExp || this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP;
70
+ const url = await (0, s3_request_presigner_1.getSignedUrl)(this.s3, new AWSS3.PutObjectCommand(putParams), { expiresIn });
70
71
  return new idea_toolbox_1.SignedURL({ url });
71
72
  }
72
73
  /**
73
74
  * Get a signed URL to get a file on a S3 bucket.
74
- * @param expires seconds after which the signed URL expires
75
75
  */
76
- async signedURLGet(bucket, key, expires) {
77
- const getCommand = new AWSS3.GetObjectCommand({ Bucket: bucket, Key: key });
78
- const expiresIn = expires || this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP;
79
- const url = await (0, s3_request_presigner_1.getSignedUrl)(this.s3, getCommand, { expiresIn });
76
+ async signedURLGet(bucket, key, options) {
77
+ const getParams = { Bucket: bucket, Key: key };
78
+ if (options.filename)
79
+ getParams.ResponseContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
80
+ const expiresIn = options.secToExp || this.DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP;
81
+ const url = await (0, s3_request_presigner_1.getSignedUrl)(this.s3, new AWSS3.GetObjectCommand(getParams), { expiresIn });
80
82
  return new idea_toolbox_1.SignedURL({ url });
81
83
  }
82
84
  /**
@@ -96,7 +98,10 @@ class S3 {
96
98
  */
97
99
  async getObject(options) {
98
100
  this.logger.debug(`S3 get object: ${options.key}`);
99
- const command = new AWSS3.GetObjectCommand({ Bucket: options.bucket, Key: options.key });
101
+ const params = { Bucket: options.bucket, Key: options.key };
102
+ if (!options.type && options.filename)
103
+ params.ResponseContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
104
+ const command = new AWSS3.GetObjectCommand(params);
100
105
  const result = await this.s3.send(command);
101
106
  switch (options.type) {
102
107
  case GetObjectTypes.JSON:
@@ -118,6 +123,8 @@ class S3 {
118
123
  params.ACL = options.acl;
119
124
  if (options.metadata)
120
125
  params.Metadata = options.metadata;
126
+ if (options.filename)
127
+ params.ContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
121
128
  this.logger.debug(`S3 put object: ${options.key}`);
122
129
  return await this.s3.send(new AWSS3.PutObjectCommand(params));
123
130
  }
@@ -167,3 +174,8 @@ var GetObjectTypes;
167
174
  GetObjectTypes["JSON"] = "JSON";
168
175
  GetObjectTypes["TEXT"] = "TEXT";
169
176
  })(GetObjectTypes || (exports.GetObjectTypes = GetObjectTypes = {}));
177
+ /**
178
+ * Clean a filename to be compatible with most OS.
179
+ */
180
+ const cleanFilename = (filename) => filename.replace(/[^a-z0-9-.\s]/gi, '_');
181
+ exports.cleanFilename = cleanFilename;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "4.0.14",
3
+ "version": "4.1.0",
4
4
  "description": "AWS wrappers to use in IDEA's back-ends",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",