@sprucelabs/spruce-file-utils 7.0.216 → 7.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.
@@ -1,12 +1,13 @@
1
1
  export default class FileUploaderImpl implements FileUploader {
2
- static Class?: new () => FileUploader;
3
- protected constructor();
2
+ static Class?: new (localUploadDir?: string) => FileUploader;
3
+ private localUploadDir?;
4
+ private uploadStrategy;
5
+ protected constructor(localUploadDir?: string);
4
6
  static Uploader(): FileUploader;
5
7
  upload(payload: UploadPayload): Promise<{
6
8
  uri: string;
7
9
  type: string;
8
10
  }>;
9
- private uploadFileToS3;
10
11
  private findTypeAndBuffer;
11
12
  }
12
13
  export interface UploadPayload {
@@ -20,3 +21,11 @@ export interface UploadResponse {
20
21
  export interface FileUploader {
21
22
  upload(payload: UploadPayload): Promise<UploadResponse>;
22
23
  }
24
+ export interface UploadStrategyUploadOptions {
25
+ name: string;
26
+ buffer: Buffer;
27
+ type: string;
28
+ }
29
+ export interface UploadStrategy {
30
+ upload(options: UploadStrategyUploadOptions): Promise<string>;
31
+ }
@@ -8,15 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { assertOptions, SchemaError } from '@sprucelabs/schema';
11
- import AWS from 'aws-sdk';
12
11
  import FileType from 'file-type';
13
12
  import MimeTypes from 'mime-types';
14
13
  import * as uuid from 'uuid';
14
+ import LocalUploadStrategy from './LocalUploadStrategy.js';
15
+ import S3UploadStrategy from './S3UploadStrategy.js';
15
16
  export default class FileUploaderImpl {
16
- constructor() { }
17
+ constructor(localUploadDir) {
18
+ this.localUploadDir = localUploadDir;
19
+ this.uploadStrategy = this.localUploadDir
20
+ ? new LocalUploadStrategy(this.localUploadDir)
21
+ : new S3UploadStrategy();
22
+ }
17
23
  static Uploader() {
18
24
  var _a;
19
- return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)();
25
+ return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)(process.env.LOCAL_UPLOAD_DIR);
20
26
  }
21
27
  upload(payload) {
22
28
  return __awaiter(this, void 0, void 0, function* () {
@@ -26,30 +32,12 @@ export default class FileUploaderImpl {
26
32
  ]);
27
33
  const { type, ext, buffer } = yield this.findTypeAndBuffer(base64Body, name);
28
34
  const uniqueName = `${uuid.v4()}.${ext}`;
29
- const { Location: uri } = yield this.uploadFileToS3(uniqueName, buffer, type);
30
- return { uri, type };
31
- });
32
- }
33
- uploadFileToS3(name, buffer, type) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- const { env: { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET }, } = assertOptions(process, [
36
- 'env.AWS_ACCESS_KEY_ID',
37
- 'env.AWS_SECRET_ACCESS_KEY',
38
- 'env.AWS_S3_BUCKET',
39
- ]);
40
- const s3 = new AWS.S3({
41
- accessKeyId: AWS_ACCESS_KEY_ID,
42
- secretAccessKey: AWS_SECRET_ACCESS_KEY,
35
+ const uri = yield this.uploadStrategy.upload({
36
+ name: uniqueName,
37
+ buffer,
38
+ type,
43
39
  });
44
- const s3Params = {
45
- Bucket: AWS_S3_BUCKET,
46
- Key: name,
47
- Body: buffer,
48
- ACL: 'public-read',
49
- ContentType: type,
50
- };
51
- const uploadedFile = yield s3.upload(s3Params).promise();
52
- return uploadedFile;
40
+ return { uri, type };
53
41
  });
54
42
  }
