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:
1 parent
a2d0a02645
commit
6b49a0bea7
34 files changed
+910
-111
No files matched your search
+95
-36
@@ -2,13 +2,17 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* The shared half of the image, font and sound stores: `{dir}/{sha256}.{ext}`.
|
||||
* The shared half of the image, font and sound stores: `{dir}/{folder}/{sha256}.{ext}`.
|
||||
*
|
||||
* The name of a file is the hash of its bytes, which buys four things at once: uploading the same
|
||||
* The name of a file is the hash of its bytes, which buys three things at once: uploading the same
|
||||
* file twice costs one file, a stored file can be cached forever (its URL cannot ever mean
|
||||
* different bytes), projects reference a short string instead of embedding megabytes of base64,
|
||||
* and deleting an asset is undoable for free because nothing here is removed when it stops being
|
||||
* referenced. What a store adds is which bytes it takes, and what its records say beyond the name.
|
||||
* different bytes), and projects reference a short string instead of embedding megabytes of base64.
|
||||
* What a store adds is which bytes it takes, and what its records say beyond the name.
|
||||
*
|
||||
* Every account uploads into a folder of its own (#344, see `UserStore::folderOf`), and the folder
|
||||
* is part of the name a project stores: `{folder}/{sha256}.{ext}`. A name without a folder is a
|
||||
* file that belongs to nobody – what everything uploaded before #344 and used by no project is.
|
||||
* The picker shows an account its own files and nobody's; an administrator sees them all.
|
||||
*/
|
||||
abstract class HashedFileStore
|
||||
{
|
||||
@@ -19,30 +23,45 @@ abstract class HashedFileStore
|
||||
}
|
||||
}
|
||||
|
||||
/** A stored file name, as it appears in a project document. */
|
||||
abstract public static function isValidName(string $name): bool;
|
||||
/** The hash-and-extension half of a stored name, as a regular expression without delimiters. */
|
||||
abstract protected static function filePattern(): string;
|
||||
|
||||
/** A stored file name as it appears in a project document, with or without its folder. */
|
||||
public static function isValidName(string $name): bool
|
||||
{
|
||||
return (bool) preg_match('#^(?:' . UserStore::FOLDER_PATTERN . '/)?' . static::filePattern() . '$#', $name);
|
||||
}
|
||||
|
||||
/** The folder a stored name lies in, '' for a file that belongs to nobody. */
|
||||
public static function folderOf(string $name): string
|
||||
{
|
||||
$slash = strpos($name, '/');
|
||||
return $slash === false ? '' : substr($name, 0, $slash);
|
||||
}
|
||||
|
||||
public function exists(string $file): bool
|
||||
{
|
||||
return static::isValidName($file) && is_file($this->dir . '/' . $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every stored file, newest first – what the picker of its kind offers.
|
||||
* What the picker of its kind offers, newest first: the files of `$folder` and the ones that
|
||||
* belong to nobody, or with `$seesAll` every file there is. `owner` is the account a file
|
||||
* belongs to, null for nobody, '' for an account that was deleted; `deletable` says whether
|
||||
* the one asking may try (whether a project still uses it is only known when they do).
|
||||
*
|
||||
* @return list<array<string,string|int>>
|
||||
* @param array<string,string> $owners folder => account, see `UserStore::folderOwners`
|
||||
* @return list<array<string,string|int|bool|null>>
|
||||
*/
|
||||
public function list(): array
|
||||
public function list(string $folder, bool $seesAll, array $owners): array
|
||||
{
|
||||
$items = [];
|
||||
foreach (glob($this->dir . '/*.*') ?: [] as $path) {
|
||||
$file = basename($path);
|
||||
// Sidecars share the directory and are skipped: `{hash}.png.json` is not a valid name.
|
||||
if (!static::isValidName($file)) {
|
||||
$items = $this->listIn('', null, $seesAll);
|
||||
foreach (glob($this->dir . '/*', GLOB_ONLYDIR) ?: [] as $path) {
|
||||
$name = basename($path);
|
||||
if (!preg_match('/^' . UserStore::FOLDER_PATTERN . '$/', $name) || (!$seesAll && $name !== $folder)) {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
'file' => $file,
|
||||
'name' => OriginalName::read($this->dir, $file),
|
||||
...$this->details($path),
|
||||
'bytes' => (int) filesize($path),
|
||||
'updatedAt' => gmdate('c', (int) filemtime($path)),
|
||||
];
|
||||
array_push($items, ...$this->listIn($name, $owners[$name] ?? '', true));
|
||||
}
|
||||
usort($items, static fn(array $a, array $b): int => strcmp($b['updatedAt'], $a['updatedAt']));
|
||||
return $items;
|
||||
@@ -50,35 +69,41 @@ abstract class HashedFileStore
|
||||
|
||||
public function delete(string $file): bool
|
||||
{
|
||||
if (!static::isValidName($file)) {
|
||||
if (!$this->exists($file)) {
|
||||
return false;
|
||||
}
|
||||
$path = $this->dir . '/' . $file;
|
||||
if (!is_file($path) || !unlink($path)) {
|
||||
[$dir, $base] = $this->split($file);
|
||||
if (!unlink($dir . '/' . $base)) {
|
||||
return false;
|
||||
}
|
||||
OriginalName::delete($this->dir, $file);
|
||||
OriginalName::delete($dir, $base);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps bytes that passed the store's checks under their hash, with `$originalName` beside them
|
||||
* as a caption (see OriginalName), and returns the record. Bytes stored before are not written
|
||||
* again; a new name renames them.
|
||||
* Keeps bytes that passed the store's checks under their hash in `$folder`, with `$originalName`
|
||||
* beside them as a caption (see OriginalName), and returns the record. Bytes stored there before
|
||||
* are not written again; a new name renames them.
|
||||
*
|
||||
* @param array<string,int> $details what the record says between the name and the size
|
||||
* @return array<string,string|int>
|
||||
*/
|
||||
protected function keep(string $bytes, string $extension, string $originalName, array $details = []): array
|
||||
protected function keep(string $bytes, string $extension, string $originalName, string $folder, array $details = []): array
|
||||
{
|
||||
$file = hash('sha256', $bytes) . '.' . $extension;
|
||||
$path = $this->dir . '/' . $file;
|
||||
if (!is_file($path)) {
|
||||
AtomicFile::write($path, $bytes, AtomicFile::SERVED, 'stored file');
|
||||
if (!preg_match('/^' . UserStore::FOLDER_PATTERN . '$/', $folder)) {
|
||||
throw new InvalidArgumentException("Invalid folder: {$folder}");
|
||||
}
|
||||
OriginalName::write($this->dir, $file, $originalName);
|
||||
$dir = $this->dir . '/' . $folder;
|
||||
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
throw new RuntimeException("Cannot create directory {$dir}");
|
||||
}
|
||||
$base = hash('sha256', $bytes) . '.' . $extension;
|
||||
if (!is_file($dir . '/' . $base)) {
|
||||
AtomicFile::write($dir . '/' . $base, $bytes, AtomicFile::SERVED, 'stored file');
|
||||
}
|
||||
OriginalName::write($dir, $base, $originalName);
|
||||
|
||||
return ['file' => $file, 'name' => OriginalName::read($this->dir, $file), ...$details, 'bytes' => strlen($bytes)];
|
||||
return ['file' => "{$folder}/{$base}", 'name' => OriginalName::read($dir, $base), ...$details, 'bytes' => strlen($bytes)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,4 +116,38 @@ abstract class HashedFileStore
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @return list<array<string,string|int|bool|null>> */
|
||||
private function listIn(string $folder, ?string $owner, bool $deletable): array
|
||||
{
|
||||
$dir = $folder === '' ? $this->dir : $this->dir . '/' . $folder;
|
||||
$prefix = $folder === '' ? '' : $folder . '/';
|
||||
$items = [];
|
||||
foreach (glob($dir . '/*.*') ?: [] as $path) {
|
||||
$base = basename($path);
|
||||
// Sidecars share the directory and are skipped: `{hash}.png.json` is not a valid name.
|
||||
if (!static::isValidName($base)) {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
'file' => $prefix . $base,
|
||||
'name' => OriginalName::read($dir, $base),
|
||||
...$this->details($path),
|
||||
'bytes' => (int) filesize($path),
|
||||
'updatedAt' => gmdate('c', (int) filemtime($path)),
|
||||
'owner' => $owner,
|
||||
'deletable' => $deletable,
|
||||
];
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
/** @return array{0:string,1:string} the directory a stored name lies in, and its last part */
|
||||
private function split(string $file): array
|
||||
{
|
||||
$folder = self::folderOf($file);
|
||||
return $folder === ''
|
||||
? [$this->dir, $file]
|
||||
: [$this->dir . '/' . $folder, substr($file, strlen($folder) + 1)];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user