@sprucelabs/spruce-file-utils 15.0.1 → 15.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.
- package/build/esm/index-module.d.ts +2 -0
- package/build/esm/index-module.js +2 -0
- package/build/esm/uploading/ChunkingUploader.d.ts +34 -0
- package/build/esm/uploading/ChunkingUploader.js +136 -0
- package/build/index-module.d.ts +2 -0
- package/build/index-module.js +4 -1
- package/build/uploading/ChunkingUploader.d.ts +34 -0
- package/build/uploading/ChunkingUploader.js +123 -0
- package/package.json +6 -1
@@ -0,0 +1,34 @@
|
|
1
|
+
import { MercuryConnectFactory } from '@sprucelabs/mercury-client';
|
2
|
+
import { UploadedFile } from '../files.types';
|
3
|
+
export default class ChunkingUploaderImpl implements ChunkingUploader {
|
4
|
+
private connectToApi;
|
5
|
+
static fetch: typeof fetch;
|
6
|
+
static chunkSizeKb: number;
|
7
|
+
private restEndpointUrl?;
|
8
|
+
private person;
|
9
|
+
private proxyToken;
|
10
|
+
protected constructor(connectToApi: MercuryConnectFactory);
|
11
|
+
static Uploader(connectToApi: MercuryConnectFactory): ChunkingUploaderImpl;
|
12
|
+
upload(options: UploadOptions): Promise<{
|
13
|
+
file: UploadedFile;
|
14
|
+
}>;
|
15
|
+
private uploadChunking;
|
16
|
+
private get chunkSizeKb();
|
17
|
+
private doesSupportChunking;
|
18
|
+
private emitUpload;
|
19
|
+
private loadRestEndpoint;
|
20
|
+
private login;
|
21
|
+
private post;
|
22
|
+
private get fetch();
|
23
|
+
}
|
24
|
+
export interface ChunkingUploader {
|
25
|
+
upload(options: UploadOptions): Promise<{
|
26
|
+
file: UploadedFile;
|
27
|
+
}>;
|
28
|
+
}
|
29
|
+
export interface UploadOptions {
|
30
|
+
fileName: string;
|
31
|
+
base64: string;
|
32
|
+
locationId?: string;
|
33
|
+
organizationId?: string;
|
34
|
+
}
|
@@ -0,0 +1,136 @@
|
|
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 SpruceError from '../errors/SpruceError.js';
|
12
|
+
class ChunkingUploaderImpl {
|
13
|
+
constructor(connectToApi) {
|
14
|
+
this.connectToApi = connectToApi;
|
15
|
+
}
|
16
|
+
static Uploader(connectToApi) {
|
17
|
+
assertOptions({ connectToApi }, ['connectToApi']);
|
18
|
+
return new this(connectToApi);
|
19
|
+
}
|
20
|
+
upload(options) {
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
22
|
+
const { fileName, locationId, organizationId, base64 } = assertOptions(options, ['fileName', 'base64']);
|
23
|
+
yield this.login();
|
24
|
+
yield this.loadRestEndpoint();
|
25
|
+
const method = this.doesSupportChunking() ? 'uploadChunking' : 'emitUpload';
|
26
|
+
const file = yield this[method]({
|
27
|
+
fileName,
|
28
|
+
locationId,
|
29
|
+
organizationId,
|
30
|
+
base64,
|
31
|
+
});
|
32
|
+
return { file };
|
33
|
+
});
|
34
|
+
}
|
35
|
+
uploadChunking(options) {
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
37
|
+
const { fileName, locationId, organizationId, base64 } = options;
|
38
|
+
const totalChunks = Math.ceil(base64.length / (this.chunkSizeKb * 1024));
|
39
|
+
const initialize = yield this.post('/initialize', {
|
40
|
+
proxyToken: this.proxyToken,
|
41
|
+
fileName,
|
42
|
+
personId: this.person.id,
|
43
|
+
totalChunks,
|
44
|
+
locationId,
|
45
|
+
organizationId,
|
46
|
+
});
|
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
|
+
let upload = {};
|
52
|
+
for (let i = 0; i < chunks.length; i++) {
|
53
|
+
const chunk = chunks[i];
|
54
|
+
upload = yield this.post('/upload', {
|
55
|
+
chunkIdx: i,
|
56
|
+
base64: chunk,
|
57
|
+
}, {
|
58
|
+
'x-upload-token': initialize.uploadToken,
|
59
|
+
});
|
60
|
+
}
|
61
|
+
const file = upload.file;
|
62
|
+
return file;
|
63
|
+
});
|
64
|
+
}
|
65
|
+
get chunkSizeKb() {
|
66
|
+
return ChunkingUploaderImpl.chunkSizeKb;
|
67
|
+
}
|
68
|
+
doesSupportChunking() {
|
69
|
+
return !!this.restEndpointUrl;
|
70
|
+
}
|
71
|
+
emitUpload(options) {
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
73
|
+
const { organizationId, locationId, base64, fileName } = options;
|
74
|
+
const client = yield this.connectToApi();
|
75
|
+
const [{ file }] = yield client.emitAndFlattenResponses('files.upload::v2022_05_13', {
|
76
|
+
target: {
|
77
|
+
organizationId,
|
78
|
+
locationId,
|
79
|
+
},
|
80
|
+
payload: {
|
81
|
+
base64Body: base64,
|
82
|
+
name: fileName,
|
83
|
+
},
|
84
|
+
});
|
85
|
+
const f = file;
|
86
|
+
return f;
|
87
|
+
});
|
88
|
+
}
|
89
|
+
loadRestEndpoint() {
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
91
|
+
const client = yield this.connectToApi();
|
92
|
+
const [{ restEndpointUrl }] = yield client.emitAndFlattenResponses('files.get-upload-endpoint::v2022_05_13');
|
93
|
+
this.restEndpointUrl = restEndpointUrl;
|
94
|
+
});
|
95
|
+
}
|
96
|
+
login() {
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
98
|
+
const client = yield this.connectToApi();
|
99
|
+
const [{ type, auth }] = yield client.emitAndFlattenResponses('whoami::v2020_12_25');
|
100
|
+
if (type === 'anonymous') {
|
101
|
+
throw new SpruceError({
|
102
|
+
code: 'UNAUTHORIZED',
|
103
|
+
friendlyMessage: `You have to be logged in before you can upload any files.`,
|
104
|
+
endpoint: 'whoami::v2020_12_25',
|
105
|
+
});
|
106
|
+
}
|
107
|
+
this.person = auth.person;
|
108
|
+
this.proxyToken = client.getProxyToken();
|
109
|
+
return client;
|
110
|
+
});
|
111
|
+
}
|
112
|
+
post(path, postBody, headers) {
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
114
|
+
const response = yield this.fetch(this.restEndpointUrl + path, {
|
115
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, headers),
|
116
|
+
method: 'POST',
|
117
|
+
body: JSON.stringify(postBody),
|
118
|
+
});
|
119
|
+
const body = yield response.json();
|
120
|
+
if (response.status !== 200) {
|
121
|
+
throw new SpruceError({
|
122
|
+
code: response.status === 400 ? 'BAD_REQUEST' : 'UNAUTHORIZED',
|
123
|
+
friendlyMessage: body.errorMessage,
|
124
|
+
endpoint: this.restEndpointUrl + path,
|
125
|
+
});
|
126
|
+
}
|
127
|
+
return body;
|
128
|
+
});
|
129
|
+
}
|
130
|
+
get fetch() {
|
131
|
+
return ChunkingUploaderImpl.fetch;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
ChunkingUploaderImpl.fetch = fetch;
|
135
|
+
ChunkingUploaderImpl.chunkSizeKb = 1024;
|
136
|
+
export default ChunkingUploaderImpl;
|
package/build/index-module.d.ts
CHANGED
package/build/index-module.js
CHANGED
@@ -17,7 +17,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
18
18
|
};
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
20
|
-
exports.FileUploaderImpl = void 0;
|
20
|
+
exports.ChunkingUploaderImpl = exports.FileUploaderImpl = void 0;
|
21
21
|
var FileUploader_1 = require("./uploading/FileUploader");
|
22
22
|
Object.defineProperty(exports, "FileUploaderImpl", { enumerable: true, get: function () { return __importDefault(FileUploader_1).default; } });
|
23
23
|
__exportStar(require("./uploading/FileUploader"), exports);
|
24
|
+
var ChunkingUploader_1 = require("./uploading/ChunkingUploader");
|
25
|
+
Object.defineProperty(exports, "ChunkingUploaderImpl", { enumerable: true, get: function () { return __importDefault(ChunkingUploader_1).default; } });
|
26
|
+
__exportStar(require("./uploading/ChunkingUploader"), exports);
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { MercuryConnectFactory } from '@sprucelabs/mercury-client';
|
2
|
+
import { UploadedFile } from '../files.types';
|
3
|
+
export default class ChunkingUploaderImpl implements ChunkingUploader {
|
4
|
+
private connectToApi;
|
5
|
+
static fetch: typeof fetch;
|
6
|
+
static chunkSizeKb: number;
|
7
|
+
private restEndpointUrl?;
|
8
|
+
private person;
|
9
|
+
private proxyToken;
|
10
|
+
protected constructor(connectToApi: MercuryConnectFactory);
|
11
|
+
static Uploader(connectToApi: MercuryConnectFactory): ChunkingUploaderImpl;
|
12
|
+
upload(options: UploadOptions): Promise<{
|
13
|
+
file: UploadedFile;
|
14
|
+
}>;
|
15
|
+
private uploadChunking;
|
16
|
+
private get chunkSizeKb();
|
17
|
+
private doesSupportChunking;
|
18
|
+
private emitUpload;
|
19
|
+
private loadRestEndpoint;
|
20
|
+
private login;
|
21
|
+
private post;
|
22
|
+
private get fetch();
|
23
|
+
}
|
24
|
+
export interface ChunkingUploader {
|
25
|
+
upload(options: UploadOptions): Promise<{
|
26
|
+
file: UploadedFile;
|
27
|
+
}>;
|
28
|
+
}
|
29
|
+
export interface UploadOptions {
|
30
|
+
fileName: string;
|
31
|
+
base64: string;
|
32
|
+
locationId?: string;
|
33
|
+
organizationId?: string;
|
34
|
+
}
|
@@ -0,0 +1,123 @@
|
|
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 SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
8
|
+
class ChunkingUploaderImpl {
|
9
|
+
constructor(connectToApi) {
|
10
|
+
this.connectToApi = connectToApi;
|
11
|
+
}
|
12
|
+
static Uploader(connectToApi) {
|
13
|
+
(0, schema_1.assertOptions)({ connectToApi }, ['connectToApi']);
|
14
|
+
return new this(connectToApi);
|
15
|
+
}
|
16
|
+
async upload(options) {
|
17
|
+
const { fileName, locationId, organizationId, base64 } = (0, schema_1.assertOptions)(options, ['fileName', 'base64']);
|
18
|
+
await this.login();
|
19
|
+
await this.loadRestEndpoint();
|
20
|
+
const method = this.doesSupportChunking() ? 'uploadChunking' : 'emitUpload';
|
21
|
+
const file = await this[method]({
|
22
|
+
fileName,
|
23
|
+
locationId,
|
24
|
+
organizationId,
|
25
|
+
base64,
|
26
|
+
});
|
27
|
+
return { file };
|
28
|
+
}
|
29
|
+
async uploadChunking(options) {
|
30
|
+
const { fileName, locationId, organizationId, base64 } = options;
|
31
|
+
const totalChunks = Math.ceil(base64.length / (this.chunkSizeKb * 1024));
|
32
|
+
const initialize = await this.post('/initialize', {
|
33
|
+
proxyToken: this.proxyToken,
|
34
|
+
fileName,
|
35
|
+
personId: this.person.id,
|
36
|
+
totalChunks,
|
37
|
+
locationId,
|
38
|
+
organizationId,
|
39
|
+
});
|
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
|
+
}
|
44
|
+
let upload = {};
|
45
|
+
for (let i = 0; i < chunks.length; i++) {
|
46
|
+
const chunk = chunks[i];
|
47
|
+
upload = await this.post('/upload', {
|
48
|
+
chunkIdx: i,
|
49
|
+
base64: chunk,
|
50
|
+
}, {
|
51
|
+
'x-upload-token': initialize.uploadToken,
|
52
|
+
});
|
53
|
+
}
|
54
|
+
const file = upload.file;
|
55
|
+
return file;
|
56
|
+
}
|
57
|
+
get chunkSizeKb() {
|
58
|
+
return ChunkingUploaderImpl.chunkSizeKb;
|
59
|
+
}
|
60
|
+
doesSupportChunking() {
|
61
|
+
return !!this.restEndpointUrl;
|
62
|
+
}
|
63
|
+
async emitUpload(options) {
|
64
|
+
const { organizationId, locationId, base64, fileName } = options;
|
65
|
+
const client = await this.connectToApi();
|
66
|
+
const [{ file }] = await client.emitAndFlattenResponses('files.upload::v2022_05_13', {
|
67
|
+
target: {
|
68
|
+
organizationId,
|
69
|
+
locationId,
|
70
|
+
},
|
71
|
+
payload: {
|
72
|
+
base64Body: base64,
|
73
|
+
name: fileName,
|
74
|
+
},
|
75
|
+
});
|
76
|
+
const f = file;
|
77
|
+
return f;
|
78
|
+
}
|
79
|
+
async loadRestEndpoint() {
|
80
|
+
const client = await this.connectToApi();
|
81
|
+
const [{ restEndpointUrl }] = await client.emitAndFlattenResponses('files.get-upload-endpoint::v2022_05_13');
|
82
|
+
this.restEndpointUrl = restEndpointUrl;
|
83
|
+
}
|
84
|
+
async login() {
|
85
|
+
const client = await this.connectToApi();
|
86
|
+
const [{ type, auth }] = await client.emitAndFlattenResponses('whoami::v2020_12_25');
|
87
|
+
if (type === 'anonymous') {
|
88
|
+
throw new SpruceError_1.default({
|
89
|
+
code: 'UNAUTHORIZED',
|
90
|
+
friendlyMessage: `You have to be logged in before you can upload any files.`,
|
91
|
+
endpoint: 'whoami::v2020_12_25',
|
92
|
+
});
|
93
|
+
}
|
94
|
+
this.person = auth.person;
|
95
|
+
this.proxyToken = client.getProxyToken();
|
96
|
+
return client;
|
97
|
+
}
|
98
|
+
async post(path, postBody, headers) {
|
99
|
+
const response = await this.fetch(this.restEndpointUrl + path, {
|
100
|
+
headers: {
|
101
|
+
'Content-Type': 'application/json',
|
102
|
+
...headers,
|
103
|
+
},
|
104
|
+
method: 'POST',
|
105
|
+
body: JSON.stringify(postBody),
|
106
|
+
});
|
107
|
+
const body = await response.json();
|
108
|
+
if (response.status !== 200) {
|
109
|
+
throw new SpruceError_1.default({
|
110
|
+
code: response.status === 400 ? 'BAD_REQUEST' : 'UNAUTHORIZED',
|
111
|
+
friendlyMessage: body.errorMessage,
|
112
|
+
endpoint: this.restEndpointUrl + path,
|
113
|
+
});
|
114
|
+
}
|
115
|
+
return body;
|
116
|
+
}
|
117
|
+
get fetch() {
|
118
|
+
return ChunkingUploaderImpl.fetch;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
ChunkingUploaderImpl.fetch = fetch;
|
122
|
+
ChunkingUploaderImpl.chunkSizeKb = 1024;
|
123
|
+
exports.default = ChunkingUploaderImpl;
|
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.0
|
4
|
+
"version": "15.1.0",
|
5
5
|
"skill": {
|
6
6
|
"namespace": "files"
|
7
7
|
},
|
@@ -25,6 +25,11 @@
|
|
25
25
|
"build/uploading/FileUploader.d.ts",
|
26
26
|
"build/uploading/FileUploader.js",
|
27
27
|
|
28
|
+
"build/esm/uploading/ChunkingUploader.d.ts",
|
29
|
+
"build/esm/uploading/ChunkingUploader.js",
|
30
|
+
"build/uploading/ChunkingUploader.d.ts",
|
31
|
+
"build/uploading/ChunkingUploader.js",
|
32
|
+
|
28
33
|
"build/esm/uploading/LocalUploadStrategy.d.ts",
|
29
34
|
"build/esm/uploading/LocalUploadStrategy.js",
|
30
35
|
"build/uploading/LocalUploadStrategy.d.ts",
|