@sprucelabs/spruce-file-utils 15.1.0 → 15.1.1
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,18 +1,22 @@
|
|
1
1
|
import { MercuryConnectFactory } from '@sprucelabs/mercury-client';
|
2
|
+
import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
|
3
|
+
import { MercuryEventEmitter } from '@sprucelabs/mercury-types';
|
2
4
|
import { UploadedFile } from '../files.types';
|
3
|
-
export default class ChunkingUploaderImpl implements ChunkingUploader {
|
5
|
+
export default class ChunkingUploaderImpl extends AbstractEventEmitter<ChunkingUploaderEventContract> implements ChunkingUploader {
|
4
6
|
private connectToApi;
|
5
7
|
static fetch: typeof fetch;
|
6
8
|
static chunkSizeKb: number;
|
9
|
+
static Class?: new (connectToApi: MercuryConnectFactory) => ChunkingUploader;
|
7
10
|
private restEndpointUrl?;
|
8
11
|
private person;
|
9
12
|
private proxyToken;
|
10
13
|
protected constructor(connectToApi: MercuryConnectFactory);
|
11
|
-
static Uploader(connectToApi: MercuryConnectFactory): ChunkingUploaderImpl;
|
14
|
+
static Uploader(connectToApi: MercuryConnectFactory): ChunkingUploaderImpl | ChunkingUploader;
|
12
15
|
upload(options: UploadOptions): Promise<{
|
13
16
|
file: UploadedFile;
|
14
17
|
}>;
|
15
18
|
private uploadChunking;
|
19
|
+
private splitIntoChunks;
|
16
20
|
private get chunkSizeKb();
|
17
21
|
private doesSupportChunking;
|
18
22
|
private emitUpload;
|
@@ -21,14 +25,31 @@ export default class ChunkingUploaderImpl implements ChunkingUploader {
|
|
21
25
|
private post;
|
22
26
|
private get fetch();
|
23
27
|
}
|
24
|
-
export interface ChunkingUploader {
|
28
|
+
export interface ChunkingUploader extends MercuryEventEmitter<ChunkingUploaderEventContract> {
|
25
29
|
upload(options: UploadOptions): Promise<{
|
26
30
|
file: UploadedFile;
|
27
31
|
}>;
|
28
32
|
}
|
33
|
+
declare const chunkingUploaderEventContract: {
|
34
|
+
eventSignatures: {
|
35
|
+
'did-upload-chunk': {
|
36
|
+
emitPayloadSchema: {
|
37
|
+
id: string;
|
38
|
+
fields: {
|
39
|
+
percentComplete: {
|
40
|
+
type: "number";
|
41
|
+
isRequired: true;
|
42
|
+
};
|
43
|
+
};
|
44
|
+
};
|
45
|
+
};
|
46
|
+
};
|
47
|
+
};
|
48
|
+
type ChunkingUploaderEventContract = typeof chunkingUploaderEventContract;
|
29
49
|
export interface UploadOptions {
|
30
50
|
fileName: string;
|
31
51
|
base64: string;
|
32
52
|
locationId?: string;
|
33
53
|
organizationId?: string;
|
34
54
|
}
|
55
|
+
export {};
|
@@ -7,15 +7,19 @@ 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 {
|
10
|
+
import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
|
11
|
+
import { buildEventContract, } from '@sprucelabs/mercury-types';
|
12
|
+
import { assertOptions, buildSchema } from '@sprucelabs/schema';
|
11
13
|
import SpruceError from '../errors/SpruceError.js';
|
12
|
-
class ChunkingUploaderImpl {
|
14
|
+
class ChunkingUploaderImpl extends AbstractEventEmitter {
|
13
15
|
constructor(connectToApi) {
|
16
|
+
super(chunkingUploaderEventContract);
|
14
17
|
this.connectToApi = connectToApi;
|
15
18
|
}
|
16
19
|
static Uploader(connectToApi) {
|
20
|
+
var _a;
|
17
21
|
assertOptions({ connectToApi }, ['connectToApi']);
|
18
|
-
return new this(connectToApi);
|
22
|
+
return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)(connectToApi);
|
19
23
|
}
|
20
24
|
upload(options) {
|
21
25
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -44,10 +48,7 @@ class ChunkingUploaderImpl {
|
|
44
48
|
locationId,
|
45
49
|
organizationId,
|
46
50
|
});
|
47
|
-
const chunks =
|
48
|
-
for (let i = 0; i < base64.length; i += this.chunkSizeKb * 1024) {
|
49
|
-
chunks.push(base64.slice(i, i + this.chunkSizeKb * 1024));
|
50
|
-
}
|
51
|
+
const chunks = this.splitIntoChunks(base64);
|
51
52
|
let upload = {};
|
52
53
|
for (let i = 0; i < chunks.length; i++) {
|
53
54
|
const chunk = chunks[i];
|
@@ -57,11 +58,22 @@ class ChunkingUploaderImpl {
|
|
57
58
|
}, {
|
58
59
|
'x-upload-token': initialize.uploadToken,
|
59
60
|
});
|
61
|
+
const percentComplete = (i + 1) / totalChunks;
|
62
|
+
yield this.emit('did-upload-chunk', {
|
63
|
+
percentComplete,
|
64
|
+
});
|
60
65
|
}
|
61
66
|
const file = upload.file;
|
62
67
|
return file;
|
63
68
|
});
|
64
69
|
}
|
70
|
+
splitIntoChunks(base64) {
|
71
|
+
const chunks = [];
|
72
|
+
for (let i = 0; i < base64.length; i += this.chunkSizeKb * 1024) {
|
73
|
+
chunks.push(base64.slice(i, i + this.chunkSizeKb * 1024));
|
74
|
+
}
|
75
|
+
return chunks;
|
76
|
+
}
|
65
77
|
get chunkSizeKb() {
|
66
78
|
return ChunkingUploaderImpl.chunkSizeKb;
|
67
79
|
}
|
@@ -82,8 +94,7 @@ class ChunkingUploaderImpl {
|
|
82
94
|
name: fileName,
|
83
95
|
},
|
84
96
|
});
|
85
|
-
|
86
|
-
return f;
|
97
|
+
return file;
|
87
98
|
});
|
88
99
|
}
|
89
100
|
loadRestEndpoint() {
|
@@ -134,3 +145,18 @@ class ChunkingUploaderImpl {
|
|
134
145
|
ChunkingUploaderImpl.fetch = fetch;
|
135
146
|
ChunkingUploaderImpl.chunkSizeKb = 1024;
|
136
147
|
export default ChunkingUploaderImpl;
|
148
|
+
const chunkingUploaderEventContract = buildEventContract({
|
149
|
+
eventSignatures: {
|
150
|
+
'did-upload-chunk': {
|
151
|
+
emitPayloadSchema: buildSchema({
|
152
|
+
id: 'didUploadChunkPayload',
|
153
|
+
fields: {
|
154
|
+
percentComplete: {
|
155
|
+
type: 'number',
|
156
|
+
isRequired: true,
|
157
|
+
},
|
158
|
+
},
|
159
|
+
}),
|
160
|
+
},
|
161
|
+
},
|
162
|
+
});
|
@@ -1,18 +1,22 @@
|
|
1
1
|
import { MercuryConnectFactory } from '@sprucelabs/mercury-client';
|
2
|
+
import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
|
3
|
+
import { MercuryEventEmitter } from '@sprucelabs/mercury-types';
|
2
4
|
import { UploadedFile } from '../files.types';
|
3
|
-
export default class ChunkingUploaderImpl implements ChunkingUploader {
|
5
|
+
export default class ChunkingUploaderImpl extends AbstractEventEmitter<ChunkingUploaderEventContract> implements ChunkingUploader {
|
4
6
|
private connectToApi;
|
5
7
|
static fetch: typeof fetch;
|
6
8
|
static chunkSizeKb: number;
|
9
|
+
static Class?: new (connectToApi: MercuryConnectFactory) => ChunkingUploader;
|
7
10
|
private restEndpointUrl?;
|
8
11
|
private person;
|
9
12
|
private proxyToken;
|
10
13
|
protected constructor(connectToApi: MercuryConnectFactory);
|
11
|
-
static Uploader(connectToApi: MercuryConnectFactory): ChunkingUploaderImpl;
|
14
|
+
static Uploader(connectToApi: MercuryConnectFactory): ChunkingUploaderImpl | ChunkingUploader;
|
12
15
|
upload(options: UploadOptions): Promise<{
|
13
16
|
file: UploadedFile;
|
14
17
|
}>;
|
15
18
|
private uploadChunking;
|
19
|
+
private splitIntoChunks;
|
16
20
|
private get chunkSizeKb();
|
17
21
|
private doesSupportChunking;
|
18
22
|
private emitUpload;
|
@@ -21,14 +25,31 @@ export default class ChunkingUploaderImpl implements ChunkingUploader {
|
|
21
25
|
private post;
|
22
26
|
private get fetch();
|
23
27
|
}
|
24
|
-
export interface ChunkingUploader {
|
28
|
+
export interface ChunkingUploader extends MercuryEventEmitter<ChunkingUploaderEventContract> {
|
25
29
|
upload(options: UploadOptions): Promise<{
|
26
30
|
file: UploadedFile;
|
27
31
|
}>;
|
28
32
|
}
|
33
|
+
declare const chunkingUploaderEventContract: {
|
34
|
+
eventSignatures: {
|
35
|
+
'did-upload-chunk': {
|
36
|
+
emitPayloadSchema: {
|
37
|
+
id: string;
|
38
|
+
fields: {
|
39
|
+
percentComplete: {
|
40
|
+
type: "number";
|
41
|
+
isRequired: true;
|
42
|
+
};
|
43
|
+
};
|
44
|
+
};
|
45
|
+
};
|
46
|
+
};
|
47
|
+
};
|
48
|
+
type ChunkingUploaderEventContract = typeof chunkingUploaderEventContract;
|
29
49
|
export interface UploadOptions {
|
30
50
|
fileName: string;
|
31
51
|
base64: string;
|
32
52
|
locationId?: string;
|
33
53
|
organizationId?: string;
|
34
54
|
}
|
55
|
+
export {};
|
@@ -3,15 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const mercury_event_emitter_1 = require("@sprucelabs/mercury-event-emitter");
|
7
|
+
const mercury_types_1 = require("@sprucelabs/mercury-types");
|
6
8
|
const schema_1 = require("@sprucelabs/schema");
|
7
9
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
8
|
-
class ChunkingUploaderImpl {
|
10
|
+
class ChunkingUploaderImpl extends mercury_event_emitter_1.AbstractEventEmitter {
|
9
11
|
constructor(connectToApi) {
|
12
|
+
super(chunkingUploaderEventContract);
|
10
13
|
this.connectToApi = connectToApi;
|
11
14
|
}
|
12
15
|
static Uploader(connectToApi) {
|
13
16
|
(0, schema_1.assertOptions)({ connectToApi }, ['connectToApi']);
|
14
|
-
return new this(connectToApi);
|
17
|
+
return new (this.Class ?? this)(connectToApi);
|
15
18
|
}
|
16
19
|
async upload(options) {
|
17
20
|
const { fileName, locationId, organizationId, base64 } = (0, schema_1.assertOptions)(options, ['fileName', 'base64']);
|
@@ -37,10 +40,7 @@ class ChunkingUploaderImpl {
|
|
37
40
|
locationId,
|
38
41
|
organizationId,
|
39
42
|
});
|
40
|
-
const chunks =
|
41
|
-
for (let i = 0; i < base64.length; i += this.chunkSizeKb * 1024) {
|
42
|
-
chunks.push(base64.slice(i, i + this.chunkSizeKb * 1024));
|
43
|
-
}
|
43
|
+
const chunks = this.splitIntoChunks(base64);
|
44
44
|
let upload = {};
|
45
45
|
for (let i = 0; i < chunks.length; i++) {
|
46
46
|
const chunk = chunks[i];
|
@@ -50,10 +50,21 @@ class ChunkingUploaderImpl {
|
|
50
50
|
}, {
|
51
51
|
'x-upload-token': initialize.uploadToken,
|
52
52
|
});
|
53
|
+
const percentComplete = (i + 1) / totalChunks;
|
54
|
+
await this.emit('did-upload-chunk', {
|
55
|
+
percentComplete,
|
56
|
+
});
|
53
57
|
}
|
54
58
|
const file = upload.file;
|
55
59
|
return file;
|
56
60
|
}
|
61
|
+
splitIntoChunks(base64) {
|
62
|
+
const chunks = [];
|
63
|
+
for (let i = 0; i < base64.length; i += this.chunkSizeKb * 1024) {
|
64
|
+
chunks.push(base64.slice(i, i + this.chunkSizeKb * 1024));
|
65
|
+
}
|
66
|
+
return chunks;
|
67
|
+
}
|
57
68
|
get chunkSizeKb() {
|
58
69
|
return ChunkingUploaderImpl.chunkSizeKb;
|
59
70
|
}
|
@@ -73,8 +84,7 @@ class ChunkingUploaderImpl {
|
|
73
84
|
name: fileName,
|
74
85
|
},
|
75
86
|
});
|
76
|
-
|
77
|
-
return f;
|
87
|
+
return file;
|
78
88
|
}
|
79
89
|
async loadRestEndpoint() {
|
80
90
|
const client = await this.connectToApi();
|
@@ -121,3 +131,18 @@ class ChunkingUploaderImpl {
|
|
121
131
|
ChunkingUploaderImpl.fetch = fetch;
|
122
132
|
ChunkingUploaderImpl.chunkSizeKb = 1024;
|
123
133
|
exports.default = ChunkingUploaderImpl;
|
134
|
+
const chunkingUploaderEventContract = (0, mercury_types_1.buildEventContract)({
|
135
|
+
eventSignatures: {
|
136
|
+
'did-upload-chunk': {
|
137
|
+
emitPayloadSchema: (0, schema_1.buildSchema)({
|
138
|
+
id: 'didUploadChunkPayload',
|
139
|
+
fields: {
|
140
|
+
percentComplete: {
|
141
|
+
type: 'number',
|
142
|
+
isRequired: true,
|
143
|
+
},
|
144
|
+
},
|
145
|
+
}),
|
146
|
+
},
|
147
|
+
},
|
148
|
+
});
|
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": "15.1.
|
4
|
+
"version": "15.1.1",
|
5
5
|
"skill": {
|
6
6
|
"namespace": "files"
|
7
7
|
},
|
@@ -47,10 +47,11 @@
|
|
47
47
|
},
|
48
48
|
"dependencies": {
|
49
49
|
"@sprucelabs/schema": "latest",
|
50
|
+
"@sprucelabs/mercury-event-emitter": "latest",
|
50
51
|
"aws-sdk": "^2.1558.0",
|
51
52
|
"file-type": "16.5.3",
|
52
|
-
"mime-types": "^
|
53
|
-
"uuid": "^
|
53
|
+
"@types/mime-types": "^3.0.1",
|
54
|
+
"uuid": "^11.1.0"
|
54
55
|
},
|
55
56
|
"engines": {
|
56
57
|
"yarn": "1.x"
|