commit compras2 2811
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-28 15:45:52 +02:00
parent eb32c74c56
commit 060c119ba1
92 changed files with 4936 additions and 43 deletions
@@ -0,0 +1,72 @@
<?php
namespace App\Http\Controllers;
use App\Models\PrivateAuditExport;
use App\Services\PrivateAuditAccess;
use App\Services\PrivateAuditExporter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;
final class PrivateAuditController extends Controller
{
public function index(Request $request, PrivateAuditAccess $access): Response
{
$scope = $access->current($this->staffIdentity($request));
$request->validate(['page' => 'nullable|integer|min:1|max:1000000']);
$exports = PrivateAuditExport::query()->where('created_by', $scope['actor']->id)->where('work_context', $scope['context'])->latest('id')->paginate(20)
->through(fn (PrivateAuditExport $export) => ['id' => $export->id, 'families' => $export->families,
'created_at' => $export->created_at, 'size' => $export->size, 'sha256' => $export->sha256,
'download_url' => '/auditoria/privada/'.$export->id.'/download']);
Inertia::encryptHistory();
$response = Inertia::render('audit/private', ['families' => collect($scope['granted'])->map(fn (string $key) => ['key' => $key, 'label' => PrivateAuditAccess::FAMILIES[$key]['label']])->values(), 'exports' => $exports])->toResponse($request);
$response->headers->set('Cache-Control', 'private, no-store, max-age=0');
return $response;
}
public function store(Request $request, PrivateAuditExporter $service): Response
{
$service->generate($this->staffIdentity($request), $request->all());
return redirect('/auditoria/privada')->with('success', 'Pacote privado preparado e verificado.');
}
public function download(Request $request, PrivateAuditExport $export, PrivateAuditExporter $service): Response
{
$actor = $this->staffIdentity($request);
$service->verifiedPath($actor, $export);
// Snapshot the verified bytes before any response: a later file change cannot alter the download.
$copy = tmpfile();
abort_unless(is_resource($copy), 503);
try {
$source = Storage::disk('local')->readStream($export->path);
abort_unless(is_resource($source), 409);
try {
$size = stream_copy_to_stream($source, $copy);
} finally {
fclose($source);
}
rewind($copy);
$hash = hash_init('sha256');
hash_update_stream($hash, $copy);
abort_unless($size === $export->size && hash_equals($export->sha256, hash_final($hash)), 409);
$service->verifiedPath($actor, $export);
rewind($copy);
return response()->streamDownload(function () use ($copy): void {
try {
fpassthru($copy);
} finally {
fclose($copy);
}
}, 'auditoria-'.$export->id.'.zip', ['Content-Type' => 'application/zip', 'Content-Length' => (string) $export->size,
'Cache-Control' => 'private, no-store, max-age=0', 'X-Content-Type-Options' => 'nosniff']);
} catch (\Throwable $error) {
fclose($copy);
throw $error;
}
}
}