arocapi 5.0.0-beta.1 → 5.0.0-beta.10
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/LICENSE.md +1 -1
- package/README.md +37 -802
- package/dist/app.d.ts +38 -20
- package/dist/app.js +62 -40
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +37 -57
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +13 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/file.d.ts +8 -2
- package/dist/routes/file.js +37 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/metadata.d.ts +11 -5
- package/dist/routes/metadata.js +24 -68
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +8 -7
- package/dist/routes/search.js +90 -119
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +19 -0
- package/dist/storage/location.js +36 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +6 -0
- package/dist/storage/ocfl/ocfl.js +45 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/transformers/default.d.ts +11 -22
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +6 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +19 -10
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +82 -40
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +168 -95
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/entities.ts +48 -67
- package/src/routes/entity.ts +21 -35
- package/src/routes/file.ts +53 -103
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +42 -98
- package/src/routes/search.ts +131 -151
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +78 -0
- package/src/storage/ocfl/ocfl.ts +71 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/transformers/default.ts +115 -85
- package/src/types/access.ts +25 -0
- package/src/types/context.ts +15 -0
- package/src/types/transformers.ts +8 -34
- package/src/types/visibility.ts +21 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.ts +68 -11
- package/src/utils/headers.ts +22 -2
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -82
- package/dist/app.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -156
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -342
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -273
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -130
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -315
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -222
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -780
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -18
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -29
- package/dist/test/integration.setup.js +0 -187
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -396
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -163
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -187
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -41
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -66
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -35
- package/dist/utils/queryBuilder.js +0 -98
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -112
- package/src/index.dev.ts +0 -200
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/crate.test.ts +0 -423
- package/src/routes/entities.test.ts +0 -320
- package/src/routes/entity.test.ts +0 -160
- package/src/routes/file.test.ts +0 -395
- package/src/routes/files.test.ts +0 -278
- package/src/routes/search.test.ts +0 -903
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -27
- package/src/test/integration.setup.ts +0 -209
- package/src/test/integration.test.ts +0 -482
- package/src/transformers/default.test.ts +0 -184
- package/src/transformers/transformer.test.ts +0 -252
- package/src/types/fileHandlers.ts +0 -65
- package/src/utils/errors.test.ts +0 -75
- package/src/utils/queryBuilder.ts +0 -132
package/src/routes/entities.ts
CHANGED
|
@@ -2,21 +2,23 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
8
9
|
|
|
9
10
|
const querySchema = z.object({
|
|
10
11
|
memberOf: z.url().optional(),
|
|
11
12
|
entityType: z
|
|
12
|
-
.string()
|
|
13
|
-
.
|
|
14
|
-
.transform((val) =>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
.union([z.string(), z.array(z.string())])
|
|
14
|
+
.default([])
|
|
15
|
+
.transform((val) =>
|
|
16
|
+
[val]
|
|
17
|
+
.flat()
|
|
18
|
+
.map((value) => value.trim())
|
|
19
|
+
.filter(Boolean),
|
|
20
|
+
)
|
|
21
|
+
.pipe(z.array(z.url())),
|
|
20
22
|
limit: z.coerce.number().int().min(1).max(1000).default(100),
|
|
21
23
|
offset: z.coerce.number().int().min(0).default(0),
|
|
22
24
|
sort: z.enum(['id', 'name', 'createdAt', 'updatedAt']).default('id'),
|
|
@@ -25,12 +27,13 @@ const querySchema = z.object({
|
|
|
25
27
|
|
|
26
28
|
type EntitiesRouteOptions = {
|
|
27
29
|
prisma: PrismaClient;
|
|
28
|
-
|
|
30
|
+
access: AccessFunction;
|
|
29
31
|
entityTransformers?: EntityTransformer[];
|
|
32
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
30
33
|
};
|
|
31
34
|
|
|
32
35
|
const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts) => {
|
|
33
|
-
const { prisma,
|
|
36
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter } = opts;
|
|
34
37
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
35
38
|
'/entities',
|
|
36
39
|
{
|
|
@@ -38,68 +41,46 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
38
41
|
querystring: querySchema,
|
|
39
42
|
},
|
|
40
43
|
},
|
|
41
|
-
async (request
|
|
44
|
+
async (request) => {
|
|
42
45
|
const { memberOf, entityType, limit, offset, sort, order } = request.query;
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
46
|
-
|
|
47
|
-
if (memberOf) {
|
|
48
|
-
where.memberOf = memberOf;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (entityType && entityType.length > 0) {
|
|
52
|
-
where.entityType = {
|
|
53
|
-
in: entityType,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
47
|
+
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
include: { file: { select: { id: true } } },
|
|
61
|
-
orderBy: {
|
|
62
|
-
[sort]: order,
|
|
63
|
-
},
|
|
64
|
-
skip: offset,
|
|
65
|
-
take: limit,
|
|
66
|
-
}),
|
|
67
|
-
prisma.entity.count({ where }),
|
|
68
|
-
]);
|
|
69
|
-
|
|
70
|
-
// Resolve memberOf and rootCollection references
|
|
71
|
-
const refMap = await resolveEntityReferences(dbEntities, prisma);
|
|
49
|
+
if (memberOf) {
|
|
50
|
+
where.memberOf = memberOf;
|
|
51
|
+
}
|
|
72
52
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
...base,
|
|
79
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
80
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
81
|
-
};
|
|
82
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
53
|
+
if (entityType.length > 0) {
|
|
54
|
+
where.entityType = {
|
|
55
|
+
in: entityType,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
83
58
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
59
|
+
const context = { request, fastify };
|
|
60
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
61
|
+
const visibleWhere = { AND: [where, visibilityWhere] };
|
|
88
62
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
63
|
+
const [dbEntities, total] = await Promise.all([
|
|
64
|
+
prisma.entity.findMany({
|
|
65
|
+
where: visibleWhere,
|
|
66
|
+
orderBy: {
|
|
67
|
+
[sort]: order,
|
|
68
|
+
},
|
|
69
|
+
skip: offset,
|
|
70
|
+
take: limit,
|
|
71
|
+
}),
|
|
72
|
+
prisma.entity.count({ where: visibleWhere }),
|
|
73
|
+
]);
|
|
92
74
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
} catch (error) {
|
|
98
|
-
const err = error as Error;
|
|
99
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
75
|
+
const authorisedEntities = await authoriseEntities(dbEntities, prisma, access, context);
|
|
76
|
+
const entities = await Promise.all(
|
|
77
|
+
authorisedEntities.map((entity, i) => applyTransformers(entity, entityTransformers, context, dbEntities[i])),
|
|
78
|
+
);
|
|
100
79
|
|
|
101
|
-
|
|
102
|
-
|
|
80
|
+
return {
|
|
81
|
+
total,
|
|
82
|
+
entities,
|
|
83
|
+
};
|
|
103
84
|
},
|
|
104
85
|
);
|
|
105
86
|
};
|
package/src/routes/entity.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
9
|
+
import { createEntityNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
8
10
|
|
|
9
11
|
const paramsSchema = z.object({
|
|
10
12
|
id: z.url(),
|
|
@@ -12,12 +14,16 @@ const paramsSchema = z.object({
|
|
|
12
14
|
|
|
13
15
|
type EntityRouteOptions = {
|
|
14
16
|
prisma: PrismaClient;
|
|
15
|
-
|
|
17
|
+
access: AccessFunction;
|
|
16
18
|
entityTransformers?: EntityTransformer[];
|
|
19
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
20
|
+
refusal: RefusalOptions;
|
|
17
21
|
};
|
|
18
22
|
|
|
19
23
|
const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) => {
|
|
20
|
-
const { prisma,
|
|
24
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter, refusal } = opts;
|
|
25
|
+
const sendEntityNotVisible = createEntityNotVisibleResponder(prisma, refusal);
|
|
26
|
+
|
|
21
27
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
22
28
|
'/entity/:id',
|
|
23
29
|
{
|
|
@@ -28,39 +34,19 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
28
34
|
async (request, reply) => {
|
|
29
35
|
const { id } = request.params;
|
|
30
36
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!entity) {
|
|
38
|
-
return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Resolve memberOf and rootCollection references
|
|
42
|
-
const refMap = await resolveEntityReferences([entity], prisma);
|
|
37
|
+
const context = { request, fastify };
|
|
38
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
39
|
+
const entity = await prisma.entity.findFirst({
|
|
40
|
+
where: { AND: [{ id }, visibilityWhere] },
|
|
41
|
+
});
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
48
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
49
|
-
};
|
|
50
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
51
|
-
|
|
52
|
-
let result = authorisedEntity;
|
|
53
|
-
for (const transformer of entityTransformers) {
|
|
54
|
-
result = await transformer(result, { request, fastify });
|
|
55
|
-
}
|
|
43
|
+
if (!entity) {
|
|
44
|
+
return sendEntityNotVisible(reply, id, context);
|
|
45
|
+
}
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
} catch (error) {
|
|
59
|
-
const err = error as Error;
|
|
60
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
47
|
+
const [authorisedEntity] = await authoriseEntities([entity], prisma, access, context);
|
|
61
48
|
|
|
62
|
-
|
|
63
|
-
}
|
|
49
|
+
return applyTransformers(authorisedEntity, entityTransformers, context, entity);
|
|
64
50
|
},
|
|
65
51
|
);
|
|
66
52
|
};
|
package/src/routes/file.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { create as contentDisposition } from 'content-disposition';
|
|
2
2
|
import type { FastifyPluginAsync } from 'fastify';
|
|
3
3
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import type { ContentSource } from '../storage/backend.js';
|
|
7
|
+
import { sendDelivery } from '../storage/deliver.js';
|
|
8
|
+
import type { AccessFunction } from '../types/access.js';
|
|
9
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
10
|
+
import { createAccessGuard } from '../utils/access.js';
|
|
11
|
+
import { createNotFoundError } from '../utils/errors.js';
|
|
12
|
+
import { createNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
9
13
|
|
|
10
14
|
const paramsSchema = z.object({
|
|
11
15
|
id: z.url(),
|
|
@@ -14,122 +18,68 @@ const paramsSchema = z.object({
|
|
|
14
18
|
const querySchema = z.object({
|
|
15
19
|
disposition: z.enum(['inline', 'attachment']).optional().default('inline'),
|
|
16
20
|
filename: z.string().optional(),
|
|
17
|
-
noRedirect: z
|
|
21
|
+
noRedirect: z
|
|
22
|
+
.enum(['true', 'false'])
|
|
23
|
+
.optional()
|
|
24
|
+
.default('false')
|
|
25
|
+
.transform((value) => value === 'true'),
|
|
18
26
|
});
|
|
19
27
|
|
|
20
28
|
type FileRouteOptions = {
|
|
21
29
|
prisma: PrismaClient;
|
|
22
|
-
|
|
30
|
+
content: ContentSource;
|
|
31
|
+
access: AccessFunction;
|
|
32
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
33
|
+
refusal: RefusalOptions;
|
|
23
34
|
};
|
|
24
35
|
|
|
25
36
|
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
26
|
-
const { prisma,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
const { prisma, content, access, entityVisibilityFilter, refusal } = opts;
|
|
38
|
+
|
|
39
|
+
const denyContent = createAccessGuard({ access, isAuthenticated: refusal.isAuthenticated, level: 'content' });
|
|
40
|
+
|
|
41
|
+
const sendFileNotVisible = createNotVisibleResponder({
|
|
42
|
+
...refusal,
|
|
43
|
+
notFoundMessage: 'The requested file was not found',
|
|
44
|
+
exists: async (id) => (await prisma.file.count({ where: { id } })) > 0,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
fastify.withTypeProvider<ZodTypeProvider>().route({
|
|
48
|
+
method: ['GET', 'HEAD'],
|
|
49
|
+
url: '/file/:id',
|
|
50
|
+
schema: {
|
|
51
|
+
params: paramsSchema,
|
|
52
|
+
querystring: querySchema,
|
|
34
53
|
},
|
|
35
|
-
async (request, reply) => {
|
|
54
|
+
handler: async (request, reply) => {
|
|
36
55
|
const { id } = request.params;
|
|
37
56
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (!file) {
|
|
44
|
-
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const metadata: FileMetadata | false = await fileHandler.head(file, { request, fastify });
|
|
48
|
-
|
|
49
|
-
if (!metadata) {
|
|
50
|
-
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
setFileHeaders(reply, metadata);
|
|
54
|
-
|
|
55
|
-
return reply.code(200).send();
|
|
56
|
-
} catch (error) {
|
|
57
|
-
const err = error as Error;
|
|
58
|
-
fastify.log.error(`File metadata retrieval error: ${err.message}`);
|
|
57
|
+
const context = { request, fastify };
|
|
58
|
+
const found = await prisma.file.findFirst({
|
|
59
|
+
where: { id, entity: await entityVisibilityFilter(context) },
|
|
60
|
+
include: { entity: true },
|
|
61
|
+
});
|
|
59
62
|
|
|
60
|
-
|
|
63
|
+
if (!found) {
|
|
64
|
+
return sendFileNotVisible(reply, id, context);
|
|
61
65
|
}
|
|
62
|
-
},
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
66
|
-
'/file/:id',
|
|
67
|
-
{
|
|
68
|
-
schema: {
|
|
69
|
-
params: paramsSchema,
|
|
70
|
-
querystring: querySchema,
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
async (request, reply) => {
|
|
74
|
-
const { id } = request.params;
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
const file = await prisma.file.findUnique({
|
|
78
|
-
where: { id },
|
|
79
|
-
});
|
|
80
66
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const result = await fileHandler.get(file, { request, fastify });
|
|
86
|
-
|
|
87
|
-
if (!result) {
|
|
88
|
-
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (result.type === 'redirect') {
|
|
92
|
-
if (request.query.noRedirect) {
|
|
93
|
-
return reply.code(200).send({ location: result.url });
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return reply.code(302).redirect(result.url);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const { disposition, filename: customFilename } = request.query;
|
|
100
|
-
const filename = customFilename || file.filename;
|
|
101
|
-
setFileHeaders(reply, result.metadata);
|
|
102
|
-
reply.header('Content-Disposition', `${disposition}; filename="${filename}"`);
|
|
103
|
-
|
|
104
|
-
// Handle stream response
|
|
105
|
-
if (result.type === 'stream') {
|
|
106
|
-
return reply.code(200).send(result.stream);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (result.type === 'file') {
|
|
110
|
-
if (result.accelPath) {
|
|
111
|
-
reply.header('X-Accel-Redirect', result.accelPath);
|
|
112
|
-
return reply.code(200).send();
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const stream = createReadStream(result.path);
|
|
116
|
-
return reply.code(200).send(stream);
|
|
117
|
-
}
|
|
67
|
+
const { entity, ...file } = found;
|
|
68
|
+
if (await denyContent(reply, entity, context)) {
|
|
69
|
+
return reply;
|
|
70
|
+
}
|
|
118
71
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
reply.removeHeader('Content-Type');
|
|
123
|
-
reply.removeHeader('Content-Length');
|
|
124
|
-
return reply.code(500).send(createInternalError());
|
|
125
|
-
} catch (error) {
|
|
126
|
-
const err = error as Error;
|
|
127
|
-
fastify.log.error(`File retrieval error: ${err}`);
|
|
72
|
+
const { disposition, filename, noRedirect } = request.query;
|
|
73
|
+
const disposed = contentDisposition(filename || file.filename, { type: disposition });
|
|
74
|
+
const delivery = await content(file, entity, { ...context, contentDisposition: disposed });
|
|
128
75
|
|
|
129
|
-
|
|
76
|
+
if (!delivery) {
|
|
77
|
+
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
|
130
78
|
}
|
|
79
|
+
|
|
80
|
+
return sendDelivery(request, reply, delivery, { contentDisposition: disposed, noRedirect });
|
|
131
81
|
},
|
|
132
|
-
);
|
|
82
|
+
});
|
|
133
83
|
};
|
|
134
84
|
|
|
135
85
|
export default file;
|
package/src/routes/files.ts
CHANGED
|
@@ -2,9 +2,10 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseFiles } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { FileTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
8
9
|
|
|
9
10
|
const querySchema = z.object({
|
|
10
11
|
memberOf: z.url().optional(),
|
|
@@ -16,12 +17,13 @@ const querySchema = z.object({
|
|
|
16
17
|
|
|
17
18
|
type FilesRouteOptions = {
|
|
18
19
|
prisma: PrismaClient;
|
|
19
|
-
|
|
20
|
+
access: AccessFunction;
|
|
20
21
|
fileTransformers?: FileTransformer[];
|
|
22
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
24
|
-
const { prisma,
|
|
26
|
+
const { prisma, access, fileTransformers = [], entityVisibilityFilter } = opts;
|
|
25
27
|
|
|
26
28
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
27
29
|
'/files',
|
|
@@ -30,53 +32,35 @@ const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
|
30
32
|
querystring: querySchema,
|
|
31
33
|
},
|
|
32
34
|
},
|
|
33
|
-
async (request
|
|
35
|
+
async (request) => {
|
|
34
36
|
const { memberOf, limit, offset, sort, order } = request.query;
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
const context = { request, fastify };
|
|
39
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
40
|
+
const where = { entity: { AND: [{ memberOf }, visibilityWhere] } };
|
|
38
41
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
const [dbFiles, total] = await Promise.all([
|
|
43
|
+
prisma.file.findMany({
|
|
44
|
+
where,
|
|
45
|
+
include: { entity: true },
|
|
46
|
+
orderBy: {
|
|
47
|
+
[sort]: order,
|
|
48
|
+
},
|
|
49
|
+
skip: offset,
|
|
50
|
+
take: limit,
|
|
51
|
+
}),
|
|
52
|
+
prisma.file.count({ where }),
|
|
53
|
+
]);
|
|
42
54
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
[sort]: order,
|
|
48
|
-
},
|
|
49
|
-
skip: offset,
|
|
50
|
-
take: limit,
|
|
51
|
-
}),
|
|
52
|
-
prisma.file.count({ where }),
|
|
53
|
-
]);
|
|
55
|
+
const authorisedFiles = await authoriseFiles(dbFiles, access, context);
|
|
56
|
+
const filesWithAccess = await Promise.all(
|
|
57
|
+
authorisedFiles.map((file, i) => applyTransformers(file, fileTransformers, context, dbFiles[i])),
|
|
58
|
+
);
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const authorisedFile = await fileAccessTransformer(standardFile, { request, fastify });
|
|
60
|
-
|
|
61
|
-
let result = authorisedFile;
|
|
62
|
-
for (const transformer of fileTransformers) {
|
|
63
|
-
result = await transformer(result, { request, fastify });
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return result;
|
|
67
|
-
}),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
total,
|
|
72
|
-
files: filesWithAccess,
|
|
73
|
-
};
|
|
74
|
-
} catch (error) {
|
|
75
|
-
const err = error as Error;
|
|
76
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
77
|
-
|
|
78
|
-
return reply.code(500).send(createInternalError());
|
|
79
|
-
}
|
|
60
|
+
return {
|
|
61
|
+
total,
|
|
62
|
+
files: filesWithAccess,
|
|
63
|
+
};
|
|
80
64
|
},
|
|
81
65
|
);
|
|
82
66
|
};
|