A share can now hold a private text (a password, a key, a short note) with its files or on its own. The upload page's new "Private text" card takes up to 100 KB; the browser encrypts the text and sends it through the same chunk pipeline as a file, flagged is_text on share_files, so it gets the share's password, expiry, download limit and cleanup. The recipient sees the text only after pressing "Show text", which counts as their download, so a messenger link preview cannot use up a share limited to one download. The text is left out of the file list, the ZIP and the file counts; the admin dashboard marks shares that hold one with "Text". The website gains a Private text feature card and a fifth phone screenshot; every screenshot is retaken. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
265 lines
9.0 KiB
PHP
265 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\Setting;
|
|
use App\Models\Share;
|
|
use App\Models\ShareFile;
|
|
use App\Services\PasswordGeneratorService;
|
|
use App\Services\ShareService;
|
|
use Carbon\CarbonInterval;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
/**
|
|
* The upload page. The browser encrypts each file chunk by chunk and sends the chunks to
|
|
* UploadChunkController (resources/js/share-uploader.js); this component registers the files
|
|
* into a pending share, lists them and completes the share with its options.
|
|
*/
|
|
#[Layout('layouts.app')]
|
|
class FileUploader extends Component
|
|
{
|
|
/** The pending share this page uploads into: created with the first file, one per page load. */
|
|
#[Locked]
|
|
public ?string $pendingToken = null;
|
|
|
|
public bool $usePassword = false;
|
|
|
|
public string $password = '';
|
|
|
|
public string $password_confirmation = '';
|
|
|
|
public string $expiration = '7d';
|
|
|
|
public ?int $maxDownloads = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->expiration = Setting::get('default_expiration', '7d') ?: '7d';
|
|
}
|
|
|
|
/**
|
|
* Register the files a visitor chose and hand the browser what it encrypts and sends them
|
|
* with. A file an admin limit refuses gets `null` in its place and the reason under `files`.
|
|
*
|
|
* @param array<int, array{name?: mixed, size?: mixed, path?: mixed}> $files
|
|
* @return array<int, array{id: int, url: string, key: string, noncePrefix: string, chunkSize: int, chunkCount: int}|null>
|
|
*/
|
|
public function registerFiles(array $files, ShareService $shareService): array
|
|
{
|
|
$this->resetErrorBag('files');
|
|
|
|
$targets = [];
|
|
|
|
foreach ($files as $file) {
|
|
try {
|
|
$shareFile = $shareService->registerFile(
|
|
$this->pendingShare(),
|
|
(string) ($file['name'] ?? ''),
|
|
(int) ($file['size'] ?? -1),
|
|
isset($file['path']) ? (string) $file['path'] : null,
|
|
);
|
|
} catch (ValidationException $e) {
|
|
if (! $this->getErrorBag()->has('files')) {
|
|
$this->addError('files', $e->errors()['files'][0]);
|
|
}
|
|
|
|
$targets[] = null;
|
|
|
|
continue;
|
|
}
|
|
|
|
$this->rememberPendingShare($shareFile->share);
|
|
|
|
$targets[] = $this->uploadTarget($shareFile, $shareService);
|
|
}
|
|
|
|
return $targets;
|
|
}
|
|
|
|
/**
|
|
* Register the private text as the share is created, in place of any text an earlier attempt
|
|
* registered, and hand the browser what it encrypts and sends it with. Only its size in UTF-8
|
|
* bytes reaches the server here: the text itself arrives encrypted, like a file. Nothing is
|
|
* registered for an empty text; a text an admin limit refuses gets `null` and the reason under
|
|
* `text`.
|
|
*
|
|
* @return array{id: int, url: string, key: string, noncePrefix: string, chunkSize: int, chunkCount: int}|null
|
|
*/
|
|
public function registerText(int $size, ShareService $shareService): ?array
|
|
{
|
|
$this->resetErrorBag('text');
|
|
|
|
$existingText = $this->pendingShare()?->textFile()->first();
|
|
|
|
if ($existingText !== null) {
|
|
$shareService->removeFile($existingText);
|
|
}
|
|
|
|
if ($size === 0) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$shareFile = $shareService->registerFile($this->pendingShare(), 'text.txt', $size, null, isText: true);
|
|
} catch (ValidationException $e) {
|
|
$this->addError('text', $e->errors()['files'][0]);
|
|
|
|
return null;
|
|
}
|
|
|
|
$this->rememberPendingShare($shareFile->share);
|
|
|
|
return $this->uploadTarget($shareFile, $shareService);
|
|
}
|
|
|
|
/**
|
|
* Take files out of the pending share, whether or not their upload finished.
|
|
*
|
|
* @param array<int, mixed> $fileIds
|
|
*/
|
|
public function removeFiles(array $fileIds, ShareService $shareService): void
|
|
{
|
|
$files = $this->pendingShare()?->files()->whereIn('id', array_map('intval', $fileIds))->get() ?? [];
|
|
|
|
foreach ($files as $file) {
|
|
$shareService->removeFile($file);
|
|
}
|
|
|
|
$this->resetErrorBag('files');
|
|
}
|
|
|
|
/**
|
|
* Fill in a generated password as protection is switched on, when the admin chose "Prefilled".
|
|
* A password already in the field stays.
|
|
*/
|
|
public function updatedUsePassword(bool $value): void
|
|
{
|
|
$passwordGenerator = app(PasswordGeneratorService::class);
|
|
|
|
if ($value && $this->password === '' && $passwordGenerator->mode() === 'prefill') {
|
|
$this->password = $passwordGenerator->generate();
|
|
}
|
|
}
|
|
|
|
public function generatePassword(PasswordGeneratorService $passwordGenerator): void
|
|
{
|
|
if ($passwordGenerator->mode() === 'off') {
|
|
return;
|
|
}
|
|
|
|
$this->password = $passwordGenerator->generate();
|
|
$this->resetErrorBag('password');
|
|
}
|
|
|
|
public function createShare(ShareService $shareService): void
|
|
{
|
|
$rules = [];
|
|
|
|
if (! Setting::get('allow_never_expire', false)) {
|
|
$rules['expiration'] = ['required', 'string', Rule::in(array_keys(Share::EXPIRATIONS))];
|
|
}
|
|
|
|
if ($this->usePassword) {
|
|
$rules['password'] = ['required', 'string', 'min:8'];
|
|
}
|
|
|
|
if ($rules !== []) {
|
|
$this->validate($rules, [
|
|
'expiration.required' => __('An expiration time is required.'),
|
|
]);
|
|
}
|
|
|
|
$pendingShare = $this->pendingShare();
|
|
|
|
if ($pendingShare === null) {
|
|
$this->addError('files', __('Add files or a text to share.'));
|
|
|
|
return;
|
|
}
|
|
|
|
$share = $shareService->completeShare($pendingShare, [
|
|
'password' => $this->usePassword ? $this->password : null,
|
|
'expires_at' => isset(Share::EXPIRATIONS[$this->expiration])
|
|
? now()->add(CarbonInterval::make(Share::EXPIRATIONS[$this->expiration]['interval']))
|
|
: null,
|
|
'max_downloads' => $this->maxDownloads ?: null,
|
|
]);
|
|
|
|
session()->put('pending_shares', array_values(array_diff(session('pending_shares', []), [$share->token])));
|
|
|
|
// The page the upload leads to offers the password once more, next to the link; it is
|
|
// never stored in the clear, so this flash is the only way it gets there.
|
|
if ($this->usePassword) {
|
|
session()->flash('share_password', [
|
|
'token' => $share->token,
|
|
'password' => Crypt::encryptString($this->password),
|
|
]);
|
|
}
|
|
|
|
$this->redirect(route('share.created', $share), navigate: true);
|
|
}
|
|
|
|
public function render(): mixed
|
|
{
|
|
$shareService = app(ShareService::class);
|
|
$pendingFiles = $this->pendingShare()?->files()->where('is_text', false)->orderBy('id')->get() ?? collect();
|
|
|
|
return view('livewire.file-uploader', [
|
|
'pendingFiles' => $pendingFiles,
|
|
'allFilesUploaded' => $pendingFiles->every(fn ($file): bool => $file->completed_at !== null),
|
|
'maxTextBytes' => ShareFile::MAX_TEXT_BYTES,
|
|
'isStorageFull' => $shareService->isStorageFull(),
|
|
'allowNeverExpire' => (bool) Setting::get('allow_never_expire', false),
|
|
'passwordGeneratorMode' => app(PasswordGeneratorService::class)->mode(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Make a share the one this page uploads into, and let this session reach it.
|
|
*/
|
|
private function rememberPendingShare(Share $share): void
|
|
{
|
|
if ($this->pendingToken !== $share->token) {
|
|
$this->pendingToken = $share->token;
|
|
session()->push('pending_shares', $this->pendingToken);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* What the browser encrypts and sends a registered file with.
|
|
*
|
|
* @return array{id: int, url: string, key: string, noncePrefix: string, chunkSize: int, chunkCount: int}
|
|
*/
|
|
private function uploadTarget(ShareFile $shareFile, ShareService $shareService): array
|
|
{
|
|
$header = $shareService->readHeader($shareFile);
|
|
|
|
return [
|
|
'id' => $shareFile->id,
|
|
'url' => Str::beforeLast(route('upload.chunk', ['shareFile' => $shareFile, 'index' => 0]), '/'),
|
|
'key' => $shareFile->share->encryption_key,
|
|
'noncePrefix' => bin2hex($header['noncePrefix']),
|
|
'chunkSize' => $header['chunkSize'],
|
|
'chunkCount' => $header['chunkCount'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* This page's pending share, while it is still pending and this session started it.
|
|
*/
|
|
private function pendingShare(): ?Share
|
|
{
|
|
if ($this->pendingToken === null || ! in_array($this->pendingToken, session('pending_shares', []), true)) {
|
|
return null;
|
|
}
|
|
|
|
return Share::query()->where('token', $this->pendingToken)->whereNull('completed_at')->first();
|
|
}
|
|
}
|