@sprucelabs/spruce-file-utils 9.0.60 → 9.2.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/README.md +1 -0
- package/build/esm/uploading/FileUploader.d.ts +13 -3
- package/build/esm/uploading/FileUploader.js +13 -4
- package/build/esm/uploading/S3UploadStrategy.d.ts +2 -0
- package/build/esm/uploading/S3UploadStrategy.js +24 -8
- package/build/uploading/FileUploader.d.ts +13 -3
- package/build/uploading/FileUploader.js +13 -4
- package/build/uploading/S3UploadStrategy.d.ts +2 -0
- package/build/uploading/S3UploadStrategy.js +22 -7
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
+
import { Settings } from '../files.types';
|
2
|
+
import SettingsStore from '../stores/Settings.store';
|
1
3
|
export default class FileUploaderImpl implements FileUploader {
|
2
|
-
static Class?: new (
|
4
|
+
static Class?: new (options: FileUploaderConstructorOptions) => FileUploader;
|
3
5
|
private localUploadDir?;
|
4
6
|
private uploadStrategy;
|
5
|
-
|
6
|
-
|
7
|
+
private settings;
|
8
|
+
protected constructor(options: FileUploaderConstructorOptions);
|
9
|
+
static Uploader(settings: SettingsStore): FileUploader;
|
7
10
|
upload(payload: UploadPayload): Promise<{
|
8
11
|
uri: string;
|
9
12
|
type: string;
|
@@ -13,6 +16,7 @@ export default class FileUploaderImpl implements FileUploader {
|
|
13
16
|
export interface UploadPayload {
|
14
17
|
base64Body: string;
|
15
18
|
name: string;
|
19
|
+
organizationId?: string;
|
16
20
|
}
|
17
21
|
export interface UploadResponse {
|
18
22
|
uri: string;
|
@@ -25,7 +29,13 @@ export interface UploadStrategyUploadOptions {
|
|
25
29
|
name: string;
|
26
30
|
buffer: Buffer;
|
27
31
|
type: string;
|
32
|
+
settings?: Settings;
|
28
33
|
}
|
29
34
|
export interface UploadStrategy {
|
30
35
|
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
31
36
|
}
|
37
|
+
interface FileUploaderConstructorOptions {
|
38
|
+
localUploadDir?: string;
|
39
|
+
settings: SettingsStore;
|
40
|
+
}
|
41
|
+
export {};
|
@@ -14,28 +14,37 @@ import * as uuid from 'uuid';
|
|
14
14
|
import LocalUploadStrategy from './LocalUploadStrategy.js';
|
15
15
|
import S3UploadStrategy from './S3UploadStrategy.js';
|
16
16
|
export default class FileUploaderImpl {
|
17
|
-
constructor(
|
17
|
+
constructor(options) {
|
18
|
+
const { localUploadDir, settings } = options !== null && options !== void 0 ? options : {};
|
18
19
|
this.localUploadDir = localUploadDir;
|
19
20
|
this.uploadStrategy = this.localUploadDir
|
20
21
|
? new LocalUploadStrategy(this.localUploadDir)
|
21
22
|
: new S3UploadStrategy();
|
23
|
+
this.settings = settings;
|
22
24
|
}
|
23
|
-
static Uploader() {
|
25
|
+
static Uploader(settings) {
|
24
26
|
var _a;
|
25
|
-
return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)(
|
27
|
+
return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)({
|
28
|
+
localUploadDir: process.env.LOCAL_UPLOAD_DIR,
|
29
|
+
settings,
|
30
|
+
});
|
26
31
|
}
|
27
32
|
upload(payload) {
|
28
33
|
return __awaiter(this, void 0, void 0, function* () {
|
29
|
-
const { base64Body, name } = assertOptions(payload, [
|
34
|
+
const { base64Body, name, organizationId } = assertOptions(payload, [
|
30
35
|
'base64Body',
|
31
36
|
'name',
|
32
37
|
]);
|
38
|
+
const settings = yield this.settings.findOne({
|
39
|
+
'target.organizationId': organizationId,
|
40
|
+
});
|
33
41
|
const { type, ext, buffer } = yield this.findTypeAndBuffer(base64Body, name);
|
34
42
|
const uniqueName = `${uuid.v4()}.${ext}`;
|
35
43
|
const uri = yield this.uploadStrategy.upload({
|
36
44
|
name: uniqueName,
|
37
45
|
buffer,
|
38
46
|
type,
|
47
|
+
settings: settings !== null && settings !== void 0 ? settings : undefined,
|
39
48
|
});
|
40
49
|
return { uri, type };
|
41
50
|
});
|
@@ -9,16 +9,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
9
9
|
};
|
10
10
|
import { assertOptions } from '@sprucelabs/schema';
|
11
11
|
import AWS from 'aws-sdk';
|
12
|
-
|
12
|
+
class S3UploadStrategy {
|
13
13
|
upload(options) {
|
14
14
|
return __awaiter(this, void 0, void 0, function* () {
|
15
|
-
const { name, buffer, type } = options;
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
const { name, buffer, type, settings } = options;
|
16
|
+
let AWS_S3_BUCKET;
|
17
|
+
let AWS_ACCESS_KEY_ID;
|
18
|
+
let AWS_SECRET_ACCESS_KEY;
|
19
|
+
if (!settings) {
|
20
|
+
const { env: { AWS_ACCESS_KEY_ID: awsAccessKeyId, AWS_SECRET_ACCESS_KEY: awsSecretAccessKey, AWS_S3_BUCKET: awsS3Bucket, }, } = assertOptions(process, [
|
21
|
+
'env.AWS_ACCESS_KEY_ID',
|
22
|
+
'env.AWS_SECRET_ACCESS_KEY',
|
23
|
+
'env.AWS_S3_BUCKET',
|
24
|
+
]);
|
25
|
+
AWS_ACCESS_KEY_ID = awsAccessKeyId;
|
26
|
+
AWS_SECRET_ACCESS_KEY = awsSecretAccessKey;
|
27
|
+
AWS_S3_BUCKET = awsS3Bucket;
|
28
|
+
}
|
29
|
+
else {
|
30
|
+
const { awsAccessKeyId, awsSecretAccessKey, awsS3Bucket } = settings;
|
31
|
+
AWS_ACCESS_KEY_ID = awsAccessKeyId;
|
32
|
+
AWS_SECRET_ACCESS_KEY = awsSecretAccessKey;
|
33
|
+
AWS_S3_BUCKET = awsS3Bucket;
|
34
|
+
}
|
35
|
+
const s3 = new S3UploadStrategy.AWS.S3({
|
22
36
|
accessKeyId: AWS_ACCESS_KEY_ID,
|
23
37
|
secretAccessKey: AWS_SECRET_ACCESS_KEY,
|
24
38
|
});
|
@@ -34,3 +48,5 @@ export default class S3UploadStrategy {
|
|
34
48
|
});
|
35
49
|
}
|
36
50
|
}
|
51
|
+
S3UploadStrategy.AWS = AWS;
|
52
|
+
export default S3UploadStrategy;
|
@@ -1,9 +1,12 @@
|
|
1
|
+
import { Settings } from '../files.types';
|
2
|
+
import SettingsStore from '../stores/Settings.store';
|
1
3
|
export default class FileUploaderImpl implements FileUploader {
|
2
|
-
static Class?: new (
|
4
|
+
static Class?: new (options: FileUploaderConstructorOptions) => FileUploader;
|
3
5
|
private localUploadDir?;
|
4
6
|
private uploadStrategy;
|
5
|
-
|
6
|
-
|
7
|
+
private settings;
|
8
|
+
protected constructor(options: FileUploaderConstructorOptions);
|
9
|
+
static Uploader(settings: SettingsStore): FileUploader;
|
7
10
|
upload(payload: UploadPayload): Promise<{
|
8
11
|
uri: string;
|
9
12
|
type: string;
|
@@ -13,6 +16,7 @@ export default class FileUploaderImpl implements FileUploader {
|
|
13
16
|
export interface UploadPayload {
|
14
17
|
base64Body: string;
|
15
18
|
name: string;
|
19
|
+
organizationId?: string;
|
16
20
|
}
|
17
21
|
export interface UploadResponse {
|
18
22
|
uri: string;
|
@@ -25,7 +29,13 @@ export interface UploadStrategyUploadOptions {
|
|
25
29
|
name: string;
|
26
30
|
buffer: Buffer;
|
27
31
|
type: string;
|
32
|
+
settings?: Settings;
|
28
33
|
}
|
29
34
|
export interface UploadStrategy {
|
30
35
|
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
31
36
|
}
|
37
|
+
interface FileUploaderConstructorOptions {
|
38
|
+
localUploadDir?: string;
|
39
|
+
settings: SettingsStore;
|
40
|
+
}
|
41
|
+
export {};
|
@@ -43,26 +43,35 @@ const uuid = __importStar(require("uuid"));
|
|
43
43
|
const LocalUploadStrategy_1 = __importDefault(require("./LocalUploadStrategy"));
|
44
44
|
const S3UploadStrategy_1 = __importDefault(require("./S3UploadStrategy"));
|
45
45
|
class FileUploaderImpl {
|
46
|
-
constructor(
|
46
|
+
constructor(options) {
|
47
|
+
const { localUploadDir, settings } = options ?? {};
|
47
48
|
this.localUploadDir = localUploadDir;
|
48
49
|
this.uploadStrategy = this.localUploadDir
|
49
50
|
? new LocalUploadStrategy_1.default(this.localUploadDir)
|
50
51
|
: new S3UploadStrategy_1.default();
|
52
|
+
this.settings = settings;
|
51
53
|
}
|
52
|
-
static Uploader() {
|
53
|
-
return new (this.Class ?? this)(
|
54
|
+
static Uploader(settings) {
|
55
|
+
return new (this.Class ?? this)({
|
56
|
+
localUploadDir: process.env.LOCAL_UPLOAD_DIR,
|
57
|
+
settings,
|
58
|
+
});
|
54
59
|
}
|
55
60
|
async upload(payload) {
|
56
|
-
const { base64Body, name } = (0, schema_1.assertOptions)(payload, [
|
61
|
+
const { base64Body, name, organizationId } = (0, schema_1.assertOptions)(payload, [
|
57
62
|
'base64Body',
|
58
63
|
'name',
|
59
64
|
]);
|
65
|
+
const settings = await this.settings.findOne({
|
66
|
+
'target.organizationId': organizationId,
|
67
|
+
});
|
60
68
|
const { type, ext, buffer } = await this.findTypeAndBuffer(base64Body, name);
|
61
69
|
const uniqueName = `${uuid.v4()}.${ext}`;
|
62
70
|
const uri = await this.uploadStrategy.upload({
|
63
71
|
name: uniqueName,
|
64
72
|
buffer,
|
65
73
|
type,
|
74
|
+
settings: settings ?? undefined,
|
66
75
|
});
|
67
76
|
return { uri, type };
|
68
77
|
}
|
@@ -7,13 +7,27 @@ const schema_1 = require("@sprucelabs/schema");
|
|
7
7
|
const aws_sdk_1 = __importDefault(require("aws-sdk"));
|
8
8
|
class S3UploadStrategy {
|
9
9
|
async upload(options) {
|
10
|
-
const { name, buffer, type } = options;
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
const { name, buffer, type, settings } = options;
|
11
|
+
let AWS_S3_BUCKET;
|
12
|
+
let AWS_ACCESS_KEY_ID;
|
13
|
+
let AWS_SECRET_ACCESS_KEY;
|
14
|
+
if (!settings) {
|
15
|
+
const { env: { AWS_ACCESS_KEY_ID: awsAccessKeyId, AWS_SECRET_ACCESS_KEY: awsSecretAccessKey, AWS_S3_BUCKET: awsS3Bucket, }, } = (0, schema_1.assertOptions)(process, [
|
16
|
+
'env.AWS_ACCESS_KEY_ID',
|
17
|
+
'env.AWS_SECRET_ACCESS_KEY',
|
18
|
+
'env.AWS_S3_BUCKET',
|
19
|
+
]);
|
20
|
+
AWS_ACCESS_KEY_ID = awsAccessKeyId;
|
21
|
+
AWS_SECRET_ACCESS_KEY = awsSecretAccessKey;
|
22
|
+
AWS_S3_BUCKET = awsS3Bucket;
|
23
|
+
}
|
24
|
+
else {
|
25
|
+
const { awsAccessKeyId, awsSecretAccessKey, awsS3Bucket } = settings;
|
26
|
+
AWS_ACCESS_KEY_ID = awsAccessKeyId;
|
27
|
+
AWS_SECRET_ACCESS_KEY = awsSecretAccessKey;
|
28
|
+
AWS_S3_BUCKET = awsS3Bucket;
|
29
|
+
}
|
30
|
+
const s3 = new S3UploadStrategy.AWS.S3({
|
17
31
|
accessKeyId: AWS_ACCESS_KEY_ID,
|
18
32
|
secretAccessKey: AWS_SECRET_ACCESS_KEY,
|
19
33
|
});
|
@@ -28,4 +42,5 @@ class S3UploadStrategy {
|
|
28
42
|
return uploadedFile.Location;
|
29
43
|
}
|
30
44
|
}
|
45
|
+
S3UploadStrategy.AWS = aws_sdk_1.default;
|
31
46
|
exports.default = S3UploadStrategy;
|