vtlab-generic-functions 1.0.6 → 1.0.8

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 (2) hide show
  1. package/package.json +1 -1
  2. package/s3bucket/index.js +24 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },
package/s3bucket/index.js CHANGED
@@ -4,6 +4,7 @@ const config = require('../config');
4
4
  const multer = require('multer');
5
5
  const multerS3 = require('multer-s3');
6
6
  const { promisesCollector } = require('../utils');
7
+ const stream = require("stream");
7
8
 
8
9
  /**
9
10
  * Uploads a file to an S3 bucket.
@@ -106,8 +107,25 @@ const downloadFileFromS3 = async (path) => {
106
107
  Key: path
107
108
  });
108
109
 
109
- const data = await s3Client.send(command);
110
- return data;
110
+ try {
111
+ let response = await s3.send(command);
112
+ let Body = response.Body;
113
+
114
+ if (Body && Body instanceof stream.Readable) {
115
+ // Handling the stream
116
+ const chunks = [];
117
+ for await (const chunk of Body) {
118
+ chunks.push(chunk);
119
+ }
120
+ response.Body = Buffer.concat(chunks);
121
+ return response;
122
+ } else {
123
+ // If Body is not a stream, return as is
124
+ return response;
125
+ }
126
+ } catch (error) {
127
+ throw new Error(error.message);
128
+ }
111
129
  } catch (error) {
112
130
  throw error;
113
131
  }
@@ -152,8 +170,11 @@ const uploadFilesToS3 = async (path, files, extension = 'pdf', options = {
152
170
  * @returns {Promise<string>} - A promise that resolves to the presigned URL.
153
171
  * @throws {Error} - Throws an error if the URL generation fails.
154
172
  */
155
- const generatePresignedUrl = async (bucket, key, expirationInSeconds = 86400 * 30) => {
173
+ const generatePresignedUrl = async (bucket, key, expirationInSeconds = 604000) => {
156
174
  try {
175
+ if(expirationInSeconds > 604000){ //7 days is the max expiration time for a presigned url v4
176
+ expirationInSeconds = 604000;
177
+ }
157
178
  const s3Client = new S3Client();
158
179
 
159
180
  const command = new GetObjectCommand({