mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
refactor(server): boot dir-creation to driver init
This commit is contained in:
@@ -1,37 +1,46 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { CATEGORY_PREFIXES } from '../../src/nest/storage/storage-registry.service';
|
||||
|
||||
// The set of uploads subdirectories is hand-mirrored between the image build and
|
||||
// the boot-time mkdir in server/src/index.ts. Drift is invisible until a user on
|
||||
// a host whose bind-mounted uploads dir isn't writable by node hits an EACCES on
|
||||
// first upload to the missing dir (#1762), so pin the two copies together.
|
||||
// Driver-owned roots are created by LocalDriver.init on every storage-registry
|
||||
// load now (boot and reload()) — index.ts no longer mkdirs the uploads tree.
|
||||
// The Dockerfile still pre-creates the subdirs (and chowns them) at image
|
||||
// build, so its `mkdir -p` list must stay in sync with the category prefixes
|
||||
// rooted under uploads/. Drift is invisible until a user on a host whose
|
||||
// bind-mounted uploads dir isn't writable by node hits an EACCES on first
|
||||
// upload to the missing dir (#1762), so pin the two lists together.
|
||||
const repoRoot = path.resolve(__dirname, '../../..');
|
||||
|
||||
const readSubdirs = (file: string, pattern: RegExp): Set<string> => {
|
||||
const source = fs.readFileSync(path.join(repoRoot, file), 'utf8');
|
||||
const readDockerfileSubdirs = (): Set<string> => {
|
||||
const source = fs.readFileSync(path.join(repoRoot, 'Dockerfile'), 'utf8');
|
||||
const found = new Set<string>();
|
||||
for (const m of source.matchAll(pattern)) found.add(m[1]);
|
||||
for (const m of source.matchAll(/\/app\/uploads\/([A-Za-z0-9_-]+)/g)) found.add(m[1]);
|
||||
return found;
|
||||
};
|
||||
|
||||
describe('uploads subdirectory parity', () => {
|
||||
const fromDockerfile = readSubdirs('Dockerfile', /\/app\/uploads\/([A-Za-z0-9_-]+)/g);
|
||||
const fromBoot = readSubdirs('server/src/index.ts', /path\.join\(uploadsDir, '([A-Za-z0-9_-]+)'\)/g);
|
||||
const fromDockerfile = readDockerfileSubdirs();
|
||||
// The registry's real values: every non-empty category prefix lives under
|
||||
// uploads/ (backups' empty prefix is rooted in data/, not uploads/), and the
|
||||
// nested cache prefixes (photos/google, photos/trek) collapse into their
|
||||
// top-level dir.
|
||||
const fromRegistry = new Set(
|
||||
Object.values(CATEGORY_PREFIXES)
|
||||
.filter((p) => p !== '')
|
||||
.map((p) => p.split('/')[0]),
|
||||
);
|
||||
|
||||
it('finds both lists (guards against the regexes silently matching nothing)', () => {
|
||||
it('finds the Dockerfile list (guards against the regex silently matching nothing)', () => {
|
||||
expect(fromDockerfile.size).toBeGreaterThan(0);
|
||||
expect(fromBoot.size).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('creates the same uploads subdirs in the Dockerfile and at boot', () => {
|
||||
expect([...fromBoot].sort()).toEqual([...fromDockerfile].sort());
|
||||
it('creates the same uploads subdirs in the Dockerfile and at registry init', () => {
|
||||
expect([...fromRegistry].sort()).toEqual([...fromDockerfile].sort());
|
||||
});
|
||||
|
||||
it('covers every uploads subdir the server writes to', () => {
|
||||
// journey/ and places/ are written by journey.controller.ts and
|
||||
// services/placeImage.ts; the rest are the long-standing upload stores.
|
||||
expect([...fromBoot].sort()).toEqual([
|
||||
expect([...fromRegistry].sort()).toEqual([
|
||||
'avatars',
|
||||
'covers',
|
||||
'files',
|
||||
|
||||
Reference in New Issue
Block a user