$data * @param list $files */ public function create(User $user, array $data, array $files): Transfer { if ($files === []) { throw new RuntimeException('Add at least one file to share.'); } $maxFiles = (int) config('transfer.max_files_per_transfer', 20); if (count($files) > $maxFiles) { throw new RuntimeException("You can upload up to {$maxFiles} files per transfer."); } $maxBytes = (int) config('transfer.max_file_bytes', 524288000); $retentionDays = (int) ($data['retention_days'] ?? config('transfer.default_retention_days', 30)); $retentionDays = max(1, min(365, $retentionDays)); return DB::transaction(function () use ($user, $data, $files, $maxBytes, $retentionDays) { $passwordHash = null; if (! empty($data['password'])) { $passwordHash = Hash::make((string) $data['password']); } $transfer = Transfer::create([ 'user_id' => $user->id, 'title' => trim((string) $data['title']), 'message' => isset($data['message']) ? trim((string) $data['message']) : null, 'password_hash' => $passwordHash, 'retention_days' => $retentionDays, 'expires_at' => now()->addDays($retentionDays), 'status' => Transfer::STATUS_ACTIVE, ]); $totalBytes = 0; foreach ($files as $file) { if (! $file instanceof UploadedFile) { continue; } if ($file->getSize() > $maxBytes) { throw new RuntimeException($file->getClientOriginalName().' exceeds the maximum file size.'); } $stored = $this->storeFile($user, $transfer, $file); $totalBytes += $stored->size_bytes; } if ($totalBytes === 0) { throw new RuntimeException('No valid files were uploaded.'); } $qrCode = $this->createQrCode($user, $transfer, $data); $transfer->update([ 'qr_code_id' => $qrCode->id, 'storage_bytes' => $totalBytes, ]); return $transfer->fresh(['files', 'qrCode']); }); } public function recordDownload(Transfer $transfer, ?TransferFile $file, Request $request): void { TransferDownloadEvent::create([ 'transfer_id' => $transfer->id, 'transfer_file_id' => $file?->id, 'downloaded_at' => now(), 'ip_hash' => $request->ip() ? hash('sha256', $request->ip()) : null, 'user_agent' => Str::limit((string) $request->userAgent(), 500, ''), ]); $transfer->increment('downloads_total'); if ($file) { $file->increment('downloads_total'); } } public function verifyPassword(Transfer $transfer, ?string $password): bool { if (! $transfer->isPasswordProtected()) { return true; } return $password !== null && Hash::check($password, $transfer->password_hash); } private function storeFile(User $user, Transfer $transfer, UploadedFile $file): TransferFile { $uuid = Str::uuid()->toString(); $ext = $file->getClientOriginalExtension() ?: 'bin'; $path = $user->id.'/transfers/'.$transfer->id.'/'.$uuid.'.'.$ext; $file->storeAs('', $path, 'qr'); return TransferFile::create([ 'transfer_id' => $transfer->id, 'original_name' => $file->getClientOriginalName() ?: 'file.'.$ext, 'disk' => 'qr', 'path' => $path, 'mime_type' => $file->getMimeType() ?: 'application/octet-stream', 'size_bytes' => (int) $file->getSize(), ]); } /** * @param array $data */ private function createQrCode(User $user, Transfer $transfer, array $data): QrCode { $shortCode = $this->generateUniqueShortCode(); $style = QrStyleDefaults::merge($data['style'] ?? null); $qrCode = QrCode::create([ 'user_id' => $user->id, 'short_code' => $shortCode, 'type' => QrCode::TYPE_TRANSFER, 'label' => $transfer->title, 'payload' => [ 'content' => [ 'transfer_id' => $transfer->id, 'message' => $transfer->message, ], 'style' => $style, ], 'is_active' => true, 'destination_updated_at' => now(), ]); $this->imageGenerator->generateAndStore($qrCode); return $qrCode; } private function generateUniqueShortCode(): string { do { $code = Str::lower(Str::random(8)); } while (QrCode::query()->where('short_code', $code)->exists()); return $code; } public function delete(Transfer $transfer): void { DB::transaction(function () use ($transfer) { foreach ($transfer->files as $file) { Storage::disk($file->disk)->delete($file->path); } if ($transfer->qrCode) { if ($transfer->qrCode->png_path) { Storage::disk('qr')->delete($transfer->qrCode->png_path); } if ($transfer->qrCode->svg_path) { Storage::disk('qr')->delete($transfer->qrCode->svg_path); } $transfer->qrCode->update(['is_active' => false]); } $transfer->update(['status' => Transfer::STATUS_DELETED]); }); } }