@balena/pinejs 21.5.0-build-add-actions-example-44fc8db2db7a11e2f1f01af695a0bfe4b38ad880-1 → 21.5.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/.pinejs-cache.json +1 -1
- package/.versionbot/CHANGELOG.yml +3 -19
- package/CHANGELOG.md +1 -3
- package/out/config-loader/config-loader.js +6 -7
- package/out/config-loader/config-loader.js.map +1 -1
- package/out/config-loader/env.d.ts +0 -4
- package/out/config-loader/env.js +0 -4
- package/out/config-loader/env.js.map +1 -1
- package/out/server-glue/module.js +0 -2
- package/out/server-glue/module.js.map +1 -1
- package/out/tasks/index.d.ts +1 -1
- package/out/tasks/index.js +1 -1
- package/out/tasks/index.js.map +1 -1
- package/out/webresource-handler/index.d.ts +0 -9
- package/out/webresource-handler/index.js +23 -48
- package/out/webresource-handler/index.js.map +1 -1
- package/package.json +5 -5
- package/src/config-loader/config-loader.ts +8 -8
- package/src/config-loader/env.ts +0 -11
- package/src/server-glue/module.ts +0 -2
- package/src/tasks/index.ts +1 -1
- package/src/webresource-handler/index.ts +24 -69
- package/out/webresource-handler/actions/beginUpload.d.ts +0 -3
- package/out/webresource-handler/actions/beginUpload.js +0 -105
- package/out/webresource-handler/actions/beginUpload.js.map +0 -1
- package/out/webresource-handler/actions/canceUpload.d.ts +0 -3
- package/out/webresource-handler/actions/canceUpload.js +0 -58
- package/out/webresource-handler/actions/canceUpload.js.map +0 -1
- package/out/webresource-handler/actions/commitUpload.d.ts +0 -3
- package/out/webresource-handler/actions/commitUpload.js +0 -85
- package/out/webresource-handler/actions/commitUpload.js.map +0 -1
- package/out/webresource-handler/multipartUpload.d.ts +0 -4
- package/out/webresource-handler/multipartUpload.js +0 -14
- package/out/webresource-handler/multipartUpload.js.map +0 -1
- package/out/webresource-handler/webresource.d.ts +0 -42
- package/out/webresource-handler/webresource.js +0 -2
- package/out/webresource-handler/webresource.js.map +0 -1
- package/out/webresource-handler/webresource.sbvr +0 -60
- package/src/webresource-handler/actions/beginUpload.ts +0 -163
- package/src/webresource-handler/actions/canceUpload.ts +0 -92
- package/src/webresource-handler/actions/commitUpload.ts +0 -117
- package/src/webresource-handler/multipartUpload.ts +0 -23
- package/src/webresource-handler/webresource.sbvr +0 -60
- package/src/webresource-handler/webresource.ts +0 -48
@@ -11,19 +11,11 @@ import {
|
|
11
11
|
odataNameToSqlName,
|
12
12
|
sqlNameToODataName,
|
13
13
|
} from '@balena/odata-to-abstract-sql';
|
14
|
-
import type { ConfigLoader } from '../server-glue/module.js';
|
15
14
|
import { errors, permissions } from '../server-glue/module.js';
|
16
15
|
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
|
17
16
|
import { TypedError } from 'typed-error';
|
18
17
|
import type { Resolvable } from '../sbvr-api/common-types.js';
|
19
|
-
import {
|
20
|
-
import type WebresourceModel from './webresource.js';
|
21
|
-
import { importSBVR } from '../server-glue/sbvr-loader.js';
|
22
|
-
import { beginUploadAction } from './actions/beginUpload.js';
|
23
|
-
import { isMultipartUploadAvailable } from './multipartUpload.js';
|
24
|
-
import { commitUploadAction } from './actions/commitUpload.js';
|
25
|
-
import { cancelUploadAction } from './actions/canceUpload.js';
|
26
|
-
import { addAction } from '../sbvr-api/actions.js';
|
18
|
+
import { canExecuteTasks } from '../tasks/index.js';
|
27
19
|
|
28
20
|
export * from './handlers/index.js';
|
29
21
|
|
@@ -325,28 +317,33 @@ export const getWebResourceFields = (
|
|
325
317
|
.map((f) => sqlNameToODataName(f.fieldName));
|
326
318
|
};
|
327
319
|
|
320
|
+
const scheduleToDelete = (fileKey: string) => {
|
321
|
+
return sbvrUtils.api.tasks.post({
|
322
|
+
resource: 'task',
|
323
|
+
passthrough: {
|
324
|
+
req: permissions.root,
|
325
|
+
},
|
326
|
+
body: {
|
327
|
+
key: crypto.randomUUID(),
|
328
|
+
is_executed_by__handler: 'delete_webresource_file',
|
329
|
+
is_executed_with__parameter_set: {
|
330
|
+
fileKey: fileKey,
|
331
|
+
},
|
332
|
+
// limit pg integer - repeat as many time as possible
|
333
|
+
attempt_limit: 2 ** 31 - 1,
|
334
|
+
},
|
335
|
+
});
|
336
|
+
};
|
337
|
+
|
328
338
|
const deleteFiles = async (
|
329
339
|
keysToDelete: string[],
|
330
340
|
webResourceHandler: WebResourceHandler,
|
331
341
|
) => {
|
332
|
-
const promises =
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
req: permissions.root,
|
338
|
-
},
|
339
|
-
body: {
|
340
|
-
key: crypto.randomUUID(),
|
341
|
-
is_executed_by__handler: 'delete_webresource_file',
|
342
|
-
is_executed_with__parameter_set: {
|
343
|
-
fileKey: fileKey,
|
344
|
-
},
|
345
|
-
attempt_limit: 2 ** 31 - 1,
|
346
|
-
},
|
347
|
-
}),
|
348
|
-
)
|
349
|
-
: keysToDelete.map((r) => webResourceHandler.removeFile(r));
|
342
|
+
const promises = keysToDelete.map((fileKey) => {
|
343
|
+
return canExecuteTasks()
|
344
|
+
? scheduleToDelete(fileKey)
|
345
|
+
: webResourceHandler.removeFile(fileKey);
|
346
|
+
});
|
350
347
|
await Promise.all(promises);
|
351
348
|
};
|
352
349
|
|
@@ -355,13 +352,6 @@ const throwIfWebresourceNotInMultipart = (
|
|
355
352
|
{ req, request }: HookArgs,
|
356
353
|
) => {
|
357
354
|
if (
|
358
|
-
// root needs to be able to bypass the multipart check as
|
359
|
-
// it needs to pass the direct payload on multipart uploads (on storage provider)
|
360
|
-
req.user !== permissions.root.user &&
|
361
|
-
// This is checking for HTTP multipart form submission/request (e.g. send the actual file via the API)
|
362
|
-
// Not to confuse with multipart uploads
|
363
|
-
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/POST#multipart_form_submission
|
364
|
-
// See: https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpuoverview.html
|
365
355
|
!req.is?.('multipart') &&
|
366
356
|
webResourceFields.some((field) => request.values[field] != null)
|
367
357
|
) {
|
@@ -559,38 +549,3 @@ export const setupUploadHooks = (
|
|
559
549
|
getCreateWebResourceHooks(handler),
|
560
550
|
);
|
561
551
|
};
|
562
|
-
|
563
|
-
export const setupUploadActions = (apiRoot: string, resourceName: string) => {
|
564
|
-
const resource = sqlNameToODataName(resourceName);
|
565
|
-
if (isMultipartUploadAvailable(configuredWebResourceHandler)) {
|
566
|
-
addAction(apiRoot, resource, 'beginUpload', beginUploadAction);
|
567
|
-
|
568
|
-
addAction(apiRoot, resource, 'commitUpload', commitUploadAction);
|
569
|
-
|
570
|
-
addAction(apiRoot, resource, 'cancelUpload', cancelUploadAction);
|
571
|
-
}
|
572
|
-
};
|
573
|
-
|
574
|
-
const initSql = `
|
575
|
-
CREATE INDEX IF NOT EXISTS idx_multipart_upload_uuid ON "multipart upload" (uuid);
|
576
|
-
CREATE INDEX IF NOT EXISTS idx_multipart_upload_status ON "multipart upload" (status);
|
577
|
-
`;
|
578
|
-
|
579
|
-
const modelText = await importSBVR('./webresource.sbvr', import.meta);
|
580
|
-
|
581
|
-
declare module '../sbvr-api/sbvr-utils.js' {
|
582
|
-
export interface API {
|
583
|
-
webresource: PinejsClient<WebresourceModel>;
|
584
|
-
}
|
585
|
-
}
|
586
|
-
|
587
|
-
export const config: ConfigLoader.Config = {
|
588
|
-
models: [
|
589
|
-
{
|
590
|
-
modelName: 'webresource',
|
591
|
-
apiRoot: 'webresource',
|
592
|
-
modelText,
|
593
|
-
initSql,
|
594
|
-
},
|
595
|
-
],
|
596
|
-
};
|
@@ -1,105 +0,0 @@
|
|
1
|
-
import { api } from '../../sbvr-api/sbvr-utils.js';
|
2
|
-
import { getMultipartUploadHandler } from '../multipartUpload.js';
|
3
|
-
import { getWebResourceFields } from '../index.js';
|
4
|
-
import { BadRequestError, NotImplementedError } from '../../sbvr-api/errors.js';
|
5
|
-
import { permissions, sbvrUtils } from '../../server-glue/module.js';
|
6
|
-
import { randomUUID } from 'crypto';
|
7
|
-
import { ajv } from '../../tasks/common.js';
|
8
|
-
const beginUploadPayloadSchema = {
|
9
|
-
type: 'object',
|
10
|
-
minProperties: 1,
|
11
|
-
maxProperties: 1,
|
12
|
-
additionalProperties: {
|
13
|
-
type: 'object',
|
14
|
-
properties: {
|
15
|
-
filename: { type: 'string' },
|
16
|
-
content_type: { type: 'string' },
|
17
|
-
size: { type: 'number' },
|
18
|
-
chunk_size: { type: 'number' },
|
19
|
-
},
|
20
|
-
required: ['filename', 'content_type', 'size'],
|
21
|
-
additionalProperties: false,
|
22
|
-
},
|
23
|
-
};
|
24
|
-
const validateBeginUpload = ajv.compile(beginUploadPayloadSchema);
|
25
|
-
export const beginUploadAction = async ({ request, tx, id, req, }) => {
|
26
|
-
if (typeof id !== 'number') {
|
27
|
-
throw new NotImplementedError('multipart upload do not yet support non-numeric ids');
|
28
|
-
}
|
29
|
-
const handler = getMultipartUploadHandler();
|
30
|
-
const { fieldName, beginUploadPayload } = parseBeginUpload(request, handler);
|
31
|
-
await runFakeDbPatch(request, {
|
32
|
-
[fieldName]: { ...beginUploadPayload, href: 'fake_patch' },
|
33
|
-
});
|
34
|
-
const { fileKey, uploadId, uploadParts } = await handler.multipartUpload.begin(fieldName, beginUploadPayload);
|
35
|
-
const uuid = randomUUID();
|
36
|
-
await api.webresource.post({
|
37
|
-
resource: 'multipart_upload',
|
38
|
-
body: {
|
39
|
-
uuid,
|
40
|
-
resource_name: request.resourceName,
|
41
|
-
field_name: fieldName,
|
42
|
-
resource_id: id,
|
43
|
-
upload_id: uploadId,
|
44
|
-
file_key: fileKey,
|
45
|
-
status: 'pending',
|
46
|
-
...beginUploadPayload,
|
47
|
-
expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
48
|
-
is_created_by__actor: req.user?.actor,
|
49
|
-
},
|
50
|
-
passthrough: {
|
51
|
-
req: permissions.root,
|
52
|
-
tx,
|
53
|
-
},
|
54
|
-
});
|
55
|
-
return {
|
56
|
-
body: { [fieldName]: { uuid, uploadParts } },
|
57
|
-
statusCode: 200,
|
58
|
-
};
|
59
|
-
};
|
60
|
-
const parseBeginUpload = (request, webResourceHandler) => {
|
61
|
-
const { values } = request;
|
62
|
-
const isValid = validateBeginUpload(values);
|
63
|
-
if (!isValid) {
|
64
|
-
throw new BadRequestError('Invalid begin upload payload');
|
65
|
-
}
|
66
|
-
const fieldName = Object.keys(values)[0];
|
67
|
-
const webResourceFields = getWebResourceFields(request, false);
|
68
|
-
if (!webResourceFields.includes(fieldName)) {
|
69
|
-
throw new BadRequestError(`The provided field '${fieldName}' is not a valid webresource`);
|
70
|
-
}
|
71
|
-
const beginUploadPayload = {
|
72
|
-
...values[fieldName],
|
73
|
-
chunk_size: values[fieldName].chunk_size ??
|
74
|
-
webResourceHandler.multipartUpload.getMinimumPartSize(),
|
75
|
-
};
|
76
|
-
if (beginUploadPayload.chunk_size <
|
77
|
-
webResourceHandler.multipartUpload.getMinimumPartSize()) {
|
78
|
-
throw new BadRequestError('Chunk size is too small');
|
79
|
-
}
|
80
|
-
return {
|
81
|
-
beginUploadPayload,
|
82
|
-
fieldName,
|
83
|
-
};
|
84
|
-
};
|
85
|
-
const runFakeDbPatch = async (request, fakeDbPatch) => {
|
86
|
-
const fakeTx = await sbvrUtils.db.transaction();
|
87
|
-
try {
|
88
|
-
const newUrl = request.url
|
89
|
-
.slice(1)
|
90
|
-
.split('?', 1)[0]
|
91
|
-
.replace(/\/beginUpload$/, '');
|
92
|
-
await api[request.vocabulary].request({
|
93
|
-
method: 'PATCH',
|
94
|
-
url: newUrl,
|
95
|
-
body: fakeDbPatch,
|
96
|
-
passthrough: { tx: fakeTx, req: permissions.root },
|
97
|
-
});
|
98
|
-
}
|
99
|
-
finally {
|
100
|
-
if (!fakeTx.isClosed()) {
|
101
|
-
await fakeTx.rollback();
|
102
|
-
}
|
103
|
-
}
|
104
|
-
};
|
105
|
-
//# sourceMappingURL=beginUpload.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"beginUpload.js","sourceRoot":"","sources":["../../../src/webresource-handler/actions/beginUp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rC,MAAM,EAAE,OAAO;YACf,GAAG,EAAE,MAAM;YACX,IAAI,EAAE,WAAW;YAGjB,WAAW,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,CAAC,IAAI,EAAE;SAClD,CAAC,CAAC;IACJ,CAAC;YAAS,CAAC;QACV,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;YACxB,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAC;QACzB,CAAC;IACF,CAAC;AACF,CAAC,CAAC"}
|
@@ -1,58 +0,0 @@
|
|
1
|
-
import { BadRequestError, NotImplementedError, UnauthorizedError, } from '../../sbvr-api/errors.js';
|
2
|
-
import { api } from '../../sbvr-api/sbvr-utils.js';
|
3
|
-
import { permissions } from '../../server-glue/module.js';
|
4
|
-
import { getMultipartUploadHandler } from '../multipartUpload.js';
|
5
|
-
export const cancelUploadAction = async ({ request, tx, id: resourceId, }) => {
|
6
|
-
if (typeof resourceId !== 'number') {
|
7
|
-
throw new NotImplementedError('multipart upload do not yet support non-numeric ids');
|
8
|
-
}
|
9
|
-
const { id, fileKey, uploadId } = await getOngoingUpload(request, resourceId, tx);
|
10
|
-
const handler = getMultipartUploadHandler();
|
11
|
-
await api.webresource.patch({
|
12
|
-
resource: 'multipart_upload',
|
13
|
-
body: {
|
14
|
-
status: 'cancelled',
|
15
|
-
},
|
16
|
-
id,
|
17
|
-
passthrough: {
|
18
|
-
tx: tx,
|
19
|
-
req: permissions.root,
|
20
|
-
},
|
21
|
-
});
|
22
|
-
await handler.multipartUpload.cancel({ fileKey, uploadId });
|
23
|
-
return {
|
24
|
-
statusCode: 204,
|
25
|
-
};
|
26
|
-
};
|
27
|
-
const getOngoingUpload = async (request, affectedId, tx) => {
|
28
|
-
const { uuid } = request.values;
|
29
|
-
if (uuid == null || typeof uuid !== 'string') {
|
30
|
-
throw new BadRequestError('Invalid uuid type');
|
31
|
-
}
|
32
|
-
const [multipartUpload] = await api.webresource.get({
|
33
|
-
resource: 'multipart_upload',
|
34
|
-
options: {
|
35
|
-
$select: ['id', 'file_key', 'upload_id'],
|
36
|
-
$filter: {
|
37
|
-
uuid,
|
38
|
-
status: 'pending',
|
39
|
-
expiry_date: { $gt: { $now: {} } },
|
40
|
-
resource_name: request.resourceName,
|
41
|
-
resource_id: affectedId,
|
42
|
-
},
|
43
|
-
},
|
44
|
-
passthrough: {
|
45
|
-
tx,
|
46
|
-
req: permissions.rootRead,
|
47
|
-
},
|
48
|
-
});
|
49
|
-
if (multipartUpload == null) {
|
50
|
-
throw new UnauthorizedError();
|
51
|
-
}
|
52
|
-
return {
|
53
|
-
id: multipartUpload.id,
|
54
|
-
fileKey: multipartUpload.file_key,
|
55
|
-
uploadId: multipartUpload.upload_id,
|
56
|
-
};
|
57
|
-
};
|
58
|
-
//# sourceMappingURL=canceUpload.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"canceUpload.js","sourceRoot":"","sources":["../../../src/webresource-handler/actions/canceUpload.ts"],"names":[],"mappings":"AAKA,OAAO,EACN,eAAe,EACf,mBAAmB,EACnB,iBAAiB,GACjB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,GAAG,EAAiB,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAElE,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,EACxC,OAAO,EACP,EAAE,EACF,EAAE,EAAE,UAAU,GACG,EAAqB,EAAE;IACxC,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACpC,MAAM,IAAI,mBAAmB,CAC5B,qDAAqD,CACrD,CAAC;IACH,CAAC;IACD,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,MAAM,gBAAgB,CACvD,OAAO,EACP,UAAU,EACV,EAAE,CACF,CAAC;IACF,MAAM,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IAAI,iBAAiB,EAAE,CAAC;IAC/B,CAAC;IAED,OAAO;QACN,EAAE,EAAE,eAAe,CAAC,EAAE;QACtB,OAAO,EAAE,eAAe,CAAC,QAAQ;QACjC,QAAQ,EAAE,eAAe,CAAC,SAAS;KACnC,CAAC;AACH,CAAC,CAAC"}
|
@@ -1,85 +0,0 @@
|
|
1
|
-
import { BadRequestError, NotImplementedError, UnauthorizedError, } from '../../sbvr-api/errors.js';
|
2
|
-
import { api } from '../../sbvr-api/sbvr-utils.js';
|
3
|
-
import { permissions } from '../../server-glue/module.js';
|
4
|
-
import { getMultipartUploadHandler } from '../multipartUpload.js';
|
5
|
-
export const commitUploadAction = async ({ request, tx, id, api: applicationApi, }) => {
|
6
|
-
if (typeof id !== 'number') {
|
7
|
-
throw new NotImplementedError('multipart upload do not yet support non-numeric ids');
|
8
|
-
}
|
9
|
-
const multipartUpload = await getOngoingUpload(request, id, tx);
|
10
|
-
const handler = getMultipartUploadHandler();
|
11
|
-
const webresource = await handler.multipartUpload.commit({
|
12
|
-
fileKey: multipartUpload.fileKey,
|
13
|
-
uploadId: multipartUpload.uploadId,
|
14
|
-
filename: multipartUpload.filename,
|
15
|
-
providerCommitData: multipartUpload.providerCommitData,
|
16
|
-
});
|
17
|
-
await Promise.all([
|
18
|
-
api.webresource.patch({
|
19
|
-
resource: 'multipart_upload',
|
20
|
-
body: {
|
21
|
-
status: 'completed',
|
22
|
-
},
|
23
|
-
options: {
|
24
|
-
$filter: {
|
25
|
-
uuid: multipartUpload.uuid,
|
26
|
-
},
|
27
|
-
},
|
28
|
-
passthrough: {
|
29
|
-
tx: tx,
|
30
|
-
req: permissions.root,
|
31
|
-
},
|
32
|
-
}),
|
33
|
-
applicationApi.patch({
|
34
|
-
resource: request.resourceName,
|
35
|
-
id,
|
36
|
-
body: {
|
37
|
-
[multipartUpload.fieldName]: webresource,
|
38
|
-
},
|
39
|
-
passthrough: {
|
40
|
-
tx: tx,
|
41
|
-
req: permissions.root,
|
42
|
-
},
|
43
|
-
}),
|
44
|
-
]);
|
45
|
-
const body = await handler.onPreRespond(webresource);
|
46
|
-
return {
|
47
|
-
body,
|
48
|
-
statusCode: 200,
|
49
|
-
};
|
50
|
-
};
|
51
|
-
const getOngoingUpload = async (request, affectedId, tx) => {
|
52
|
-
const { uuid, providerCommitData } = request.values;
|
53
|
-
if (uuid == null || typeof uuid !== 'string') {
|
54
|
-
throw new BadRequestError('Invalid uuid type');
|
55
|
-
}
|
56
|
-
const [multipartUpload] = await api.webresource.get({
|
57
|
-
resource: 'multipart_upload',
|
58
|
-
options: {
|
59
|
-
$select: ['id', 'file_key', 'upload_id', 'field_name', 'filename'],
|
60
|
-
$filter: {
|
61
|
-
uuid,
|
62
|
-
status: 'pending',
|
63
|
-
expiry_date: { $gt: { $now: {} } },
|
64
|
-
resource_name: request.resourceName,
|
65
|
-
resource_id: affectedId,
|
66
|
-
},
|
67
|
-
},
|
68
|
-
passthrough: {
|
69
|
-
tx,
|
70
|
-
req: permissions.rootRead,
|
71
|
-
},
|
72
|
-
});
|
73
|
-
if (multipartUpload == null) {
|
74
|
-
throw new UnauthorizedError();
|
75
|
-
}
|
76
|
-
return {
|
77
|
-
uuid,
|
78
|
-
providerCommitData,
|
79
|
-
fileKey: multipartUpload.file_key,
|
80
|
-
uploadId: multipartUpload.upload_id,
|
81
|
-
filename: multipartUpload.filename,
|
82
|
-
fieldName: multipartUpload.field_name,
|
83
|
-
};
|
84
|
-
};
|
85
|
-
//# sourceMappingURL=commitUpload.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"commitUpload.js","sourceRoot":"","sources":["../../../src/webresource-handler/actions/commitUpload.ts"],"names":[],"mappings":"AAKA,OAAO,EACN,eAAe,EACf,mBAAmB,EACnB,iBAAiB,GACjB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,GAAG,EAAE,MAAM,8BAA8B,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,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AC,UAAU;KACrC,CAAC;AACH,CAAC,CAAC"}
|
@@ -1,4 +0,0 @@
|
|
1
|
-
import type { WebResourceHandler } from './index.js';
|
2
|
-
export type MultipartUploadHandler = WebResourceHandler & Required<Pick<WebResourceHandler, 'multipartUpload'>>;
|
3
|
-
export declare const isMultipartUploadAvailable: (handler: WebResourceHandler | undefined) => handler is MultipartUploadHandler;
|
4
|
-
export declare const getMultipartUploadHandler: () => MultipartUploadHandler;
|
@@ -1,14 +0,0 @@
|
|
1
|
-
import { webResource as webResourceEnv } from '../config-loader/env.js';
|
2
|
-
import { NotImplementedError } from '../sbvr-api/errors.js';
|
3
|
-
import { getWebresourceHandler } from './index.js';
|
4
|
-
export const isMultipartUploadAvailable = (handler) => {
|
5
|
-
return (webResourceEnv.multipartUploadEnabled && handler?.multipartUpload != null);
|
6
|
-
};
|
7
|
-
export const getMultipartUploadHandler = () => {
|
8
|
-
const handler = getWebresourceHandler();
|
9
|
-
if (!isMultipartUploadAvailable(handler)) {
|
10
|
-
throw new NotImplementedError('Multipart uploads not available');
|
11
|
-
}
|
12
|
-
return handler;
|
13
|
-
};
|
14
|
-
//# sourceMappingURL=multipartUpload.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"multipartUpload.js","sourceRoot":"","sources":["../../src/webresource-handler/multipartUpload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAKnD,MAAM,CAAC,MAAM,0BAA0B,GAAG,CACzC,OAAuC,EACH,EAAE;IACtC,OAAO,CACN,cAAc,CAAC,sBAAsB,IAAI,OAAO,EAAE,eAAe,IAAI,IAAI,CACzE,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAG,EAAE;IAC7C,MAAM,OAAO,GAAG,qBAAqB,EAAE,CAAC;IACxC,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,mBAAmB,CAAC,iCAAiC,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC,CAAC"}
|
@@ -1,42 +0,0 @@
|
|
1
|
-
import type { Types } from '@balena/abstract-sql-to-typescript';
|
2
|
-
export interface MultipartUpload {
|
3
|
-
Read: {
|
4
|
-
created_at: Types['Date Time']['Read'];
|
5
|
-
modified_at: Types['Date Time']['Read'];
|
6
|
-
id: Types['Serial']['Read'];
|
7
|
-
uuid: Types['Short Text']['Read'];
|
8
|
-
resource_name: Types['Short Text']['Read'];
|
9
|
-
field_name: Types['Short Text']['Read'];
|
10
|
-
resource_id: Types['Integer']['Read'];
|
11
|
-
upload_id: Types['Short Text']['Read'];
|
12
|
-
file_key: Types['Short Text']['Read'];
|
13
|
-
status: 'pending' | 'completed' | 'cancelled';
|
14
|
-
filename: Types['Short Text']['Read'];
|
15
|
-
content_type: Types['Short Text']['Read'];
|
16
|
-
size: Types['Big Integer']['Read'];
|
17
|
-
chunk_size: Types['Integer']['Read'];
|
18
|
-
expiry_date: Types['Date Time']['Read'];
|
19
|
-
is_created_by__actor: Types['Integer']['Read'] | null;
|
20
|
-
};
|
21
|
-
Write: {
|
22
|
-
created_at: Types['Date Time']['Write'];
|
23
|
-
modified_at: Types['Date Time']['Write'];
|
24
|
-
id: Types['Serial']['Write'];
|
25
|
-
uuid: Types['Short Text']['Write'];
|
26
|
-
resource_name: Types['Short Text']['Write'];
|
27
|
-
field_name: Types['Short Text']['Write'];
|
28
|
-
resource_id: Types['Integer']['Write'];
|
29
|
-
upload_id: Types['Short Text']['Write'];
|
30
|
-
file_key: Types['Short Text']['Write'];
|
31
|
-
status: 'pending' | 'completed' | 'cancelled';
|
32
|
-
filename: Types['Short Text']['Write'];
|
33
|
-
content_type: Types['Short Text']['Write'];
|
34
|
-
size: Types['Big Integer']['Write'];
|
35
|
-
chunk_size: Types['Integer']['Write'];
|
36
|
-
expiry_date: Types['Date Time']['Write'];
|
37
|
-
is_created_by__actor: Types['Integer']['Write'] | null;
|
38
|
-
};
|
39
|
-
}
|
40
|
-
export default interface $Model {
|
41
|
-
multipart_upload: MultipartUpload;
|
42
|
-
}
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"webresource.js","sourceRoot":"","sources":["../../src/webresource-handler/webresource.ts"],"names":[],"mappings":""}
|
@@ -1,60 +0,0 @@
|
|
1
|
-
Vocabulary: webresource
|
2
|
-
|
3
|
-
Term: actor
|
4
|
-
Concept Type: Integer (Type)
|
5
|
-
Term: expiry date
|
6
|
-
Concept Type: Date Time (Type)
|
7
|
-
Term: uuid
|
8
|
-
Concept Type: Short Text (Type)
|
9
|
-
Term: resource name
|
10
|
-
Concept Type: Short Text (Type)
|
11
|
-
Term: field name
|
12
|
-
Concept Type: Short Text (Type)
|
13
|
-
Term: resource id
|
14
|
-
Concept Type: Integer (Type)
|
15
|
-
Term: upload id
|
16
|
-
Concept Type: Short Text (Type)
|
17
|
-
Term: file key
|
18
|
-
Concept Type: Short Text (Type)
|
19
|
-
Term: status
|
20
|
-
Concept Type: Short Text (Type)
|
21
|
-
Term: filename
|
22
|
-
Concept Type: Short Text (Type)
|
23
|
-
Term: content type
|
24
|
-
Concept Type: Short Text (Type)
|
25
|
-
Term: size
|
26
|
-
Concept Type: Big Integer (Type)
|
27
|
-
Term: chunk size
|
28
|
-
Concept Type: Integer (Type)
|
29
|
-
Term: valid until date
|
30
|
-
Concept Type: Date Time (Type)
|
31
|
-
|
32
|
-
Term: multipart upload
|
33
|
-
Fact type: multipart upload has uuid
|
34
|
-
Necessity: each multipart upload has exactly one uuid
|
35
|
-
Necessity: each uuid is of exactly one multipart upload
|
36
|
-
Fact type: multipart upload has resource name
|
37
|
-
Necessity: each multipart upload has exactly one resource name
|
38
|
-
Fact type: multipart upload has field name
|
39
|
-
Necessity: each multipart upload has exactly one field name
|
40
|
-
Fact type: multipart upload has resource id
|
41
|
-
Necessity: each multipart upload has exactly one resource id
|
42
|
-
Fact type: multipart upload has upload id
|
43
|
-
Necessity: each multipart upload has exactly one upload id
|
44
|
-
Fact type: multipart upload has file key
|
45
|
-
Necessity: each multipart upload has exactly one file key
|
46
|
-
Fact type: multipart upload has status
|
47
|
-
Necessity: each multipart upload has exactly one status
|
48
|
-
Definition: "pending" or "completed" or "cancelled"
|
49
|
-
Fact type: multipart upload has filename
|
50
|
-
Necessity: each multipart upload has exactly one filename
|
51
|
-
Fact type: multipart upload has content type
|
52
|
-
Necessity: each multipart upload has exactly one content type
|
53
|
-
Fact type: multipart upload has size
|
54
|
-
Necessity: each multipart upload has exactly one size
|
55
|
-
Fact type: multipart upload has chunk size
|
56
|
-
Necessity: each multipart upload has exactly one chunk size
|
57
|
-
Fact type: multipart upload has expiry date
|
58
|
-
Necessity: each multipart upload has exactly one expiry date
|
59
|
-
Fact type: multipart upload is created by actor
|
60
|
-
Necessity: each multipart upload is created by at most one actor
|