ag-common 0.0.357 → 0.0.358

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.
@@ -47,3 +47,25 @@ export declare const deleteFiles: ({ Bucket, Keys, }: {
47
47
  error?: string;
48
48
  }>;
49
49
  export declare function listFiles(bucketName: string): Promise<string[]>;
50
+ /**
51
+ * allow uploading of file directly to s3
52
+ * @param param0
53
+ *
54
+ * @returns url to POST to, and fields to send, eg
55
+ * formData.append('Content-Type', file.type);
56
+ Object.entries(fields).forEach(([k, v]) => {
57
+ formData.append(k, v);
58
+ });
59
+
60
+ formData.append('file', file);
61
+ fetch.POST(url,formData)
62
+ */
63
+ export declare function getPresignedPostURL({ bucket, key, maxMb, }: {
64
+ bucket: string;
65
+ key: string;
66
+ /** max filesize. default 5 */
67
+ maxMb?: number;
68
+ }): Promise<{
69
+ url: string;
70
+ fields: Record<string, string>;
71
+ }>;
@@ -24,7 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  return (mod && mod.__esModule) ? mod : { "default": mod };
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.listFiles = exports.deleteFiles = exports.deleteFile = exports.uploadFile = exports.putS3Object = exports.getS3Objects = exports.getS3Object = exports.setS3 = void 0;
27
+ exports.getPresignedPostURL = exports.listFiles = exports.deleteFiles = exports.deleteFile = exports.uploadFile = exports.putS3Object = exports.getS3Objects = exports.getS3Object = exports.setS3 = void 0;
28
28
  const s3_1 = __importDefault(require("aws-sdk/clients/s3"));
29
29
  const log_1 = require("../../common/helpers/log");
30
30
  const array_1 = require("../../common/helpers/array");
@@ -139,3 +139,34 @@ function listFiles(bucketName) {
139
139
  });
140
140
  }
141
141
  exports.listFiles = listFiles;
142
+ /**
143
+ * allow uploading of file directly to s3
144
+ * @param param0
145
+ *
146
+ * @returns url to POST to, and fields to send, eg
147
+ * formData.append('Content-Type', file.type);
148
+ Object.entries(fields).forEach(([k, v]) => {
149
+ formData.append(k, v);
150
+ });
151
+
152
+ formData.append('file', file);
153
+ fetch.POST(url,formData)
154
+ */
155
+ function getPresignedPostURL({ bucket, key, maxMb = 5, }) {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ const ps = yield s3.createPresignedPost({
158
+ Bucket: bucket,
159
+ Fields: {
160
+ Key: key,
161
+ },
162
+ Expires: 600,
163
+ Conditions: [
164
+ ['content-length-range', 0, maxMb * 1049000],
165
+ ['starts-with', '$Content-Type', 'image/'],
166
+ ],
167
+ });
168
+ const fields = JSON.parse(JSON.stringify(ps.fields));
169
+ return { fields, url: ps.url };
170
+ });
171
+ }
172
+ exports.getPresignedPostURL = getPresignedPostURL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.357",
3
+ "version": "0.0.358",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",