@sprucelabs/spruce-file-utils 7.0.216 → 7.1.2
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/build/esm/uploading/FileUploader.d.ts +12 -3
- package/build/esm/uploading/FileUploader.js +14 -26
- package/build/esm/uploading/LocalUploadStrategy.d.ts +6 -0
- package/build/esm/uploading/LocalUploadStrategy.js +23 -0
- package/build/esm/uploading/S3UploadStrategy.d.ts +4 -0
- package/build/esm/uploading/S3UploadStrategy.js +36 -0
- package/build/uploading/FileUploader.d.ts +12 -3
- package/build/uploading/FileUploader.js +14 -24
- package/build/uploading/LocalUploadStrategy.d.ts +6 -0
- package/build/uploading/LocalUploadStrategy.js +15 -0
- package/build/uploading/S3UploadStrategy.d.ts +4 -0
- package/build/uploading/S3UploadStrategy.js +31 -0
- package/package.json +18 -5
@@ -1,12 +1,13 @@
|
|
1
1
|
export default class FileUploaderImpl implements FileUploader {
|
2
|
-
static Class?: new () => FileUploader;
|
3
|
-
|
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
|
30
|
-
|
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
|
-
|
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) {
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { UploadStrategy, UploadStrategyUploadOptions } from './FileUploader';
|
2
|
+
export default class LocalUploadStrategy implements UploadStrategy {
|
3
|
+
private localUploadDir;
|
4
|
+
constructor(localUploadDir: string);
|
5
|
+
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
6
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { diskUtil } from '@sprucelabs/spruce-skill-utils';
|
11
|
+
export default class LocalUploadStrategy {
|
12
|
+
constructor(localUploadDir) {
|
13
|
+
this.localUploadDir = localUploadDir;
|
14
|
+
}
|
15
|
+
upload(options) {
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
17
|
+
const { name, buffer } = options;
|
18
|
+
const destination = diskUtil.resolvePath(this.localUploadDir, name);
|
19
|
+
diskUtil.writeFile(destination, buffer.toString('utf-8'));
|
20
|
+
return destination;
|
21
|
+
});
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { assertOptions } from '@sprucelabs/schema';
|
11
|
+
import AWS from 'aws-sdk';
|
12
|
+
export default class S3UploadStrategy {
|
13
|
+
upload(options) {
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
15
|
+
const { name, buffer, type } = options;
|
16
|
+
const { env: { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET }, } = assertOptions(process, [
|
17
|
+
'env.AWS_ACCESS_KEY_ID',
|
18
|
+
'env.AWS_SECRET_ACCESS_KEY',
|
19
|
+
'env.AWS_S3_BUCKET',
|
20
|
+
]);
|
21
|
+
const s3 = new AWS.S3({
|
22
|
+
accessKeyId: AWS_ACCESS_KEY_ID,
|
23
|
+
secretAccessKey: AWS_SECRET_ACCESS_KEY,
|
24
|
+
});
|
25
|
+
const s3Params = {
|
26
|
+
Bucket: AWS_S3_BUCKET,
|
27
|
+
Key: name,
|
28
|
+
Body: buffer,
|
29
|
+
ACL: 'public-read',
|
30
|
+
ContentType: type,
|
31
|
+
};
|
32
|
+
const uploadedFile = yield s3.upload(s3Params).promise();
|
33
|
+
return uploadedFile.Location;
|
34
|
+
});
|
35
|
+
}
|
36
|
+
}
|
@@ -1,12 +1,13 @@
|
|
1
1
|
export default class FileUploaderImpl implements FileUploader {
|
2
|
-
static Class?: new () => FileUploader;
|
3
|
-
|
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
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
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');
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { UploadStrategy, UploadStrategyUploadOptions } from './FileUploader';
|
2
|
+
export default class LocalUploadStrategy implements UploadStrategy {
|
3
|
+
private localUploadDir;
|
4
|
+
constructor(localUploadDir: string);
|
5
|
+
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
6
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
|
4
|
+
class LocalUploadStrategy {
|
5
|
+
constructor(localUploadDir) {
|
6
|
+
this.localUploadDir = localUploadDir;
|
7
|
+
}
|
8
|
+
async upload(options) {
|
9
|
+
const { name, buffer } = options;
|
10
|
+
const destination = spruce_skill_utils_1.diskUtil.resolvePath(this.localUploadDir, name);
|
11
|
+
spruce_skill_utils_1.diskUtil.writeFile(destination, buffer.toString('utf-8'));
|
12
|
+
return destination;
|
13
|
+
}
|
14
|
+
}
|
15
|
+
exports.default = LocalUploadStrategy;
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const schema_1 = require("@sprucelabs/schema");
|
7
|
+
const aws_sdk_1 = __importDefault(require("aws-sdk"));
|
8
|
+
class S3UploadStrategy {
|
9
|
+
async upload(options) {
|
10
|
+
const { name, buffer, type } = options;
|
11
|
+
const { env: { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET }, } = (0, schema_1.assertOptions)(process, [
|
12
|
+
'env.AWS_ACCESS_KEY_ID',
|
13
|
+
'env.AWS_SECRET_ACCESS_KEY',
|
14
|
+
'env.AWS_S3_BUCKET',
|
15
|
+
]);
|
16
|
+
const s3 = new aws_sdk_1.default.S3({
|
17
|
+
accessKeyId: AWS_ACCESS_KEY_ID,
|
18
|
+
secretAccessKey: AWS_SECRET_ACCESS_KEY,
|
19
|
+
});
|
20
|
+
const s3Params = {
|
21
|
+
Bucket: AWS_S3_BUCKET,
|
22
|
+
Key: name,
|
23
|
+
Body: buffer,
|
24
|
+
ACL: 'public-read',
|
25
|
+
ContentType: type,
|
26
|
+
};
|
27
|
+
const uploadedFile = await s3.upload(s3Params).promise();
|
28
|
+
return uploadedFile.Location;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
exports.default = S3UploadStrategy;
|
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.
|
4
|
+
"version": "7.1.2",
|
5
5
|
"skill": {
|
6
6
|
"namespace": "files"
|
7
7
|
},
|
@@ -16,12 +16,25 @@
|
|
16
16
|
"files": [
|
17
17
|
"build/index-module.js",
|
18
18
|
"build/index-module.d.ts",
|
19
|
-
|
20
|
-
"build/uploading/FileUploader.js",
|
19
|
+
|
21
20
|
"build/esm/index-module.js",
|
22
21
|
"build/esm/index-module.d.ts",
|
22
|
+
|
23
23
|
"build/esm/uploading/FileUploader.d.ts",
|
24
|
-
"build/esm/uploading/FileUploader.js"
|
24
|
+
"build/esm/uploading/FileUploader.js",
|
25
|
+
"build/uploading/FileUploader.d.ts",
|
26
|
+
"build/uploading/FileUploader.js",
|
27
|
+
|
28
|
+
"build/esm/uploading/LocalUploadStrategy.d.ts",
|
29
|
+
"build/esm/uploading/LocalUploadStrategy.js",
|
30
|
+
"build/uploading/LocalUploadStrategy.d.ts",
|
31
|
+
"build/uploading/LocalUploadStrategy.js",
|
32
|
+
|
33
|
+
"build/esm/uploading/S3UploadStrategy.d.ts",
|
34
|
+
"build/esm/uploading/S3UploadStrategy.js",
|
35
|
+
"build/uploading/S3UploadStrategy.d.ts",
|
36
|
+
"build/uploading/S3UploadStrategy.js"
|
37
|
+
|
25
38
|
],
|
26
39
|
"keywords": [],
|
27
40
|
"scripts": {
|
@@ -37,4 +50,4 @@
|
|
37
50
|
"engines": {
|
38
51
|
"yarn": "1.x"
|
39
52
|
}
|
40
|
-
}
|
53
|
+
}
|