Give each account its own folder of uploaded files to delete (#344)

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>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-07 08:25:13 +02:00
1 parent a2d0a02645
commit 6b49a0bea7
34 files changed
+910 -111

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
/**
* Which projects still use a stored image, font or sound (#344) – asked before one is deleted.
*
* A project counts when the file is named in the project itself, in one of its snapshots or in its
* release: restoring a snapshot or playing a released game must not find a hole. Any string value
* equal to the stored name counts, wherever in the document it is; that may refuse a file nothing
* really draws, but never lets one go that something does.
*/
final class FileUsage
{
public function __construct(private readonly string $data) {}
/**
* The projects using `$file`, each once.
*
* @return list<array{id:string,owner:string,name:string}>
*/
public function of(string $file): array
{
$used = [];
foreach (glob("{$this->data}/projects/*.json") ?: [] as $path) {
if ($this->names($path, $file)) {
$used[basename($path, '.json')] = true;
}
}
foreach (glob("{$this->data}/snapshots/*/*.json") ?: [] as $path) {
if ($this->names($path, $file)) {
$used[basename(dirname($path))] = true;
}
}
foreach (glob("{$this->data}/releases/*.json") ?: [] as $path) {
$release = $this->decoded($path, $file);
if ($release !== null && is_string($release['projectId'] ?? null) && self::uses($release['project'] ?? null, $file)) {
$used[$release['projectId']] = true;
}
}
$uses = [];
foreach (array_keys($used) as $id) {
$uses[] = ['id' => (string) $id, ...$this->project((string) $id)];
}
return $uses;
}
/**
* Why a file cannot go. The asker's own projects are named; somebody else's only for an
* administrator, everybody else learns no more than that there is one.
*
* @param list<array{id:string,owner:string,name:string}> $uses
*/
public static function refusal(array $uses, string $asker, bool $seesAll): string
{
/** @var array<string,int> $labels each name once, with how many projects carry it */
$labels = [];
$others = 0;
foreach ($uses as $use) {
if (strcasecmp($use['owner'], $asker) === 0) {
$label = "“{$use['name']}”";
} elseif ($seesAll) {
$label = "“{$use['name']}” of " . ($use['owner'] === '' ? 'nobody' : $use['owner']);
} else {
$others++;
continue;
}
$labels[$label] = ($labels[$label] ?? 0) + 1;
}
$named = [];
foreach ($labels as $label => $count) {
$named[] = $count === 1 ? $label : "{$label} ({$count} projects)";
}
if ($others > 0) {
$named[] = $others === 1 ? 'a project of another account' : "{$others} projects of other accounts";
}
$list = count($named) > 1 ? implode(', ', array_slice($named, 0, -1)) . ' and ' . end($named) : $named[0];
return "Still used by {$list}, counting snapshots and releases";
}
/** Whether a value holds `$file` as a string anywhere inside it. */
public static function uses(mixed $value, string $file): bool
{
if (is_string($value)) {
return $value === $file;
}
if (is_array($value)) {
foreach ($value as $item) {
if (self::uses($item, $file)) {
return true;
}
}
}
return false;
}
private function names(string $path, string $file): bool
{
$doc = $this->decoded($path, $file);
return $doc !== null && self::uses($doc['project'] ?? null, $file);
}
/**
* The document, or null when it cannot name the file. The text is searched for the hash first,
* which has no slash to be escaped: most documents are never decoded.
*/
private function decoded(string $path, string $file): ?array
{
$raw = file_get_contents($path);
if ($raw === false || !str_contains($raw, basename($file))) {
return null;
}
$doc = json_decode($raw, true);
return is_array($doc) ? $doc : null;
}
/** @return array{owner:string,name:string} */
private function project(string $id): array
{
$path = "{$this->data}/projects/{$id}.json";
$raw = is_file($path) ? file_get_contents($path) : false;
$doc = $raw === false ? null : json_decode($raw, true);
return [
'owner' => is_string($doc['owner'] ?? null) ? $doc['owner'] : '',
'name' => is_string($doc['name'] ?? null) ? $doc['name'] : 'Untitled',
];
}
}