domma-cms 1.47.2 → 1.47.4
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/CHANGELOG.md +10 -0
- package/admin/js/lib/form-submissions-arrange.js +1 -1
- package/admin/js/lib/private-files.js +1 -0
- package/admin/js/templates/docs/api-forms.html +10 -1
- package/admin/js/templates/docs/tutorial-forms.html +6 -0
- package/admin/js/views/collection-entries.js +7 -7
- package/admin/js/views/form-submissions.js +2 -2
- package/docs/DommaFeatures.md +44 -6
- package/package.json +1 -1
- package/plugins/free-tier.lock.json +9 -9
- package/plugins/shopping-cart/CHANGELOG.md +4 -0
- package/plugins/shopping-cart/CLAUDE.md +3 -0
- package/plugins/shopping-cart/admin/views/products.js +2 -2
- package/plugins/shopping-cart/plugin.json +1 -1
- package/plugins/shopping-cart/public/lib/products.js +14 -0
- package/plugins/shopping-cart/public/shop.css +21 -2
- package/plugins/shopping-cart/tests/pure.test.js +12 -1
- package/public/js/form-answers.mjs +1 -0
- package/server/routes/api/forms.js +161 -16
- package/server/server.js +15 -1
- package/server/services/email.js +30 -11
- package/server/services/form-uploads-migration.js +254 -0
- package/server/services/formUploads.js +161 -0
- package/server/services/markdown.js +3 -1
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form-uploads migration - one-off move of visitors' form uploads out of
|
|
3
|
+
* content/media/ into the private store (services/formUploads.js).
|
|
4
|
+
*
|
|
5
|
+
* Before uploads were private, saveFormFiles() wrote each file to
|
|
6
|
+
* content/media/<8 hex>-<name>, served to anyone at /media/ and listed in the
|
|
7
|
+
* Media library. This finds every file a form SUBMISSION (an entry stamped
|
|
8
|
+
* `meta.source: 'form:<slug>'`) references that way - in one of the form's file
|
|
9
|
+
* fields, with exactly that shape, resolved through fileIn inside the media root,
|
|
10
|
+
* and a regular file (never a link or a folder) - and nothing else in Media:
|
|
11
|
+
*
|
|
12
|
+
* 1. copies it to content/form-uploads/<form slug>/<entry id>/<name>
|
|
13
|
+
* (never over a file already there: one there is a previous run's copy);
|
|
14
|
+
* 2. rewrites the reference to the admin-only route (uploadUrl);
|
|
15
|
+
* 3. removes the media copy - unless the file's name appears anywhere else in
|
|
16
|
+
* the site's content (a page, block, menu, view, config, another
|
|
17
|
+
* collection's data, an entry that is not a submission), where removing it
|
|
18
|
+
* would break a link. Then the media copy is left: COPIED, not moved.
|
|
19
|
+
*
|
|
20
|
+
* The "used elsewhere" test is a text search for the stored name (it carries a
|
|
21
|
+
* random 8-hex prefix, so a match is not a coincidence) over content/ and
|
|
22
|
+
* config/ text files, minus the submissions themselves. Collections kept in
|
|
23
|
+
* MongoDB are searched only for the forms' own collections.
|
|
24
|
+
*
|
|
25
|
+
* Idempotent and resumable: a reference already private is skipped; a missing
|
|
26
|
+
* file is skipped (and counted) with its reference left as it was; the media
|
|
27
|
+
* copies still to remove are recorded in content/form-uploads/.migration.json
|
|
28
|
+
* as each entry is rewritten, and are removed only once no submission still
|
|
29
|
+
* links to them. The marker (content/.migrations.json) is written when a run
|
|
30
|
+
* finishes with nothing left to retry.
|
|
31
|
+
*/
|
|
32
|
+
import fs from 'fs/promises';
|
|
33
|
+
import {existsSync} from 'fs';
|
|
34
|
+
import path from 'path';
|
|
35
|
+
import {config} from '../config.js';
|
|
36
|
+
import {fileRefs} from '../../public/js/form-files.mjs';
|
|
37
|
+
import {fileIn} from './safePath.js';
|
|
38
|
+
import {formUploadsRoot, keepUploadsOutOfGit, parseUploadUrl, uploadPath, uploadUrl, validParts} from './formUploads.js';
|
|
39
|
+
|
|
40
|
+
export const MIGRATION_ID = 'form-uploads-private';
|
|
41
|
+
|
|
42
|
+
/** What saveFormFiles() stored before uploads were private. */
|
|
43
|
+
const LEGACY_URL = /^\/media\/([0-9a-f]{8}-[\w.-]{1,200})$/;
|
|
44
|
+
const TEXT_FILE = /\.(md|markdown|json|html?|txt|css|js|mjs|ya?ml|xml|csv)$/i;
|
|
45
|
+
const MAX_TEXT_BYTES = 8 * 1024 * 1024;
|
|
46
|
+
|
|
47
|
+
const contentRoot = () => path.dirname(path.resolve(config.content.collectionsDir));
|
|
48
|
+
const defaultMarkerPath = () => path.join(contentRoot(), '.migrations.json');
|
|
49
|
+
|
|
50
|
+
async function readJson(file, fallback) {
|
|
51
|
+
try { return JSON.parse(await fs.readFile(file, 'utf8')) ?? fallback; } catch { return fallback; }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The collection a form's submissions are in, and whether an entry is one of them (as routes/api/forms.js). */
|
|
55
|
+
const collectionOf = (form) => (form?.actions?.collection?.enabled && form.actions.collection.slug) ? form.actions.collection.slug : form?.slug;
|
|
56
|
+
/**
|
|
57
|
+
* A real submission of the form: stamped `meta.source: 'form:<slug>'` by the submit
|
|
58
|
+
* route. Only these are migrated - an entry made any other way (the admin, a token,
|
|
59
|
+
* a public create, an import) may carry a planted /media/ reference, and must not
|
|
60
|
+
* pull a Media file out of public view.
|
|
61
|
+
*/
|
|
62
|
+
const isSubmissionOf = (form, entry) => entry?.meta?.source === `form:${form.slug}`;
|
|
63
|
+
|
|
64
|
+
/** The legacy media name a reference points at, or null. */
|
|
65
|
+
export function legacyName(ref) {
|
|
66
|
+
const m = LEGACY_URL.exec(String(ref?.url || ''));
|
|
67
|
+
return m && m[1] !== '.' && m[1] !== '..' ? m[1] : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Every text file under dir, skipping the folders in `skip` (absolute paths). */
|
|
71
|
+
async function* textFiles(dir, skip) {
|
|
72
|
+
let list;
|
|
73
|
+
try { list = await fs.readdir(dir, {withFileTypes: true}); } catch { return; }
|
|
74
|
+
for (const d of list) {
|
|
75
|
+
const p = path.join(dir, d.name);
|
|
76
|
+
if (skip.has(p)) continue;
|
|
77
|
+
if (d.isDirectory()) yield* textFiles(p, skip);
|
|
78
|
+
else if (d.isFile() && TEXT_FILE.test(d.name)) yield p;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Run the migration.
|
|
84
|
+
*
|
|
85
|
+
* @param {{markerPath?: string, mediaDir?: string, uploadsRoot?: string, searchRoots?: string[], force?: boolean, log?: {info: Function, warn: Function}}} [opts]
|
|
86
|
+
* @returns {Promise<{ran: boolean, entries: number, moved: number, copied: number, missing: number, skipped: number, pending: string[]}>}
|
|
87
|
+
*/
|
|
88
|
+
export async function migrateFormUploads({
|
|
89
|
+
markerPath = defaultMarkerPath(),
|
|
90
|
+
mediaDir = path.resolve(config.content.mediaDir),
|
|
91
|
+
uploadsRoot = formUploadsRoot(),
|
|
92
|
+
searchRoots = [contentRoot(), path.resolve(contentRoot(), '..', 'config')],
|
|
93
|
+
force = false
|
|
94
|
+
} = {}) {
|
|
95
|
+
const result = {ran: false, entries: 0, moved: 0, copied: 0, missing: 0, skipped: 0, pending: []};
|
|
96
|
+
const marker = await readJson(markerPath, {});
|
|
97
|
+
if (marker[MIGRATION_ID] && !force) return result;
|
|
98
|
+
result.ran = true;
|
|
99
|
+
|
|
100
|
+
const {listForms} = await import('./forms.js');
|
|
101
|
+
const {getAdapter} = await import('./adapterRegistry.js');
|
|
102
|
+
const cache = await import('./cache/index.js');
|
|
103
|
+
|
|
104
|
+
const progressFile = path.join(uploadsRoot, '.migration.json');
|
|
105
|
+
const progress = await readJson(progressFile, {remove: []});
|
|
106
|
+
const toRemove = new Set(Array.isArray(progress.remove) ? progress.remove : []);
|
|
107
|
+
const saveProgress = async () => {
|
|
108
|
+
keepUploadsOutOfGit(uploadsRoot);
|
|
109
|
+
await fs.writeFile(progressFile, JSON.stringify({remove: [...toRemove]}, null, 2) + '\n');
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// 1. The forms, their submissions, and the legacy files those reference.
|
|
113
|
+
const forms = (await listForms()).filter(f => f && f.slug);
|
|
114
|
+
const work = []; // {form, collection, adapter, entries}
|
|
115
|
+
const submissionFiles = new Set(); // data.json files of the forms' collections - searched entry by entry instead
|
|
116
|
+
let haystack = ''; // text of everything that is not a submission
|
|
117
|
+
for (const form of forms) {
|
|
118
|
+
const slug = collectionOf(form);
|
|
119
|
+
if (!form.fields?.some(f => f?.type === 'file')) continue;
|
|
120
|
+
try {
|
|
121
|
+
const adapter = await getAdapter(slug);
|
|
122
|
+
const entries = await adapter.all(slug);
|
|
123
|
+
work.push({form, slug, adapter, entries});
|
|
124
|
+
submissionFiles.add(path.join(path.resolve(config.content.collectionsDir), slug));
|
|
125
|
+
} catch {
|
|
126
|
+
result.pending.push(form.slug);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// A collection's entries that are no form's submission (several forms may share one) are "elsewhere".
|
|
130
|
+
for (const slug of new Set(work.map(w => w.slug))) {
|
|
131
|
+
const sharing = forms.filter(f => collectionOf(f) === slug);
|
|
132
|
+
const {entries} = work.find(w => w.slug === slug);
|
|
133
|
+
for (const e of entries) if (!sharing.some(f => isSubmissionOf(f, e))) haystack += JSON.stringify(e.data ?? {}) + '\n';
|
|
134
|
+
}
|
|
135
|
+
const work2 = work.filter(w => w.entries.some(e => isSubmissionOf(w.form, e)
|
|
136
|
+
&& w.form.fields.some(f => f?.type === 'file' && fileRefs(e.data?.[f.name]).some(legacyName))));
|
|
137
|
+
|
|
138
|
+
if (work2.length || toRemove.size) {
|
|
139
|
+
// 2. Where else a name appears: content/ and config/ text, minus media, the store,
|
|
140
|
+
// and the forms' own collection folders (their other entries were added above).
|
|
141
|
+
const skip = new Set([mediaDir, uploadsRoot, ...submissionFiles]);
|
|
142
|
+
for (const root of searchRoots) {
|
|
143
|
+
for await (const file of textFiles(root, skip)) {
|
|
144
|
+
try {
|
|
145
|
+
const st = await fs.stat(file);
|
|
146
|
+
if (st.size <= MAX_TEXT_BYTES) haystack += await fs.readFile(file, 'utf8') + '\n';
|
|
147
|
+
} catch { /* unreadable: not a use */ }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const usedElsewhere = (name) => haystack.includes(name);
|
|
152
|
+
|
|
153
|
+
// 3. Copy, rewrite, record.
|
|
154
|
+
for (const {form, slug, adapter, entries} of work2) {
|
|
155
|
+
let changed = 0;
|
|
156
|
+
for (const entry of entries) {
|
|
157
|
+
if (!isSubmissionOf(form, entry) || !entry?.data) continue;
|
|
158
|
+
let touched = false;
|
|
159
|
+
const data = {...entry.data};
|
|
160
|
+
const moved = [];
|
|
161
|
+
try { for (const f of form.fields) {
|
|
162
|
+
if (f?.type !== 'file' || !f.name || data[f.name] == null) continue;
|
|
163
|
+
const one = !Array.isArray(data[f.name]);
|
|
164
|
+
const list = (one ? [data[f.name]] : data[f.name]).map(ref => {
|
|
165
|
+
const name = legacyName(ref);
|
|
166
|
+
if (!name) return ref;
|
|
167
|
+
if (!validParts(form.slug, String(entry.id ?? ''), name)) { result.skipped++; return ref; }
|
|
168
|
+
let src;
|
|
169
|
+
try { src = fileIn(mediaDir, name); } catch { result.skipped++; return ref; }
|
|
170
|
+
return {ref, name, src};
|
|
171
|
+
});
|
|
172
|
+
const next = [];
|
|
173
|
+
for (const item of list) {
|
|
174
|
+
if (!item || !item.src) { next.push(item); continue; }
|
|
175
|
+
const {ref, name, src} = item;
|
|
176
|
+
const dest = uploadPath(form.slug, String(entry.id), name, uploadsRoot);
|
|
177
|
+
// Only a regular file: lstat does not follow a link, and copyFile would.
|
|
178
|
+
const kind = await fs.lstat(src).then(st => (st.isFile() ? 'file' : 'other'), () => 'none');
|
|
179
|
+
if (kind === 'other') { result.skipped++; next.push(ref); continue; }
|
|
180
|
+
if (kind === 'none' && !existsSync(dest)) { result.missing++; next.push(ref); continue; }
|
|
181
|
+
if (!existsSync(dest)) {
|
|
182
|
+
await fs.mkdir(path.dirname(dest), {recursive: true});
|
|
183
|
+
keepUploadsOutOfGit(uploadsRoot);
|
|
184
|
+
await fs.copyFile(src, dest, fs.constants.COPYFILE_EXCL).catch((err) => { if (err.code !== 'EEXIST') throw err; });
|
|
185
|
+
}
|
|
186
|
+
next.push({...ref, url: uploadUrl(form.slug, String(entry.id), name)});
|
|
187
|
+
moved.push(name);
|
|
188
|
+
touched = true;
|
|
189
|
+
}
|
|
190
|
+
data[f.name] = one ? next[0] : next;
|
|
191
|
+
} } catch {
|
|
192
|
+
// A copy that failed (a full disk): this entry is left as it was, for the next start.
|
|
193
|
+
result.pending.push(`${form.slug}/${entry.id}`);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (!touched) continue;
|
|
197
|
+
// Recorded before the rewrite: a run cut short after it still removes the media copy next time.
|
|
198
|
+
const removing = moved.filter(name => !usedElsewhere(name));
|
|
199
|
+
for (const name of removing) toRemove.add(name);
|
|
200
|
+
try {
|
|
201
|
+
await saveProgress();
|
|
202
|
+
await adapter.update(slug, entry.id, {...entry, data});
|
|
203
|
+
} catch {
|
|
204
|
+
result.pending.push(`${form.slug}/${entry.id}`);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
result.copied += moved.length - removing.length;
|
|
208
|
+
result.moved += removing.length;
|
|
209
|
+
entry.data = data;
|
|
210
|
+
changed++;
|
|
211
|
+
}
|
|
212
|
+
result.entries += changed;
|
|
213
|
+
if (changed) await cache.invalidateTags([`collection:${slug}`]).catch(() => {});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 4. Remove the media copies no submission links to any more (and nothing else uses).
|
|
217
|
+
// Only a name a submission now links to in the store, with the copy there - so
|
|
218
|
+
// whatever the progress file says, nothing else in Media is ever removed.
|
|
219
|
+
const stillLinked = new Set();
|
|
220
|
+
const migrated = new Set();
|
|
221
|
+
for (const {form, entries} of work) {
|
|
222
|
+
for (const e of entries) {
|
|
223
|
+
if (!isSubmissionOf(form, e)) continue;
|
|
224
|
+
for (const f of form.fields) {
|
|
225
|
+
if (f?.type !== 'file' || !f.name) continue;
|
|
226
|
+
for (const ref of fileRefs(e.data?.[f.name])) {
|
|
227
|
+
const n = legacyName(ref);
|
|
228
|
+
if (n) { stillLinked.add(n); continue; }
|
|
229
|
+
const priv = parseUploadUrl(ref.url);
|
|
230
|
+
if (priv && priv.slug === form.slug && existsSync(uploadPath(priv.slug, priv.id, priv.name, uploadsRoot))) migrated.add(priv.name);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
for (const name of [...toRemove]) {
|
|
236
|
+
if (!migrated.has(name) || stillLinked.has(name) || usedElsewhere(name)) { toRemove.delete(name); continue; }
|
|
237
|
+
try {
|
|
238
|
+
const file = fileIn(mediaDir, name);
|
|
239
|
+
if (!(await fs.lstat(file)).isFile()) { toRemove.delete(name); continue; }
|
|
240
|
+
await fs.unlink(file);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
if (err.code !== 'ENOENT') { result.pending.push(name); continue; }
|
|
243
|
+
}
|
|
244
|
+
toRemove.delete(name);
|
|
245
|
+
}
|
|
246
|
+
if (toRemove.size) await saveProgress();
|
|
247
|
+
else await fs.rm(progressFile, {force: true});
|
|
248
|
+
|
|
249
|
+
if (!result.pending.length) {
|
|
250
|
+
const latest = await readJson(markerPath, {});
|
|
251
|
+
await fs.writeFile(markerPath, JSON.stringify({...latest, [MIGRATION_ID]: new Date().toISOString()}, null, 2) + '\n');
|
|
252
|
+
}
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form uploads - the private store for files visitors send with a form.
|
|
3
|
+
*
|
|
4
|
+
* A visitor's file (a CV, a right-to-work document, a photo of a fault) is not
|
|
5
|
+
* site content: it is kept out of content/media/ - so it is neither served at
|
|
6
|
+
* /media/ to anyone with the address nor listed in the admin's Media library -
|
|
7
|
+
* and out of the site repo. Layout:
|
|
8
|
+
*
|
|
9
|
+
* content/form-uploads/<form slug>/<upload id>/<8 hex>-<cleaned name>
|
|
10
|
+
*
|
|
11
|
+
* The upload id is one uuid per submission (the entry id is not known until
|
|
12
|
+
* the entry is written, after its files), or the entry id for a file moved here
|
|
13
|
+
* by form-uploads-migration.js. Every path is built with fileIn
|
|
14
|
+
* (services/safePath.js), one plain segment at a time, so no stored reference
|
|
15
|
+
* can name a file outside this folder.
|
|
16
|
+
*
|
|
17
|
+
* A submission stores each file as {url, name, size, mime}, where url is the
|
|
18
|
+
* admin-only route that serves it (routes/api/forms.js):
|
|
19
|
+
*
|
|
20
|
+
* /api/forms/<form slug>/uploads/<upload id>/<stored name>
|
|
21
|
+
*
|
|
22
|
+
* @module services/formUploads
|
|
23
|
+
*/
|
|
24
|
+
import fs from 'fs/promises';
|
|
25
|
+
import {createReadStream, existsSync, mkdirSync, writeFileSync} from 'fs';
|
|
26
|
+
import path from 'path';
|
|
27
|
+
import {config} from '../config.js';
|
|
28
|
+
import {fileIn} from './safePath.js';
|
|
29
|
+
|
|
30
|
+
/** content/form-uploads - beside content/media, never inside it. */
|
|
31
|
+
export function formUploadsRoot() {
|
|
32
|
+
return path.join(path.dirname(path.resolve(config.content.mediaDir)), 'form-uploads');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** An upload id: a uuid for a new submission, an entry id for a migrated one. */
|
|
36
|
+
const UPLOAD_ID = /^[A-Za-z0-9_-]{1,80}$/;
|
|
37
|
+
/** A stored name: eight hex characters, a dash and the cleaned upload name (cleanUploadName). */
|
|
38
|
+
const STORED_NAME = /^[0-9a-f]{8}-[\w.-]{1,200}$/;
|
|
39
|
+
|
|
40
|
+
/** Whether the three parts of a private reference have the shapes this store writes. */
|
|
41
|
+
export function validParts(slug, id, name) {
|
|
42
|
+
return typeof slug === 'string' && slug !== '' && UPLOAD_ID.test(String(id || ''))
|
|
43
|
+
&& STORED_NAME.test(String(name || '')) && name !== '.' && name !== '..';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The file for slug/id/name, inside the store. Throws (statusCode 404) for any
|
|
48
|
+
* part that is not one plain segment of the expected shape.
|
|
49
|
+
*/
|
|
50
|
+
export function uploadPath(slug, id, name, root = formUploadsRoot()) {
|
|
51
|
+
if (!validParts(slug, id, name)) {
|
|
52
|
+
throw Object.assign(new Error('Not found'), {statusCode: 404, code: 'ENOENT'});
|
|
53
|
+
}
|
|
54
|
+
return fileIn(fileIn(fileIn(root, slug), id), name);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The route a stored reference points at. */
|
|
58
|
+
export function uploadUrl(slug, id, name) {
|
|
59
|
+
return `/api/forms/${encodeURIComponent(slug)}/uploads/${encodeURIComponent(id)}/${encodeURIComponent(name)}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const URL_RE = /^\/api\/forms\/([^/?#]+)\/uploads\/([^/?#]+)\/([^/?#]+)$/;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The parts of a private reference's url, or null when it is not one (or has
|
|
66
|
+
* parts of the wrong shape).
|
|
67
|
+
*
|
|
68
|
+
* @param {string} url
|
|
69
|
+
* @returns {{slug: string, id: string, name: string}|null}
|
|
70
|
+
*/
|
|
71
|
+
export function parseUploadUrl(url) {
|
|
72
|
+
const m = URL_RE.exec(String(url || ''));
|
|
73
|
+
if (!m) return null;
|
|
74
|
+
let slug, id, name;
|
|
75
|
+
try { [slug, id, name] = m.slice(1).map(decodeURIComponent); } catch { return null; }
|
|
76
|
+
return validParts(slug, id, name) ? {slug, id, name} : null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Keep the store out of git: a `*` .gitignore inside it (so a site repo
|
|
81
|
+
* never commits a visitor's file, whatever its own .gitignore says). Never throws.
|
|
82
|
+
*/
|
|
83
|
+
export function keepUploadsOutOfGit(root = formUploadsRoot()) {
|
|
84
|
+
try {
|
|
85
|
+
mkdirSync(root, {recursive: true});
|
|
86
|
+
const ignore = path.join(root, '.gitignore');
|
|
87
|
+
if (!existsSync(ignore)) writeFileSync(ignore, '# Visitors\' form uploads - private, never committed.\n*\n');
|
|
88
|
+
} catch { /* never fail a submission over this */ }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Write one file of a submission. Never over another file: EEXIST is thrown for
|
|
93
|
+
* the caller to pick another name, and a write that fails part way is removed.
|
|
94
|
+
*
|
|
95
|
+
* @returns {Promise<{slug: string, id: string, name: string, url: string}>}
|
|
96
|
+
*/
|
|
97
|
+
export async function saveUpload(slug, id, name, buffer, root = formUploadsRoot()) {
|
|
98
|
+
const file = uploadPath(slug, id, name, root);
|
|
99
|
+
keepUploadsOutOfGit(root);
|
|
100
|
+
await fs.mkdir(path.dirname(file), {recursive: true});
|
|
101
|
+
try {
|
|
102
|
+
await fs.writeFile(file, buffer, {flag: 'wx'});
|
|
103
|
+
} catch (err) {
|
|
104
|
+
if (err.code !== 'EEXIST') await fs.rm(file, {force: true}).catch(() => {});
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
107
|
+
return {slug, id, name, url: uploadUrl(slug, id, name)};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A stored file to serve: its path and size, or null when it is not there (or
|
|
112
|
+
* is not a regular file).
|
|
113
|
+
*/
|
|
114
|
+
export async function statUpload(slug, id, name, root = formUploadsRoot()) {
|
|
115
|
+
let file;
|
|
116
|
+
try { file = uploadPath(slug, id, name, root); } catch { return null; }
|
|
117
|
+
try {
|
|
118
|
+
const st = await fs.lstat(file);
|
|
119
|
+
return st.isFile() ? {file, size: st.size} : null;
|
|
120
|
+
} catch { return null; }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const openUpload = (file) => createReadStream(file);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Delete one stored file, and its submission folder once empty. Answers false
|
|
127
|
+
* when there was nothing to delete. Throws (404) for a reference of the wrong shape.
|
|
128
|
+
*/
|
|
129
|
+
export async function deleteUpload(slug, id, name, root = formUploadsRoot()) {
|
|
130
|
+
const file = uploadPath(slug, id, name, root);
|
|
131
|
+
let st;
|
|
132
|
+
try { st = await fs.lstat(file); } catch (err) { if (err.code === 'ENOENT') return false; throw err; }
|
|
133
|
+
if (!st.isFile()) return false;
|
|
134
|
+
await fs.unlink(file);
|
|
135
|
+
await fs.rmdir(path.dirname(file)).catch(() => {}); // only when empty
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* How a file is sent: inline only for the kinds a browser shows without
|
|
141
|
+
* running anything (PNG, JPEG, GIF, WebP, PDF), as an attachment otherwise.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} name - the stored name
|
|
144
|
+
* @returns {{type: string, inline: boolean}}
|
|
145
|
+
*/
|
|
146
|
+
export function servedAs(name) {
|
|
147
|
+
const ext = path.extname(String(name || '')).toLowerCase();
|
|
148
|
+
const inline = {'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif',
|
|
149
|
+
'.webp': 'image/webp', '.pdf': 'application/pdf'}[ext];
|
|
150
|
+
return inline ? {type: inline, inline: true} : {type: 'application/octet-stream', inline: false};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* A Content-Disposition value: `inline` or `attachment`, an ASCII fallback
|
|
155
|
+
* name and the UTF-8 one (RFC 6266 / 5987).
|
|
156
|
+
*/
|
|
157
|
+
export function disposition(inline, displayName) {
|
|
158
|
+
const raw = String(displayName || 'file').replace(/[\u0000-\u001f\u007f"\\/]/g, '_').slice(0, 200) || 'file';
|
|
159
|
+
const ascii = raw.replace(/[^\x20-\x7e]/g, '_');
|
|
160
|
+
return `${inline ? 'inline' : 'attachment'}; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(raw)}`;
|
|
161
|
+
}
|
|
@@ -93,11 +93,13 @@ export function inertShortcodes(html) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
function escapeHtmlText(str) {
|
|
96
|
+
// ' too: a block template may print {{field}} inside a single-quoted attribute.
|
|
96
97
|
return String(str ?? '')
|
|
97
98
|
.replace(/&/g, '&')
|
|
98
99
|
.replace(/</g, '<')
|
|
99
100
|
.replace(/>/g, '>')
|
|
100
|
-
.replace(/"/g, '"')
|
|
101
|
+
.replace(/"/g, '"')
|
|
102
|
+
.replace(/'/g, ''');
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
/**
|