55
43
  findTypeAndBuffer(base64Body, name) {
@@ -1,12 +1,13 @@
1
1
  export default class FileUploaderImpl implements FileUploader {
2
- static Class?: new () => FileUploader;
3
- protected constructor();
2
+ static Class?: new (localUploadDir?: string) => FileUploader;
3
+ private localUploadDir?;
4
+ private uploadStrategy;
5
+ protected constructor(localUploadDir?: string);
4
6
  static Uploader(): FileUploader;
5
7
  upload(payload: UploadPayload): Promise<{
6
8
  uri: string;
7
9
  type: string;
8
10
  }>;
9
- private uploadFileToS3;
10
11
  private findTypeAndBuffer;
11
12
  }
12
13
  export interface UploadPayload {
@@ -20,3 +21,11 @@ export interface UploadResponse {
20
21
  export interface FileUploader {
21
22
  upload(payload: UploadPayload): Promise<UploadResponse>;
22
23
  }
24
+ export interface UploadStrategyUploadOptions {
25
+ name: string;
26
+ buffer: Buffer;
27
+ type: string;
28
+ }
29
+ export interface UploadStrategy {
30
+ upload(options: UploadStrategyUploadOptions): Promise<string>;
31
+ }
@@ -37,14 +37,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  const schema_1 = require("@sprucelabs/schema");
40
- const aws_sdk_1 = __importDefault(require("aws-sdk"));
41
40
  const file_type_1 = __importDefault(require("file-type"));
42
41
  const mime_types_1 = __importDefault(require("mime-types"));
43
42
  const uuid = __importStar(require("uuid"));
43
+ const LocalUploadStrategy_1 = __importDefault(require("./LocalUploadStrategy"));
44
+ const S3UploadStrategy_1 = __importDefault(require("./S3UploadStrategy"));
44
45
  class FileUploaderImpl {
45
- constructor() { }
46
+ constructor(localUploadDir) {
47
+ this.localUploadDir = localUploadDir;
48
+ this.uploadStrategy = this.localUploadDir
49
+ ? new LocalUploadStrategy_1.default(this.localUploadDir)
50
+ : new S3UploadStrategy_1.default();
51
+ }
46
52
  static Uploader() {
47
- return new (this.Class ?? this)();
53
+ return new (this.Class ?? this)(process.env.LOCAL_UPLOAD_DIR);
48
54
  }
49
55
  async upload(payload) {
50
56
  const { base64Body, name } = (0, schema_1.assertOptions)(payload, [
@@ -53,28 +59,12 @@ class FileUploaderImpl {
53
59
  ]);
54
60
  const { type, ext, buffer } = await this.findTypeAndBuffer(base64Body, name);
55
61
  const uniqueName = `${uuid.v4()}.${ext}`;
56
- const { Location: uri } = await this.uploadFileToS3(uniqueName, buffer, type);
57
- return { uri, type };
58
- }
59
- async uploadFileToS3(name, buffer, type) {
60
- const { env: { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET }, } = (0, schema_1.assertOptions)(process, [
61
- 'env.AWS_ACCESS_KEY_ID',
62
- 'env.AWS_SECRET_ACCESS_KEY',
63
- 'env.AWS_S3_BUCKET',
64
- ]);
65
- const s3 = new aws_sdk_1.default.S3({
66
- accessKeyId: AWS_ACCESS_KEY_ID,
67
- secretAccessKey: AWS_SECRET_ACCESS_KEY,
62
+ const uri = await this.uploadStrategy.upload({
63
+ name: uniqueName,
64
+ buffer,
65
+ type,
68
66
  });
69
- const s3Params = {
70
- Bucket: AWS_S3_BUCKET,
71
- Key: name,
72
- Body: buffer,
73
- ACL: 'public-read',
74
- ContentType: type,
75
- };
76
- const uploadedFile = await s3.upload(s3Params).promise();
77
- return uploadedFile;
67
+ return { uri, type };
78
68
  }
79
69
  async findTypeAndBuffer(base64Body, name) {
80
70
  const buffer = Buffer.from(base64Body, 'base64');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sprucelabs/spruce-file-utils",
3
3
  "description": "Utils for working with files and Sprucebot.",
4
- "version": "7.0.216",
4
+ "version": "7.1.0",
5
5
  "skill": {
6
6
  "namespace": "files"
7
7
  },