mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-17 17:15:08 +00:00
6e47d76ba6
Client file sharing, rebuilt from the ground up: a private area per client, resumable uploads, folders, groups and categories, sharing with expiry dates and download limits, comments, file versions, an activity log, a REST API, and sixteen languages. This repository begins here. ProjectSend 2 was developed privately, and that development history is not published — the previous generation remains available, with its own history, at projectsend/legacy. Free software under the GNU General Public License v2, or (at your option) any later version.
174 lines
6.2 KiB
PHP
174 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Files;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Files\Models\File;
|
|
use App\Modules\Files\Uploads\UploadExtensionPolicy;
|
|
use App\Modules\Platform\Settings\ExternalStorageConfigApplier;
|
|
use App\Modules\Platform\Settings\ExternalStorageSettings;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Finds files sitting on disk with no corresponding File row — v1
|
|
* parity for the import_orphans permission. Scans the local 'files'
|
|
* disk, and 'files_external' too once external storage is active
|
|
* (ExternalStorageConfigApplier::isActive()) — an upload can land on
|
|
* either, so an orphan can too. See docs/unused-permissions-audit.md.
|
|
*/
|
|
class OrphanFileScanner
|
|
{
|
|
// Derived artifacts written by FileThumbnailController and
|
|
// BuildZipDownloadJob respectively — never orphaned uploads, so
|
|
// never candidates regardless of what's in the files table.
|
|
// Thumbnails are always local; zips would be too if that job ever
|
|
// ran against 'files_external', so the exclusion applies per-disk.
|
|
private const EXCLUDED_PREFIXES = ['thumbnails/', 'zips/'];
|
|
|
|
public function __construct(
|
|
private readonly UploadExtensionPolicy $extensionPolicy,
|
|
private readonly ExternalStorageConfigApplier $externalStorage,
|
|
) {}
|
|
|
|
/**
|
|
* Every disk this install could possibly have orphans on right now,
|
|
* keyed by disk name, with a human-readable label and location for
|
|
* display — so the page can say exactly where it looked.
|
|
*
|
|
* @return array<string, array{label: string, location: string}>
|
|
*/
|
|
public function scannedDisks(): array
|
|
{
|
|
$disks = [
|
|
'files' => [
|
|
'label' => 'Local storage',
|
|
'location' => rtrim(Storage::disk('files')->path(''), '/'),
|
|
],
|
|
];
|
|
|
|
if ($this->externalStorage->isActive()) {
|
|
$settings = ExternalStorageSettings::current();
|
|
$root = $settings->root !== null && $settings->root !== '' ? '/'.trim($settings->root, '/') : '';
|
|
|
|
$disks['files_external'] = [
|
|
'label' => 'External storage',
|
|
'location' => 's3://'.$settings->bucket.$root,
|
|
];
|
|
}
|
|
|
|
return $disks;
|
|
}
|
|
|
|
/**
|
|
* Sorted by disk then path so pagination over the result is stable
|
|
* across requests — an install can have thousands of these, so the
|
|
* caller paginates rather than rendering the whole list at once.
|
|
*
|
|
* $viewer is optional — null for callers with no session user (e.g. a
|
|
* scheduled command), in which case 'allowed' is unused by the caller
|
|
* and just reported as true rather than requiring a real user to
|
|
* evaluate UploadExtensionPolicy against.
|
|
*
|
|
* @return list<array{disk: string, path: string, size: int, last_modified: int, allowed: bool}>
|
|
*/
|
|
public function scan(?User $viewer = null, ?string $search = null): array
|
|
{
|
|
$needle = $search !== null && $search !== '' ? mb_strtolower($search) : null;
|
|
$orphans = [];
|
|
|
|
foreach (array_keys($this->scannedDisks()) as $diskName) {
|
|
$disk = Storage::disk($diskName);
|
|
$knownPaths = array_flip($this->knownPaths($diskName));
|
|
|
|
foreach ($disk->allFiles() as $path) {
|
|
if ($this->isExcluded($path) || isset($knownPaths[$path])) {
|
|
continue;
|
|
}
|
|
|
|
if ($needle !== null && ! str_contains(mb_strtolower($path), $needle)) {
|
|
continue;
|
|
}
|
|
|
|
$orphans[] = [
|
|
'disk' => $diskName,
|
|
'path' => $path,
|
|
'size' => $disk->size($path),
|
|
'last_modified' => $disk->lastModified($path),
|
|
'allowed' => $viewer !== null ? $this->extensionPolicy->isAllowed($viewer, basename($path)) : true,
|
|
];
|
|
}
|
|
}
|
|
|
|
usort($orphans, fn (array $a, array $b): int => [$a['disk'], $a['path']] <=> [$b['disk'], $b['path']]);
|
|
|
|
return $orphans;
|
|
}
|
|
|
|
/**
|
|
* Re-validated at import/delete time — never trust a client-supplied
|
|
* disk/path just because it was in an earlier scan response. $disk
|
|
* must be one of scannedDisks()'s current keys — a client could
|
|
* otherwise name any configured Laravel disk (e.g. 'public').
|
|
*/
|
|
public function isOrphan(string $disk, string $path): bool
|
|
{
|
|
if (! array_key_exists($disk, $this->scannedDisks())) {
|
|
return false;
|
|
}
|
|
|
|
if ($this->isExcluded($path) || ! Storage::disk($disk)->exists($path)) {
|
|
return false;
|
|
}
|
|
|
|
return ! in_array($path, $this->knownPaths($disk), true);
|
|
}
|
|
|
|
public function isAllowedFor(User $viewer, string $path): bool
|
|
{
|
|
return $this->extensionPolicy->isAllowed($viewer, basename($path));
|
|
}
|
|
|
|
/**
|
|
* A 0-byte file is virtually certain to be a failed or interrupted
|
|
* write rather than real content — importing it would only give a
|
|
* client an empty download, so unlike a merely restricted-extension
|
|
* orphan (still worth adopting once permitted), an empty one is never
|
|
* importable. Still listed and still deletable, same as a restricted
|
|
* one.
|
|
*/
|
|
public function isImportable(User $viewer, string $disk, string $path): bool
|
|
{
|
|
return $this->isOrphan($disk, $path)
|
|
&& $this->isAllowedFor($viewer, $path)
|
|
&& Storage::disk($disk)->size($path) > 0;
|
|
}
|
|
|
|
/**
|
|
* Soft-deleted rows still count as "known" — a path stays claimed as
|
|
* long as any row references it, even one only awaiting the erasure
|
|
* grace period, so a scan never offers to double-adopt it.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
private function knownPaths(string $disk): array
|
|
{
|
|
return array_values(array_filter(
|
|
File::withTrashed()->where('disk', $disk)->pluck('path')->all(),
|
|
fn (mixed $path): bool => is_string($path),
|
|
));
|
|
}
|
|
|
|
private function isExcluded(string $path): bool
|
|
{
|
|
foreach (self::EXCLUDED_PREFIXES as $prefix) {
|
|
if (str_starts_with($path, $prefix)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|