@crombolo/core 0.34.0 → 0.36.2
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/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/media-access.ts +146 -0
- package/src/uploads.ts +124 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.2",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Domain model, entity keys and shared contracts for crombolo. No I/O.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './form-submissions.ts'
|
|
|
5
5
|
export * from './forms.ts'
|
|
6
6
|
export * from './json-patch.ts'
|
|
7
7
|
export * from './keys.ts'
|
|
8
|
+
export * from './media-access.ts'
|
|
8
9
|
export * from './notifications.ts'
|
|
9
10
|
export * from './object-types.ts'
|
|
10
11
|
export * from './paging.ts'
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who may read and change a media item (story 196). A folder or item may carry
|
|
3
|
+
* a rule naming member groups that may read and write; the nearest rule on an
|
|
4
|
+
* item's path governs, as for protected pages. Each item also has its own read
|
|
5
|
+
* and write policy, stamped from the folder's defaults when it was created.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** An item's own setting: `inherit` follows the nearest rule. */
|
|
9
|
+
export type MediaPolicy = 'inherit' | 'public' | 'members' | 'groups' | 'creator' | 'none'
|
|
10
|
+
|
|
11
|
+
/** Who may see a file created in a folder later. */
|
|
12
|
+
export type NewMediaRead = 'public' | 'members' | 'groups' | 'creator' | 'none'
|
|
13
|
+
/** Who may change a file created in a folder later. */
|
|
14
|
+
export type NewMediaWrite = 'groups' | 'creator' | 'none'
|
|
15
|
+
|
|
16
|
+
export const MEDIA_POLICIES: readonly MediaPolicy[] = [
|
|
17
|
+
'inherit',
|
|
18
|
+
'public',
|
|
19
|
+
'members',
|
|
20
|
+
'groups',
|
|
21
|
+
'creator',
|
|
22
|
+
'none',
|
|
23
|
+
]
|
|
24
|
+
export const NEW_MEDIA_READ: readonly NewMediaRead[] = [
|
|
25
|
+
'public',
|
|
26
|
+
'none',
|
|
27
|
+
'members',
|
|
28
|
+
'groups',
|
|
29
|
+
'creator',
|
|
30
|
+
]
|
|
31
|
+
export const NEW_MEDIA_WRITE: readonly NewMediaWrite[] = ['groups', 'creator', 'none']
|
|
32
|
+
|
|
33
|
+
/** A rule set on a media folder or item, governing it and everything below that sets none. */
|
|
34
|
+
export interface MediaRule {
|
|
35
|
+
/** The node the rule is set on. */
|
|
36
|
+
nodeId: number
|
|
37
|
+
/** Anyone may read, whatever the groups say. */
|
|
38
|
+
publicRead: boolean
|
|
39
|
+
readGroups: readonly string[]
|
|
40
|
+
writeGroups: readonly string[]
|
|
41
|
+
readMembers?: readonly string[]
|
|
42
|
+
writeMembers?: readonly string[]
|
|
43
|
+
/** What files created below get; null leaves them following the rule. */
|
|
44
|
+
newRead: NewMediaRead | null
|
|
45
|
+
newWrite: NewMediaWrite | null
|
|
46
|
+
/** File types a member may upload below (story 198), by name; empty is none. */
|
|
47
|
+
memberTypes: readonly string[]
|
|
48
|
+
/** The largest file a member may upload below, in bytes; null sets no limit of its own. */
|
|
49
|
+
memberMaxBytes: number | null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What an item says about itself. */
|
|
53
|
+
export interface MediaItemAccess {
|
|
54
|
+
readPolicy: MediaPolicy
|
|
55
|
+
writePolicy: MediaPolicy
|
|
56
|
+
/** The member who created it, by key. */
|
|
57
|
+
creatorMemberKey: string | null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The member asking, by key and the names of their groups. */
|
|
61
|
+
export interface MediaMember {
|
|
62
|
+
key: string
|
|
63
|
+
groups: readonly string[]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const OPEN_MEDIA: MediaItemAccess = {
|
|
67
|
+
readPolicy: 'inherit',
|
|
68
|
+
writePolicy: 'inherit',
|
|
69
|
+
creatorMemberKey: null,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const parseMediaPolicy = (value: unknown): MediaPolicy =>
|
|
73
|
+
MEDIA_POLICIES.includes(value as MediaPolicy) ? (value as MediaPolicy) : 'inherit'
|
|
74
|
+
|
|
75
|
+
/** The nearest rule on a path of node ids (`-1,1050,1063`), the node's own first. Pure. */
|
|
76
|
+
export function nearestMediaRule(
|
|
77
|
+
path: string,
|
|
78
|
+
rules: ReadonlyMap<number, MediaRule>,
|
|
79
|
+
): MediaRule | undefined {
|
|
80
|
+
const ids = path.split(',').map(Number)
|
|
81
|
+
for (let i = ids.length - 1; i >= 0; i--) {
|
|
82
|
+
const rule = rules.get(ids[i] as number)
|
|
83
|
+
if (rule) return rule
|
|
84
|
+
}
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const inGroups = (names: readonly string[], member: MediaMember | undefined) => {
|
|
89
|
+
if (!member) return false
|
|
90
|
+
const held = new Set(member.groups.map((g) => g.toLowerCase()))
|
|
91
|
+
return names.some((name) => held.has(name.toLowerCase()))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether a member, or a visitor when `member` is undefined, may read or change
|
|
96
|
+
* an item. The item's own policy wins; `inherit` follows the nearest rule, and
|
|
97
|
+
* with no rule anyone may read and no member may change. Groups match in any
|
|
98
|
+
* case. Pure.
|
|
99
|
+
*/
|
|
100
|
+
export function memberMay(
|
|
101
|
+
action: 'read' | 'write',
|
|
102
|
+
item: MediaItemAccess,
|
|
103
|
+
rule: MediaRule | undefined,
|
|
104
|
+
member: MediaMember | undefined,
|
|
105
|
+
): boolean {
|
|
106
|
+
const policy = action === 'read' ? item.readPolicy : item.writePolicy
|
|
107
|
+
const groups = action === 'read' ? (rule?.readGroups ?? []) : (rule?.writeGroups ?? [])
|
|
108
|
+
const selected = action === 'read' ? rule?.readMembers : rule?.writeMembers
|
|
109
|
+
const allowed =
|
|
110
|
+
inGroups(groups, member) ||
|
|
111
|
+
Boolean(member && selected?.some((key) => key.toLowerCase() === member.key.toLowerCase()))
|
|
112
|
+
switch (policy) {
|
|
113
|
+
case 'public':
|
|
114
|
+
return action === 'read'
|
|
115
|
+
case 'members':
|
|
116
|
+
return action === 'read' && member !== undefined
|
|
117
|
+
case 'groups':
|
|
118
|
+
return allowed
|
|
119
|
+
case 'creator':
|
|
120
|
+
return (
|
|
121
|
+
member !== undefined &&
|
|
122
|
+
item.creatorMemberKey !== null &&
|
|
123
|
+
member.key.toLowerCase() === item.creatorMemberKey.toLowerCase()
|
|
124
|
+
)
|
|
125
|
+
case 'none':
|
|
126
|
+
return false
|
|
127
|
+
case 'inherit':
|
|
128
|
+
if (action === 'read') return !rule || rule.publicRead || allowed
|
|
129
|
+
return allowed
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether an item is closed to a visitor who is not signed in. Pure. */
|
|
134
|
+
export const isProtectedMedia = (item: MediaItemAccess, rule: MediaRule | undefined): boolean =>
|
|
135
|
+
!memberMay('read', item, rule, undefined)
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The policies a new file gets from the rule above it: `inherit`, following
|
|
139
|
+
* the folder live, unless the rule sets an explicit default for new files. Pure.
|
|
140
|
+
*/
|
|
141
|
+
export function stampedPolicies(rule: MediaRule | undefined): {
|
|
142
|
+
readPolicy: MediaPolicy
|
|
143
|
+
writePolicy: MediaPolicy
|
|
144
|
+
} {
|
|
145
|
+
return { readPolicy: rule?.newRead ?? 'inherit', writePolicy: rule?.newWrite ?? 'inherit' }
|
|
146
|
+
}
|
package/src/uploads.ts
CHANGED
|
@@ -39,3 +39,127 @@ export function isUploadAllowed(fileName: string, settings: UploadSettings): boo
|
|
|
39
39
|
if (settings.disallowedExtensions.includes(extension)) return false
|
|
40
40
|
return settings.allowedExtensions.length === 0 || settings.allowedExtensions.includes(extension)
|
|
41
41
|
}
|
|
42
|
+
|
|
43
|
+
/** The extension of a file name, lowercase and without the dot; `''` when it has none. Pure. */
|
|
44
|
+
export const extensionOf = (fileName: string): string => {
|
|
45
|
+
const dot = fileName.lastIndexOf('.')
|
|
46
|
+
return dot >= 0 ? fileName.slice(dot + 1).toLowerCase() : ''
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A file type a member may be allowed to upload into a media folder (story
|
|
51
|
+
* 198), recognised by its first bytes. `image` types are re-encoded on the way
|
|
52
|
+
* in, so their metadata is stripped.
|
|
53
|
+
*/
|
|
54
|
+
export interface MemberFileType {
|
|
55
|
+
/** Stored on the folder's rule. */
|
|
56
|
+
id: string
|
|
57
|
+
label: string
|
|
58
|
+
extensions: readonly string[]
|
|
59
|
+
image: boolean
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const MEMBER_FILE_TYPES: readonly MemberFileType[] = [
|
|
63
|
+
{ id: 'jpeg', label: 'JPEG image', extensions: ['jpg', 'jpeg'], image: true },
|
|
64
|
+
{ id: 'png', label: 'PNG image', extensions: ['png'], image: true },
|
|
65
|
+
{ id: 'gif', label: 'GIF image', extensions: ['gif'], image: true },
|
|
66
|
+
{ id: 'webp', label: 'WebP image', extensions: ['webp'], image: true },
|
|
67
|
+
{ id: 'avif', label: 'AVIF image', extensions: ['avif'], image: true },
|
|
68
|
+
{ id: 'heic', label: 'HEIC image (iPhone)', extensions: ['heic', 'heif'], image: true },
|
|
69
|
+
{ id: 'pdf', label: 'PDF document', extensions: ['pdf'], image: false },
|
|
70
|
+
{ id: 'mp3', label: 'MP3 audio', extensions: ['mp3'], image: false },
|
|
71
|
+
{ id: 'wav', label: 'WAV audio', extensions: ['wav'], image: false },
|
|
72
|
+
{ id: 'mp4', label: 'MP4 video', extensions: ['mp4', 'm4v'], image: false },
|
|
73
|
+
{ id: 'mov', label: 'QuickTime video', extensions: ['mov'], image: false },
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
const memberFileType = (id: string) => MEMBER_FILE_TYPES.find((type) => type.id === id)
|
|
77
|
+
|
|
78
|
+
const ascii = (bytes: Uint8Array, at: number, length: number) =>
|
|
79
|
+
String.fromCharCode(...bytes.subarray(at, at + length))
|
|
80
|
+
|
|
81
|
+
/** The brands an ISO media file's `ftyp` box names: its major brand and the compatible ones. */
|
|
82
|
+
function ftypBrands(bytes: Uint8Array): string[] {
|
|
83
|
+
if (bytes.length < 12 || ascii(bytes, 4, 4) !== 'ftyp') return []
|
|
84
|
+
const size = Math.min(
|
|
85
|
+
bytes.length,
|
|
86
|
+
((bytes[0] as number) << 24) |
|
|
87
|
+
((bytes[1] as number) << 16) |
|
|
88
|
+
((bytes[2] as number) << 8) |
|
|
89
|
+
(bytes[3] as number),
|
|
90
|
+
)
|
|
91
|
+
const brands = [ascii(bytes, 8, 4)]
|
|
92
|
+
for (let at = 16; at + 4 <= size; at += 4) brands.push(ascii(bytes, at, 4))
|
|
93
|
+
return brands
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* What a file is, read from its first bytes (its magic number) for the types
|
|
98
|
+
* members may be allowed to upload; undefined for anything else. Pure.
|
|
99
|
+
*/
|
|
100
|
+
export function detectFileType(bytes: Uint8Array): string | undefined {
|
|
101
|
+
const starts = (...values: number[]) => values.every((value, i) => bytes[i] === value)
|
|
102
|
+
if (starts(0xff, 0xd8, 0xff)) return 'jpeg'
|
|
103
|
+
if (starts(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) return 'png'
|
|
104
|
+
if (ascii(bytes, 0, 6) === 'GIF87a' || ascii(bytes, 0, 6) === 'GIF89a') return 'gif'
|
|
105
|
+
if (ascii(bytes, 0, 4) === 'RIFF' && ascii(bytes, 8, 4) === 'WEBP') return 'webp'
|
|
106
|
+
if (ascii(bytes, 0, 4) === 'RIFF' && ascii(bytes, 8, 4) === 'WAVE') return 'wav'
|
|
107
|
+
if (ascii(bytes, 0, 5) === '%PDF-') return 'pdf'
|
|
108
|
+
if (ascii(bytes, 0, 3) === 'ID3' || (bytes[0] === 0xff && ((bytes[1] ?? 0) & 0xe0) === 0xe0))
|
|
109
|
+
return 'mp3'
|
|
110
|
+
const brands = ftypBrands(bytes)
|
|
111
|
+
if (brands.length > 0) {
|
|
112
|
+
if (brands.some((b) => b === 'avif' || b === 'avis')) return 'avif'
|
|
113
|
+
if (
|
|
114
|
+
brands.some((b) =>
|
|
115
|
+
['heic', 'heix', 'heim', 'heis', 'hevc', 'hevx', 'mif1', 'msf1'].includes(b),
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
return 'heic'
|
|
119
|
+
if (brands[0] === 'qt ') return 'mov'
|
|
120
|
+
if (
|
|
121
|
+
brands.some((b) =>
|
|
122
|
+
['isom', 'iso2', 'iso4', 'iso5', 'iso6', 'mp41', 'mp42', 'avc1', 'M4V ', 'dash'].includes(
|
|
123
|
+
b,
|
|
124
|
+
),
|
|
125
|
+
)
|
|
126
|
+
)
|
|
127
|
+
return 'mp4'
|
|
128
|
+
}
|
|
129
|
+
return undefined
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Why a member's upload is refused, or the type it is. */
|
|
133
|
+
export type MemberUploadCheck =
|
|
134
|
+
| { ok: true; type: MemberFileType }
|
|
135
|
+
| { ok: false; reason: string; status: 400 | 415 }
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether a member may upload this file where `allowed` lists the types
|
|
139
|
+
* accepted: what its first bytes say it is, its extension and the list must
|
|
140
|
+
* all agree, and the site's own upload settings must not refuse it. Pure.
|
|
141
|
+
*/
|
|
142
|
+
export function checkMemberUpload(
|
|
143
|
+
fileName: string,
|
|
144
|
+
head: Uint8Array,
|
|
145
|
+
allowed: readonly string[],
|
|
146
|
+
settings: UploadSettings = DEFAULT_UPLOAD_SETTINGS,
|
|
147
|
+
): MemberUploadCheck {
|
|
148
|
+
const extension = extensionOf(fileName)
|
|
149
|
+
if (!extension)
|
|
150
|
+
return { ok: false, status: 400, reason: 'The file needs an extension, such as .jpg.' }
|
|
151
|
+
if (!isUploadAllowed(fileName, settings))
|
|
152
|
+
return { ok: false, status: 415, reason: `This site does not accept .${extension} files.` }
|
|
153
|
+
const detected = detectFileType(head)
|
|
154
|
+
const type = detected ? memberFileType(detected) : undefined
|
|
155
|
+
if (!type) return { ok: false, status: 415, reason: 'This kind of file cannot be uploaded here.' }
|
|
156
|
+
if (!type.extensions.includes(extension))
|
|
157
|
+
return {
|
|
158
|
+
ok: false,
|
|
159
|
+
status: 415,
|
|
160
|
+
reason: `The file is named .${extension} but is a ${type.label}; rename it and try again.`,
|
|
161
|
+
}
|
|
162
|
+
if (!allowed.includes(type.id))
|
|
163
|
+
return { ok: false, status: 415, reason: `A ${type.label} cannot be uploaded here.` }
|
|
164
|
+
return { ok: true, type }
|
|
165
|
+
}
|