Uploaded images, fonts and sounds belonged to nobody. Every account saw every file in the pickers, and only an administrator could delete one, through the API alone. Now every account uploads into a folder of its own, sees its own files and nobody's, and can delete a file of its own that no project uses. - Files are stored as data/<kind>/<folder>/<sha256>.<ext>, and the folder is part of asset.file. A name without a folder belongs to nobody. - The folder is a random 12-character name kept in users.json and given with the first upload. Renaming an account moves no file, and a released game's URLs carry no account name. - The list shows the caller's own files and nobody's. An administrator sees all, each with owner and deletable. - DELETE takes the name with its folder. It answers 403 unless the file is the caller's or the caller is an administrator, and 409 while a project, snapshot or release of any account names it (FileUsage). The 409 names the caller's own projects only. - The pickers have a Delete button. The first press asks, the second deletes, and a refusal shows the server's reason. - A deleted account's folder stays and only administrators see it. - tools/migrate-file-owners.php moved every used file into the folder of the account using it (a copy each for two accounts) and rewrote projects, snapshots and releases. A project's revision goes up, so an open editor gets a conflict instead of saving old names back. - Font families are named after the hash alone, so a folder never ends up in a CSS family name. Run on stein2 on 2026-10-07, backup in /root/config-backup-20261007/data-before-344: 25 files used, 27 copies, 8 projects, 53 snapshots and 1 release rewritten, 30 unused files stay nobody's. Afterwards all 600 references resolve to a file in their owner's folder. Found while building: the Delete button cut its new label to "Del…" because a button does not measure itself again on a new text. The picker now lays its button row out again. Tests: - PHP files.test.php (new): 46 checks, counter-check 3 red - Unit core + editor: 220 files, 3403 tests - e2e 34 (melektaus-atx): ALL PASSED - 67 checks (2 new) - Gate (melektaus-atx): CHECK EXIT 0, 256 files, 3977 tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.5 KiB
PHP
64 lines
2.5 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Content-addressed font store: `data/fonts/{folder}/{sha256}.{ext}` (see HashedFileStore).
|
||
*
|
||
* A font is stored byte for byte. There is no re-encoding step as there is for images, so what
|
||
* the browser loads is exactly what was picked. The picker shows each face by rendering with it,
|
||
* so a record has nothing to read out of the file.
|
||
*/
|
||
final class FontStore extends HashedFileStore
|
||
{
|
||
/**
|
||
* Accepted formats, keyed by the four bytes every one of them starts with. Bitmap and
|
||
* collection formats are absent: a `.ttc` holds several faces and `@font-face` cannot say
|
||
* which, and SVG fonts can carry script.
|
||
*/
|
||
private const SIGNATURES = [
|
||
"\x00\x01\x00\x00" => 'ttf',
|
||
'true' => 'ttf',
|
||
'OTTO' => 'otf',
|
||
'wOFF' => 'woff',
|
||
'wOF2' => 'woff2',
|
||
];
|
||
|
||
protected static function filePattern(): string
|
||
{
|
||
return '[a-f0-9]{64}\.(ttf|otf|woff|woff2)';
|
||
}
|
||
|
||
/**
|
||
* Stores the raw bytes of a typeface and returns its record.
|
||
*
|
||
* The format is read from the file's own signature, never from anything the client says.
|
||
* `finfo` is not consulted the way it is for images: it reports fonts inconsistently across
|
||
* libmagic versions (`font/sfnt`, `application/vnd.ms-opentype`, plain `application/octet-stream`),
|
||
* so a second opinion from it would reject valid files rather than catch bad ones.
|
||
*
|
||
* `$originalName` is kept beside the file as a caption (see OriginalName): the hash says
|
||
* nothing, and the name inside the font is deliberately never read (see `gm-font-` families).
|
||
*
|
||
* @return array{file:string,name:string,bytes:int}
|
||
* @throws InvalidFontException when the bytes are not a supported font
|
||
*/
|
||
public function store(string $bytes, string $originalName, string $folder): array
|
||
{
|
||
$extension = self::SIGNATURES[substr($bytes, 0, 4)] ?? '';
|
||
if ($extension === '') {
|
||
throw new InvalidFontException('Not a font file – use TTF, OTF, WOFF or WOFF2');
|
||
}
|
||
// The shortest thing that can still be a font: a table directory and one entry.
|
||
if (strlen($bytes) < 28) {
|
||
throw new InvalidFontException('Font file is truncated');
|
||
}
|
||
|
||
return $this->keep($bytes, $extension, $originalName, $folder);
|
||
}
|
||
}
|
||
|
||
/** Thrown by FontStore::store when the uploaded bytes are not a usable font. */
|
||
final class InvalidFontException extends RuntimeException
|
||
{
|
||
}
|