@sprucelabs/spruce-file-utils 16.1.1 → 17.0.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/build/esm/uploading/FileUploader.d.ts +3 -3
- package/build/esm/uploading/FileUploader.js +10 -5
- package/build/esm/uploading/LocalUploadStrategy.d.ts +11 -1
- package/build/esm/uploading/LocalUploadStrategy.js +13 -2
- package/build/uploading/FileUploader.d.ts +3 -3
- package/build/uploading/FileUploader.js +10 -5
- package/build/uploading/LocalUploadStrategy.d.ts +11 -1
- package/build/uploading/LocalUploadStrategy.js +12 -2
- package/package.json +1 -1
@@ -2,7 +2,6 @@ import { Settings } from '../files.types';
|
|
2
2
|
import SettingsStore from '../stores/Settings.store';
|
3
3
|
export default class FileUploaderImpl implements FileUploader {
|
4
4
|
static Class?: new (options: FileUploaderConstructorOptions) => FileUploader;
|
5
|
-
private localUploadDir?;
|
6
5
|
private uploadStrategy;
|
7
6
|
private settings?;
|
8
7
|
protected constructor(options: FileUploaderConstructorOptions);
|
@@ -34,8 +33,9 @@ export interface UploadStrategyUploadOptions {
|
|
34
33
|
export interface UploadStrategy {
|
35
34
|
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
36
35
|
}
|
37
|
-
interface FileUploaderConstructorOptions {
|
36
|
+
export interface FileUploaderConstructorOptions {
|
38
37
|
localUploadDir?: string;
|
38
|
+
fileHostUrl?: string;
|
39
|
+
strategy?: 'local' | 's3';
|
39
40
|
settings?: SettingsStore;
|
40
41
|
}
|
41
|
-
export {};
|
@@ -15,17 +15,22 @@ import LocalUploadStrategy from './LocalUploadStrategy.js';
|
|
15
15
|
import S3UploadStrategy from './S3UploadStrategy.js';
|
16
16
|
export default class FileUploaderImpl {
|
17
17
|
constructor(options) {
|
18
|
-
const { localUploadDir, settings } = options !== null && options !== void 0 ? options : {};
|
19
|
-
this.
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
const { localUploadDir, settings, strategy, fileHostUrl } = options !== null && options !== void 0 ? options : {};
|
19
|
+
this.uploadStrategy =
|
20
|
+
strategy === 'local'
|
21
|
+
? LocalUploadStrategy.Uploader({
|
22
|
+
localUploadDir: localUploadDir,
|
23
|
+
fileHostUrl: fileHostUrl,
|
24
|
+
})
|
25
|
+
: new S3UploadStrategy();
|
23
26
|
this.settings = settings;
|
24
27
|
}
|
25
28
|
static Uploader(settings) {
|
26
29
|
var _a;
|
27
30
|
return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)({
|
28
31
|
localUploadDir: process.env.LOCAL_UPLOAD_DIR,
|
32
|
+
fileHostUrl: process.env.FILE_HOST_URL,
|
33
|
+
strategy: process.env.UPLOAD_STRATEGY,
|
29
34
|
settings,
|
30
35
|
});
|
31
36
|
}
|
@@ -1,6 +1,16 @@
|
|
1
1
|
import { UploadStrategy, UploadStrategyUploadOptions } from './FileUploader';
|
2
2
|
export default class LocalUploadStrategy implements UploadStrategy {
|
3
|
+
static Class?: new (options: LocalStrategyOptions) => UploadStrategy;
|
3
4
|
private localUploadDir;
|
4
|
-
|
5
|
+
private fileHostUrl;
|
6
|
+
constructor(options: {
|
7
|
+
localUploadDir: string;
|
8
|
+
fileHostUrl: string;
|
9
|
+
});
|
10
|
+
static Uploader(options: LocalStrategyOptions): UploadStrategy;
|
5
11
|
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
6
12
|
}
|
13
|
+
export interface LocalStrategyOptions {
|
14
|
+
localUploadDir: string;
|
15
|
+
fileHostUrl: string;
|
16
|
+
}
|
@@ -7,17 +7,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
8
|
});
|
9
9
|
};
|
10
|
+
import { assertOptions } from '@sprucelabs/schema';
|
10
11
|
import { diskUtil } from '@sprucelabs/spruce-skill-utils';
|
11
12
|
export default class LocalUploadStrategy {
|
12
|
-
constructor(
|
13
|
+
constructor(options) {
|
14
|
+
const { localUploadDir, fileHostUrl } = assertOptions(options, [
|
15
|
+
'localUploadDir',
|
16
|
+
'fileHostUrl',
|
17
|
+
]);
|
18
|
+
this.fileHostUrl = fileHostUrl;
|
13
19
|
this.localUploadDir = localUploadDir;
|
14
20
|
}
|
21
|
+
static Uploader(options) {
|
22
|
+
var _a;
|
23
|
+
assertOptions(options, ['localUploadDir', 'fileHostUrl']);
|
24
|
+
return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)(options);
|
25
|
+
}
|
15
26
|
upload(options) {
|
16
27
|
return __awaiter(this, void 0, void 0, function* () {
|
17
28
|
const { name, buffer } = options;
|
18
29
|
const destination = diskUtil.resolvePath(this.localUploadDir, name);
|
19
30
|
diskUtil.writeFile(destination, buffer.toString('utf-8'));
|
20
|
-
return
|
31
|
+
return `${this.fileHostUrl}/${name}`;
|
21
32
|
});
|
22
33
|
}
|
23
34
|
}
|
@@ -2,7 +2,6 @@ import { Settings } from '../files.types';
|
|
2
2
|
import SettingsStore from '../stores/Settings.store';
|
3
3
|
export default class FileUploaderImpl implements FileUploader {
|
4
4
|
static Class?: new (options: FileUploaderConstructorOptions) => FileUploader;
|
5
|
-
private localUploadDir?;
|
6
5
|
private uploadStrategy;
|
7
6
|
private settings?;
|
8
7
|
protected constructor(options: FileUploaderConstructorOptions);
|
@@ -34,8 +33,9 @@ export interface UploadStrategyUploadOptions {
|
|
34
33
|
export interface UploadStrategy {
|
35
34
|
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
36
35
|
}
|
37
|
-
interface FileUploaderConstructorOptions {
|
36
|
+
export interface FileUploaderConstructorOptions {
|
38
37
|
localUploadDir?: string;
|
38
|
+
fileHostUrl?: string;
|
39
|
+
strategy?: 'local' | 's3';
|
39
40
|
settings?: SettingsStore;
|
40
41
|
}
|
41
|
-
export {};
|
@@ -44,16 +44,21 @@ const LocalUploadStrategy_1 = __importDefault(require("./LocalUploadStrategy"));
|
|
44
44
|
const S3UploadStrategy_1 = __importDefault(require("./S3UploadStrategy"));
|
45
45
|
class FileUploaderImpl {
|
46
46
|
constructor(options) {
|
47
|
-
const { localUploadDir, settings } = options ?? {};
|
48
|
-
this.
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
const { localUploadDir, settings, strategy, fileHostUrl } = options ?? {};
|
48
|
+
this.uploadStrategy =
|
49
|
+
strategy === 'local'
|
50
|
+
? LocalUploadStrategy_1.default.Uploader({
|
51
|
+
localUploadDir: localUploadDir,
|
52
|
+
fileHostUrl: fileHostUrl,
|
53
|
+
})
|
54
|
+
: new S3UploadStrategy_1.default();
|
52
55
|
this.settings = settings;
|
53
56
|
}
|
54
57
|
static Uploader(settings) {
|
55
58
|
return new (this.Class ?? this)({
|
56
59
|
localUploadDir: process.env.LOCAL_UPLOAD_DIR,
|
60
|
+
fileHostUrl: process.env.FILE_HOST_URL,
|
61
|
+
strategy: process.env.UPLOAD_STRATEGY,
|
57
62
|
settings,
|
58
63
|
});
|
59
64
|
}
|
@@ -1,6 +1,16 @@
|
|
1
1
|
import { UploadStrategy, UploadStrategyUploadOptions } from './FileUploader';
|
2
2
|
export default class LocalUploadStrategy implements UploadStrategy {
|
3
|
+
static Class?: new (options: LocalStrategyOptions) => UploadStrategy;
|
3
4
|
private localUploadDir;
|
4
|
-
|
5
|
+
private fileHostUrl;
|
6
|
+
constructor(options: {
|
7
|
+
localUploadDir: string;
|
8
|
+
fileHostUrl: string;
|
9
|
+
});
|
10
|
+
static Uploader(options: LocalStrategyOptions): UploadStrategy;
|
5
11
|
upload(options: UploadStrategyUploadOptions): Promise<string>;
|
6
12
|
}
|
13
|
+
export interface LocalStrategyOptions {
|
14
|
+
localUploadDir: string;
|
15
|
+
fileHostUrl: string;
|
16
|
+
}
|
@@ -1,15 +1,25 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
3
4
|
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
|
4
5
|
class LocalUploadStrategy {
|
5
|
-
constructor(
|
6
|
+
constructor(options) {
|
7
|
+
const { localUploadDir, fileHostUrl } = (0, schema_1.assertOptions)(options, [
|
8
|
+
'localUploadDir',
|
9
|
+
'fileHostUrl',
|
10
|
+
]);
|
11
|
+
this.fileHostUrl = fileHostUrl;
|
6
12
|
this.localUploadDir = localUploadDir;
|
7
13
|
}
|
14
|
+
static Uploader(options) {
|
15
|
+
(0, schema_1.assertOptions)(options, ['localUploadDir', 'fileHostUrl']);
|
16
|
+
return new (this.Class ?? this)(options);
|
17
|
+
}
|
8
18
|
async upload(options) {
|
9
19
|
const { name, buffer } = options;
|
10
20
|
const destination = spruce_skill_utils_1.diskUtil.resolvePath(this.localUploadDir, name);
|
11
21
|
spruce_skill_utils_1.diskUtil.writeFile(destination, buffer.toString('utf-8'));
|
12
|
-
return
|
22
|
+
return `${this.fileHostUrl}/${name}`;
|
13
23
|
}
|
14
24
|
}
|
15
25
|
exports.default = LocalUploadStrategy;
|