@balena/pinejs 21.4.0-build-large-file-uploads-2-917f66f4e89b38adb4b0671769e30d678c031448-1 → 21.4.0-build-add-actions-example-c20002c2bf051233b333158f3b7a0719f6cf6e4e-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.
- package/.versionbot/CHANGELOG.yml +3 -3
- package/CHANGELOG.md +2 -2
- package/out/actions/index.d.ts +32 -0
- package/out/actions/index.js +67 -0
- package/out/actions/index.js.map +1 -0
- package/out/config-loader/config-loader.js +7 -6
- package/out/config-loader/config-loader.js.map +1 -1
- package/out/sbvr-api/sbvr-utils.d.ts +1 -0
- package/out/sbvr-api/sbvr-utils.js +8 -1
- package/out/sbvr-api/sbvr-utils.js.map +1 -1
- package/out/webresource-handler/actions/beginUpload.d.ts +3 -0
- package/out/webresource-handler/actions/beginUpload.js +99 -0
- package/out/webresource-handler/actions/beginUpload.js.map +1 -0
- package/out/webresource-handler/actions/canceUpload.d.ts +3 -0
- package/out/webresource-handler/actions/canceUpload.js +59 -0
- package/out/webresource-handler/actions/canceUpload.js.map +1 -0
- package/out/webresource-handler/actions/commitUpload.d.ts +3 -0
- package/out/webresource-handler/actions/commitUpload.js +85 -0
- package/out/webresource-handler/actions/commitUpload.js.map +1 -0
- package/out/webresource-handler/index.d.ts +1 -1
- package/out/webresource-handler/index.js +11 -5
- package/out/webresource-handler/index.js.map +1 -1
- package/out/webresource-handler/multiparUpload.d.ts +4 -0
- package/out/webresource-handler/multiparUpload.js +14 -0
- package/out/webresource-handler/multiparUpload.js.map +1 -0
- package/package.json +3 -3
- package/src/actions/index.ts +142 -0
- package/src/config-loader/config-loader.ts +8 -8
- package/src/sbvr-api/sbvr-utils.ts +10 -1
- package/src/webresource-handler/actions/beginUpload.ts +160 -0
- package/src/webresource-handler/actions/canceUpload.ts +93 -0
- package/src/webresource-handler/actions/commitUpload.ts +116 -0
- package/src/webresource-handler/index.ts +17 -14
- package/src/webresource-handler/multiparUpload.ts +23 -0
- package/out/webresource-handler/multipartUpload.d.ts +0 -12
- package/out/webresource-handler/multipartUpload.js +0 -249
- package/out/webresource-handler/multipartUpload.js.map +0 -1
- package/src/webresource-handler/multipartUpload.ts +0 -367
@@ -1,249 +0,0 @@
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
2
|
-
import { getWebResourceFields } from './index.js';
|
3
|
-
import { api } from '../sbvr-api/sbvr-utils.js';
|
4
|
-
import { errors, sbvrUtils } from '../server-glue/module.js';
|
5
|
-
import { webResource as webResourceEnv } from '../config-loader/env.js';
|
6
|
-
import * as permissions from '../sbvr-api/permissions.js';
|
7
|
-
export const isMultipartUploadAvailable = (webResourceHandler) => {
|
8
|
-
return (webResourceEnv.multipartUploadEnabled &&
|
9
|
-
webResourceHandler.multipartUpload != null);
|
10
|
-
};
|
11
|
-
export const multipartUploadHooks = (webResourceHandler) => {
|
12
|
-
return {
|
13
|
-
POSTPARSE: async ({ req, request, tx, api: applicationApi }) => {
|
14
|
-
if (request.odataQuery.property?.resource === 'beginUpload') {
|
15
|
-
const uploadParams = await validateBeginUpload(request, applicationApi, webResourceHandler);
|
16
|
-
tx = await sbvrUtils.db.transaction();
|
17
|
-
req.tx = tx;
|
18
|
-
request.tx = tx;
|
19
|
-
request.method = 'PATCH';
|
20
|
-
request.values = uploadParams;
|
21
|
-
request.odataQuery.resource = request.resourceName;
|
22
|
-
delete request.odataQuery.property;
|
23
|
-
request.custom.isAction = 'beginUpload';
|
24
|
-
}
|
25
|
-
else if (request.odataQuery.property?.resource === 'commitUpload') {
|
26
|
-
const commitPayload = await validateCommitUpload(request, applicationApi);
|
27
|
-
const webresource = await webResourceHandler.multipartUpload.commit({
|
28
|
-
fileKey: commitPayload.metadata.fileKey,
|
29
|
-
uploadId: commitPayload.metadata.uploadId,
|
30
|
-
filename: commitPayload.metadata.filename,
|
31
|
-
providerCommitData: commitPayload.providerCommitData,
|
32
|
-
});
|
33
|
-
await api.webresource.patch({
|
34
|
-
resource: 'multipart_upload',
|
35
|
-
body: {
|
36
|
-
status: 'completed',
|
37
|
-
},
|
38
|
-
options: {
|
39
|
-
$filter: {
|
40
|
-
uuid: commitPayload.uuid,
|
41
|
-
},
|
42
|
-
},
|
43
|
-
passthrough: {
|
44
|
-
tx: tx,
|
45
|
-
req: permissions.root,
|
46
|
-
},
|
47
|
-
});
|
48
|
-
request.method = 'PATCH';
|
49
|
-
request.values = {
|
50
|
-
[commitPayload.metadata.fieldName]: webresource,
|
51
|
-
};
|
52
|
-
request.odataQuery.resource = request.resourceName;
|
53
|
-
delete request.odataQuery.property;
|
54
|
-
request.custom.isAction = 'commitUpload';
|
55
|
-
request.custom.commitUploadPayload = webresource;
|
56
|
-
}
|
57
|
-
else if (request.odataQuery.property?.resource === 'cancelUpload') {
|
58
|
-
const { uuid, fileKey, uploadId } = await validateCancelPayload(request, applicationApi);
|
59
|
-
await webResourceHandler.multipartUpload.cancel({ fileKey, uploadId });
|
60
|
-
await api.webresource.patch({
|
61
|
-
resource: 'multipart_upload',
|
62
|
-
body: {
|
63
|
-
status: 'cancelled',
|
64
|
-
},
|
65
|
-
options: {
|
66
|
-
$filter: { uuid },
|
67
|
-
},
|
68
|
-
passthrough: {
|
69
|
-
tx: tx,
|
70
|
-
req: permissions.root,
|
71
|
-
},
|
72
|
-
});
|
73
|
-
request.method = 'GET';
|
74
|
-
request.odataQuery.resource = request.resourceName;
|
75
|
-
delete request.odataQuery.property;
|
76
|
-
request.custom.isAction = 'cancelUpload';
|
77
|
-
}
|
78
|
-
},
|
79
|
-
PRERESPOND: async ({ req, request, response, tx }) => {
|
80
|
-
if (request.custom.isAction === 'beginUpload') {
|
81
|
-
await tx.rollback();
|
82
|
-
response.statusCode = 200;
|
83
|
-
response.body = await beginUpload({
|
84
|
-
webResourceHandler,
|
85
|
-
odataRequest: request,
|
86
|
-
actorId: req.user?.actor,
|
87
|
-
});
|
88
|
-
}
|
89
|
-
else if (request.custom.isAction === 'commitUpload') {
|
90
|
-
response.body = await webResourceHandler.onPreRespond(request.custom.commitUploadPayload);
|
91
|
-
}
|
92
|
-
else if (request.custom.isAction === 'cancelUpload') {
|
93
|
-
response.statusCode = 204;
|
94
|
-
delete response.body;
|
95
|
-
}
|
96
|
-
},
|
97
|
-
};
|
98
|
-
};
|
99
|
-
const beginUpload = async ({ webResourceHandler, odataRequest, actorId, }) => {
|
100
|
-
const payload = odataRequest.values;
|
101
|
-
const fieldName = Object.keys(payload)[0];
|
102
|
-
const metadata = payload[fieldName];
|
103
|
-
const { fileKey, uploadId, uploadParts } = await webResourceHandler.multipartUpload.begin(fieldName, metadata);
|
104
|
-
const uuid = randomUUID();
|
105
|
-
return await sbvrUtils.db.transaction(async (tx) => {
|
106
|
-
try {
|
107
|
-
await api.webresource.post({
|
108
|
-
resource: 'multipart_upload',
|
109
|
-
body: {
|
110
|
-
uuid,
|
111
|
-
resource_name: odataRequest.resourceName,
|
112
|
-
field_name: fieldName,
|
113
|
-
resource_id: odataRequest.affectedIds?.[0],
|
114
|
-
upload_id: uploadId,
|
115
|
-
file_key: fileKey,
|
116
|
-
status: 'pending',
|
117
|
-
filename: metadata.filename,
|
118
|
-
content_type: metadata.content_type,
|
119
|
-
size: metadata.size,
|
120
|
-
chunk_size: metadata.chunk_size,
|
121
|
-
expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
122
|
-
is_created_by__actor: actorId,
|
123
|
-
},
|
124
|
-
passthrough: {
|
125
|
-
req: permissions.root,
|
126
|
-
tx,
|
127
|
-
},
|
128
|
-
});
|
129
|
-
return { [fieldName]: { uuid, uploadParts } };
|
130
|
-
}
|
131
|
-
catch (err) {
|
132
|
-
console.error('failed to start multipart upload', err);
|
133
|
-
throw new errors.BadRequestError('Failed to start multipart upload');
|
134
|
-
}
|
135
|
-
});
|
136
|
-
};
|
137
|
-
const validateBeginUpload = async (request, applicationApi, webResourceHandler) => {
|
138
|
-
await canAccess(request, applicationApi);
|
139
|
-
const fieldNames = Object.keys(request.values);
|
140
|
-
if (fieldNames.length !== 1) {
|
141
|
-
throw new errors.BadRequestError('You can only get upload url for one field at a time');
|
142
|
-
}
|
143
|
-
const [fieldName] = fieldNames;
|
144
|
-
const webResourceFields = getWebResourceFields(request);
|
145
|
-
if (!webResourceFields.includes(fieldName)) {
|
146
|
-
throw new errors.BadRequestError(`The provided field '${fieldName}' is not a valid webresource`);
|
147
|
-
}
|
148
|
-
const beginUploadPayload = parseBeginUploadPayload(request.values[fieldName], webResourceHandler);
|
149
|
-
if (beginUploadPayload == null) {
|
150
|
-
throw new errors.BadRequestError('Invalid file metadata');
|
151
|
-
}
|
152
|
-
const uploadMetadataCheck = {
|
153
|
-
...beginUploadPayload,
|
154
|
-
href: 'metadata_check_probe',
|
155
|
-
};
|
156
|
-
return { [fieldName]: uploadMetadataCheck };
|
157
|
-
};
|
158
|
-
const parseBeginUploadPayload = (payload, webResourceHandler) => {
|
159
|
-
if (payload == null || typeof payload !== 'object') {
|
160
|
-
return null;
|
161
|
-
}
|
162
|
-
let { filename, content_type, size, chunk_size } = payload;
|
163
|
-
if (typeof filename !== 'string' ||
|
164
|
-
typeof content_type !== 'string' ||
|
165
|
-
typeof size !== 'number' ||
|
166
|
-
(chunk_size != null && typeof chunk_size !== 'number') ||
|
167
|
-
(chunk_size != null &&
|
168
|
-
chunk_size < webResourceHandler.multipartUpload.getMinimumPartSize())) {
|
169
|
-
return null;
|
170
|
-
}
|
171
|
-
chunk_size ??= webResourceHandler.multipartUpload.getDefaultPartSize();
|
172
|
-
return { filename, content_type, size, chunk_size };
|
173
|
-
};
|
174
|
-
const validateCommitUpload = async (request, applicationApi) => {
|
175
|
-
await canAccess(request, applicationApi);
|
176
|
-
const { uuid, providerCommitData } = request.values;
|
177
|
-
if (typeof uuid !== 'string') {
|
178
|
-
throw new errors.BadRequestError('Invalid uuid type');
|
179
|
-
}
|
180
|
-
const [multipartUpload] = await api.webresource.get({
|
181
|
-
resource: 'multipart_upload',
|
182
|
-
options: {
|
183
|
-
$select: ['id', 'file_key', 'upload_id', 'field_name', 'filename'],
|
184
|
-
$filter: {
|
185
|
-
uuid,
|
186
|
-
status: 'pending',
|
187
|
-
expiry_date: { $gt: { $now: {} } },
|
188
|
-
},
|
189
|
-
},
|
190
|
-
passthrough: {
|
191
|
-
tx: request.tx,
|
192
|
-
req: permissions.rootRead,
|
193
|
-
},
|
194
|
-
});
|
195
|
-
if (multipartUpload == null) {
|
196
|
-
throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
|
197
|
-
}
|
198
|
-
const metadata = {
|
199
|
-
fileKey: multipartUpload.file_key,
|
200
|
-
uploadId: multipartUpload.upload_id,
|
201
|
-
filename: multipartUpload.filename,
|
202
|
-
fieldName: multipartUpload.field_name,
|
203
|
-
};
|
204
|
-
return { uuid, providerCommitData, metadata };
|
205
|
-
};
|
206
|
-
const validateCancelPayload = async (request, applicationApi) => {
|
207
|
-
await canAccess(request, applicationApi);
|
208
|
-
const { uuid } = request.values;
|
209
|
-
if (typeof uuid !== 'string') {
|
210
|
-
throw new errors.BadRequestError('Invalid uuid type');
|
211
|
-
}
|
212
|
-
const [multipartUpload] = await api.webresource.get({
|
213
|
-
resource: 'multipart_upload',
|
214
|
-
options: {
|
215
|
-
$select: ['id', 'file_key', 'upload_id'],
|
216
|
-
$filter: {
|
217
|
-
uuid,
|
218
|
-
status: 'pending',
|
219
|
-
expiry_date: { $gt: { $now: {} } },
|
220
|
-
},
|
221
|
-
},
|
222
|
-
passthrough: {
|
223
|
-
tx: request.tx,
|
224
|
-
req: permissions.rootRead,
|
225
|
-
},
|
226
|
-
});
|
227
|
-
if (multipartUpload == null) {
|
228
|
-
throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
|
229
|
-
}
|
230
|
-
return {
|
231
|
-
uuid,
|
232
|
-
fileKey: multipartUpload.file_key,
|
233
|
-
uploadId: multipartUpload.upload_id,
|
234
|
-
};
|
235
|
-
};
|
236
|
-
const canAccess = async (request, applicationApi) => {
|
237
|
-
if (request.odataQuery.key == null) {
|
238
|
-
throw new errors.BadRequestError();
|
239
|
-
}
|
240
|
-
const canAccessUrl = request.url
|
241
|
-
.slice(1)
|
242
|
-
.replace(/(beginUpload|commitUpload|cancelUpload)$/, 'canAccess');
|
243
|
-
await applicationApi.request({
|
244
|
-
method: 'POST',
|
245
|
-
url: canAccessUrl,
|
246
|
-
body: { method: 'PATCH' },
|
247
|
-
});
|
248
|
-
};
|
249
|
-
//# sourceMappingURL=multipartUpload.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"multipartUpload.js","sourceRoot":"","sources":["../../src/webresource-handler/multipartUpload.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD,OAAO,EAAE,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAEhD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,WAAW,IAAI,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,KAAK,WAAW,MAAM,4BAA4B,CAAC;AAe1D,MAAM,CAAC,MAAM,0BAA0B,GAAG,CACzC,kBAAsC,EACS,EAAE;IACjD,OAAO,CACN,cAAc,CAAC,sBAAsB;QACrC,kBAAkB,CAAC,eAAe,IAAI,IAAI,CAC1C,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CACnC,kBAA0C,EACxB,EAAE;IACpB,OAAO;QACN,SAAS,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,cAAc,EAAE,EAAE,EAAE;YAC9D,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAQ,KAAK,aAAa,EAAE,CAAC;gBAC7D,MAAM,YAAY,GAAG,MAAM,mBAAmB,CAC7C,OAAO,EACP,cAAc,EACd,kBAA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C;IAED,OAAO;QACN,IAAI;QACJ,OAAO,EAAE,eAAe,CAAC,QAAQ;QACjC,QAAQ,EAAE,eAAe,CAAC,SAAS;KACnC,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,KAAK,EACtB,OAAqB,EACrB,cAA4B,EAC3B,EAAE;IACH,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,IAAI,IAAI,EAAE,CAAC;QACpC,MAAM,IAAI,MAAM,CAAC,eAAe,EAAE,CAAC;IACpC,CAAC;IAED,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG;SAC9B,KAAK,CAAC,CAAC,CAAC;SACR,OAAO,CAAC,0CAA0C,EAAE,WAAW,CAAC,CAAC;IAEnE,MAAM,cAAc,CAAC,OAAO,CAAC;QAC5B,MAAM,EAAE,MAAM;QACd,GAAG,EAAE,YAAY;QACjB,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE;KACzB,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
@@ -1,367 +0,0 @@
|
|
1
|
-
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
|
2
|
-
import { randomUUID } from 'node:crypto';
|
3
|
-
import type { AnyObject } from 'pinejs-client-core';
|
4
|
-
import type {
|
5
|
-
BeginMultipartUploadPayload,
|
6
|
-
UploadPart,
|
7
|
-
WebResourceHandler,
|
8
|
-
} from './index.js';
|
9
|
-
import { getWebResourceFields } from './index.js';
|
10
|
-
import type { PinejsClient } from '../sbvr-api/sbvr-utils.js';
|
11
|
-
import { api } from '../sbvr-api/sbvr-utils.js';
|
12
|
-
import type { ODataRequest } from '../sbvr-api/uri-parser.js';
|
13
|
-
import { errors, sbvrUtils } from '../server-glue/module.js';
|
14
|
-
import { webResource as webResourceEnv } from '../config-loader/env.js';
|
15
|
-
import * as permissions from '../sbvr-api/permissions.js';
|
16
|
-
|
17
|
-
type BeginUploadDbCheck = BeginMultipartUploadPayload & WebResource;
|
18
|
-
|
19
|
-
export interface BeginUploadResponse {
|
20
|
-
[fieldName: string]: {
|
21
|
-
uuid: string;
|
22
|
-
uploadParts: UploadPart[];
|
23
|
-
};
|
24
|
-
}
|
25
|
-
|
26
|
-
// Is WebResourceHandler but with beginMultipartUpload and commitMultipartUpload as not optional
|
27
|
-
type MultipartUploadHandler = WebResourceHandler &
|
28
|
-
Required<Pick<WebResourceHandler, 'multipartUpload'>>;
|
29
|
-
|
30
|
-
export const isMultipartUploadAvailable = (
|
31
|
-
webResourceHandler: WebResourceHandler,
|
32
|
-
): webResourceHandler is MultipartUploadHandler => {
|
33
|
-
return (
|
34
|
-
webResourceEnv.multipartUploadEnabled &&
|
35
|
-
webResourceHandler.multipartUpload != null
|
36
|
-
);
|
37
|
-
};
|
38
|
-
|
39
|
-
export const multipartUploadHooks = (
|
40
|
-
webResourceHandler: MultipartUploadHandler,
|
41
|
-
): sbvrUtils.Hooks => {
|
42
|
-
return {
|
43
|
-
POSTPARSE: async ({ req, request, tx, api: applicationApi }) => {
|
44
|
-
if (request.odataQuery.property?.resource === 'beginUpload') {
|
45
|
-
const uploadParams = await validateBeginUpload(
|
46
|
-
request,
|
47
|
-
applicationApi,
|
48
|
-
webResourceHandler,
|
49
|
-
);
|
50
|
-
|
51
|
-
// This transaction is necessary because beginUpload requests
|
52
|
-
// will rollback the transaction (in order to first validate)
|
53
|
-
// The metadata requested. If we don't pass any transaction
|
54
|
-
// It will use the default transaction handler which will error out
|
55
|
-
// on any rollback.
|
56
|
-
tx = await sbvrUtils.db.transaction();
|
57
|
-
req.tx = tx;
|
58
|
-
request.tx = tx;
|
59
|
-
|
60
|
-
request.method = 'PATCH';
|
61
|
-
request.values = uploadParams;
|
62
|
-
request.odataQuery.resource = request.resourceName;
|
63
|
-
delete request.odataQuery.property;
|
64
|
-
request.custom.isAction = 'beginUpload';
|
65
|
-
} else if (request.odataQuery.property?.resource === 'commitUpload') {
|
66
|
-
const commitPayload = await validateCommitUpload(
|
67
|
-
request,
|
68
|
-
applicationApi,
|
69
|
-
);
|
70
|
-
|
71
|
-
const webresource = await webResourceHandler.multipartUpload.commit({
|
72
|
-
fileKey: commitPayload.metadata.fileKey,
|
73
|
-
uploadId: commitPayload.metadata.uploadId,
|
74
|
-
filename: commitPayload.metadata.filename,
|
75
|
-
providerCommitData: commitPayload.providerCommitData,
|
76
|
-
});
|
77
|
-
|
78
|
-
await api.webresource.patch({
|
79
|
-
resource: 'multipart_upload',
|
80
|
-
body: {
|
81
|
-
status: 'completed',
|
82
|
-
},
|
83
|
-
options: {
|
84
|
-
$filter: {
|
85
|
-
uuid: commitPayload.uuid,
|
86
|
-
},
|
87
|
-
},
|
88
|
-
passthrough: {
|
89
|
-
tx: tx,
|
90
|
-
req: permissions.root,
|
91
|
-
},
|
92
|
-
});
|
93
|
-
|
94
|
-
request.method = 'PATCH';
|
95
|
-
request.values = {
|
96
|
-
[commitPayload.metadata.fieldName]: webresource,
|
97
|
-
};
|
98
|
-
request.odataQuery.resource = request.resourceName;
|
99
|
-
delete request.odataQuery.property;
|
100
|
-
request.custom.isAction = 'commitUpload';
|
101
|
-
request.custom.commitUploadPayload = webresource;
|
102
|
-
} else if (request.odataQuery.property?.resource === 'cancelUpload') {
|
103
|
-
const { uuid, fileKey, uploadId } = await validateCancelPayload(
|
104
|
-
request,
|
105
|
-
applicationApi,
|
106
|
-
);
|
107
|
-
|
108
|
-
await webResourceHandler.multipartUpload.cancel({ fileKey, uploadId });
|
109
|
-
|
110
|
-
await api.webresource.patch({
|
111
|
-
resource: 'multipart_upload',
|
112
|
-
body: {
|
113
|
-
status: 'cancelled',
|
114
|
-
},
|
115
|
-
options: {
|
116
|
-
$filter: { uuid },
|
117
|
-
},
|
118
|
-
passthrough: {
|
119
|
-
tx: tx,
|
120
|
-
req: permissions.root,
|
121
|
-
},
|
122
|
-
});
|
123
|
-
|
124
|
-
request.method = 'GET';
|
125
|
-
request.odataQuery.resource = request.resourceName;
|
126
|
-
delete request.odataQuery.property;
|
127
|
-
request.custom.isAction = 'cancelUpload';
|
128
|
-
}
|
129
|
-
},
|
130
|
-
PRERESPOND: async ({ req, request, response, tx }) => {
|
131
|
-
if (request.custom.isAction === 'beginUpload') {
|
132
|
-
// In the case where the transaction has failed because it had invalid payload
|
133
|
-
// such as breaking a db constraint, this hook wouldn't have been called
|
134
|
-
// and would rather throw with the rule it failed to validate
|
135
|
-
// We rollback here as the patch was just a way to validate the upload payload
|
136
|
-
await tx.rollback();
|
137
|
-
|
138
|
-
response.statusCode = 200;
|
139
|
-
response.body = await beginUpload({
|
140
|
-
webResourceHandler,
|
141
|
-
odataRequest: request,
|
142
|
-
actorId: req.user?.actor,
|
143
|
-
});
|
144
|
-
} else if (request.custom.isAction === 'commitUpload') {
|
145
|
-
response.body = await webResourceHandler.onPreRespond(
|
146
|
-
request.custom.commitUploadPayload,
|
147
|
-
);
|
148
|
-
} else if (request.custom.isAction === 'cancelUpload') {
|
149
|
-
response.statusCode = 204;
|
150
|
-
delete response.body;
|
151
|
-
}
|
152
|
-
},
|
153
|
-
};
|
154
|
-
};
|
155
|
-
|
156
|
-
const beginUpload = async ({
|
157
|
-
webResourceHandler,
|
158
|
-
odataRequest,
|
159
|
-
actorId,
|
160
|
-
}: {
|
161
|
-
webResourceHandler: MultipartUploadHandler;
|
162
|
-
odataRequest: ODataRequest;
|
163
|
-
actorId?: number;
|
164
|
-
}): Promise<BeginUploadResponse> => {
|
165
|
-
const payload = odataRequest.values as {
|
166
|
-
[x: string]: BeginMultipartUploadPayload;
|
167
|
-
};
|
168
|
-
const fieldName = Object.keys(payload)[0];
|
169
|
-
const metadata = payload[fieldName];
|
170
|
-
const { fileKey, uploadId, uploadParts } =
|
171
|
-
await webResourceHandler.multipartUpload.begin(fieldName, metadata);
|
172
|
-
const uuid = randomUUID();
|
173
|
-
|
174
|
-
return await sbvrUtils.db.transaction(async (tx) => {
|
175
|
-
try {
|
176
|
-
await api.webresource.post({
|
177
|
-
resource: 'multipart_upload',
|
178
|
-
body: {
|
179
|
-
uuid,
|
180
|
-
resource_name: odataRequest.resourceName,
|
181
|
-
field_name: fieldName,
|
182
|
-
resource_id: odataRequest.affectedIds?.[0],
|
183
|
-
upload_id: uploadId,
|
184
|
-
file_key: fileKey,
|
185
|
-
status: 'pending',
|
186
|
-
filename: metadata.filename,
|
187
|
-
content_type: metadata.content_type,
|
188
|
-
size: metadata.size,
|
189
|
-
chunk_size: metadata.chunk_size,
|
190
|
-
expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days in ms
|
191
|
-
is_created_by__actor: actorId,
|
192
|
-
},
|
193
|
-
passthrough: {
|
194
|
-
req: permissions.root,
|
195
|
-
tx,
|
196
|
-
},
|
197
|
-
});
|
198
|
-
return { [fieldName]: { uuid, uploadParts } };
|
199
|
-
} catch (err) {
|
200
|
-
console.error('failed to start multipart upload', err);
|
201
|
-
throw new errors.BadRequestError('Failed to start multipart upload');
|
202
|
-
}
|
203
|
-
});
|
204
|
-
};
|
205
|
-
|
206
|
-
const validateBeginUpload = async (
|
207
|
-
request: ODataRequest,
|
208
|
-
applicationApi: PinejsClient,
|
209
|
-
webResourceHandler: MultipartUploadHandler,
|
210
|
-
) => {
|
211
|
-
await canAccess(request, applicationApi);
|
212
|
-
|
213
|
-
const fieldNames = Object.keys(request.values);
|
214
|
-
if (fieldNames.length !== 1) {
|
215
|
-
throw new errors.BadRequestError(
|
216
|
-
'You can only get upload url for one field at a time',
|
217
|
-
);
|
218
|
-
}
|
219
|
-
|
220
|
-
const [fieldName] = fieldNames;
|
221
|
-
const webResourceFields = getWebResourceFields(request);
|
222
|
-
if (!webResourceFields.includes(fieldName)) {
|
223
|
-
throw new errors.BadRequestError(
|
224
|
-
`The provided field '${fieldName}' is not a valid webresource`,
|
225
|
-
);
|
226
|
-
}
|
227
|
-
|
228
|
-
const beginUploadPayload = parseBeginUploadPayload(
|
229
|
-
request.values[fieldName],
|
230
|
-
webResourceHandler,
|
231
|
-
);
|
232
|
-
if (beginUploadPayload == null) {
|
233
|
-
throw new errors.BadRequestError('Invalid file metadata');
|
234
|
-
}
|
235
|
-
|
236
|
-
const uploadMetadataCheck: BeginUploadDbCheck = {
|
237
|
-
...beginUploadPayload,
|
238
|
-
// This is "probe" request. We don't actually store anything on the application table yet
|
239
|
-
// We just avoid creating the application record if it would fail anyway for some other db constraint
|
240
|
-
href: 'metadata_check_probe',
|
241
|
-
};
|
242
|
-
|
243
|
-
return { [fieldName]: uploadMetadataCheck };
|
244
|
-
};
|
245
|
-
|
246
|
-
const parseBeginUploadPayload = (
|
247
|
-
payload: AnyObject,
|
248
|
-
webResourceHandler: MultipartUploadHandler,
|
249
|
-
): BeginMultipartUploadPayload | null => {
|
250
|
-
if (payload == null || typeof payload !== 'object') {
|
251
|
-
return null;
|
252
|
-
}
|
253
|
-
|
254
|
-
let { filename, content_type, size, chunk_size } = payload;
|
255
|
-
if (
|
256
|
-
typeof filename !== 'string' ||
|
257
|
-
typeof content_type !== 'string' ||
|
258
|
-
typeof size !== 'number' ||
|
259
|
-
(chunk_size != null && typeof chunk_size !== 'number') ||
|
260
|
-
(chunk_size != null &&
|
261
|
-
chunk_size < webResourceHandler.multipartUpload.getMinimumPartSize())
|
262
|
-
) {
|
263
|
-
return null;
|
264
|
-
}
|
265
|
-
|
266
|
-
chunk_size ??= webResourceHandler.multipartUpload.getDefaultPartSize();
|
267
|
-
|
268
|
-
return { filename, content_type, size, chunk_size };
|
269
|
-
};
|
270
|
-
|
271
|
-
const validateCommitUpload = async (
|
272
|
-
request: ODataRequest,
|
273
|
-
applicationApi: PinejsClient,
|
274
|
-
) => {
|
275
|
-
await canAccess(request, applicationApi);
|
276
|
-
|
277
|
-
const { uuid, providerCommitData } = request.values;
|
278
|
-
if (typeof uuid !== 'string') {
|
279
|
-
throw new errors.BadRequestError('Invalid uuid type');
|
280
|
-
}
|
281
|
-
|
282
|
-
const [multipartUpload] = await api.webresource.get({
|
283
|
-
resource: 'multipart_upload',
|
284
|
-
options: {
|
285
|
-
$select: ['id', 'file_key', 'upload_id', 'field_name', 'filename'],
|
286
|
-
$filter: {
|
287
|
-
uuid,
|
288
|
-
status: 'pending',
|
289
|
-
expiry_date: { $gt: { $now: {} } },
|
290
|
-
},
|
291
|
-
},
|
292
|
-
passthrough: {
|
293
|
-
tx: request.tx,
|
294
|
-
req: permissions.rootRead,
|
295
|
-
},
|
296
|
-
});
|
297
|
-
|
298
|
-
if (multipartUpload == null) {
|
299
|
-
throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
|
300
|
-
}
|
301
|
-
|
302
|
-
const metadata = {
|
303
|
-
fileKey: multipartUpload.file_key,
|
304
|
-
uploadId: multipartUpload.upload_id,
|
305
|
-
filename: multipartUpload.filename,
|
306
|
-
fieldName: multipartUpload.field_name,
|
307
|
-
};
|
308
|
-
|
309
|
-
return { uuid, providerCommitData, metadata };
|
310
|
-
};
|
311
|
-
|
312
|
-
const validateCancelPayload = async (
|
313
|
-
request: ODataRequest,
|
314
|
-
applicationApi: PinejsClient,
|
315
|
-
) => {
|
316
|
-
await canAccess(request, applicationApi);
|
317
|
-
|
318
|
-
const { uuid } = request.values;
|
319
|
-
if (typeof uuid !== 'string') {
|
320
|
-
throw new errors.BadRequestError('Invalid uuid type');
|
321
|
-
}
|
322
|
-
|
323
|
-
const [multipartUpload] = await api.webresource.get({
|
324
|
-
resource: 'multipart_upload',
|
325
|
-
options: {
|
326
|
-
$select: ['id', 'file_key', 'upload_id'],
|
327
|
-
$filter: {
|
328
|
-
uuid,
|
329
|
-
status: 'pending',
|
330
|
-
expiry_date: { $gt: { $now: {} } },
|
331
|
-
},
|
332
|
-
},
|
333
|
-
passthrough: {
|
334
|
-
tx: request.tx,
|
335
|
-
req: permissions.rootRead,
|
336
|
-
},
|
337
|
-
});
|
338
|
-
|
339
|
-
if (multipartUpload == null) {
|
340
|
-
throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
|
341
|
-
}
|
342
|
-
|
343
|
-
return {
|
344
|
-
uuid,
|
345
|
-
fileKey: multipartUpload.file_key,
|
346
|
-
uploadId: multipartUpload.upload_id,
|
347
|
-
};
|
348
|
-
};
|
349
|
-
|
350
|
-
const canAccess = async (
|
351
|
-
request: ODataRequest,
|
352
|
-
applicationApi: PinejsClient,
|
353
|
-
) => {
|
354
|
-
if (request.odataQuery.key == null) {
|
355
|
-
throw new errors.BadRequestError();
|
356
|
-
}
|
357
|
-
|
358
|
-
const canAccessUrl = request.url
|
359
|
-
.slice(1)
|
360
|
-
.replace(/(beginUpload|commitUpload|cancelUpload)$/, 'canAccess');
|
361
|
-
|
362
|
-
await applicationApi.request({
|
363
|
-
method: 'POST',
|
364
|
-
url: canAccessUrl,
|
365
|
-
body: { method: 'PATCH' },
|
366
|
-
});
|
367
|
-
};
|