100 lines
5.1 KiB
PHP
100 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Supplier;
|
|
use App\Models\SupplierDocument;
|
|
use App\Models\User;
|
|
use App\Services\ConflictGuard;
|
|
use App\Services\CurrentActor;
|
|
use App\Services\RequisitionWorkflow;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
use Throwable;
|
|
|
|
class SupplierDocumentController extends Controller
|
|
{
|
|
public function store(Request $request, Supplier $supplier, RequisitionWorkflow $workflow): RedirectResponse
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User, 403);
|
|
Gate::authorize('update', $supplier);
|
|
$data = $request->validate([
|
|
'document_type' => ['required', Rule::in(['license', 'tax', 'conflict', 'other'])],
|
|
'file' => ['required', 'file', 'mimes:pdf,jpg,jpeg,png', 'extensions:pdf,jpg,jpeg,png', 'min:1', 'max:2048'],
|
|
]);
|
|
$file = $request->file('file');
|
|
abort_unless($file instanceof UploadedFile, 422);
|
|
$path = $file->store($supplier->tenant_id.'/'.$supplier->id, 'supplier_documents');
|
|
abort_if($path === false, 500);
|
|
try {
|
|
DB::transaction(function () use ($actor, $supplier, $workflow, $data, $file, $path) {
|
|
app(ConflictGuard::class)->lockCompany($actor);
|
|
$actor = app(CurrentActor::class)->lock($actor);
|
|
$supplier = Supplier::query()->lockForUpdate()->findOrFail($supplier->id);
|
|
Gate::forUser($actor)->authorize('update', $supplier);
|
|
$before = $supplier->toArray();
|
|
$name = Str::substr(Str::replaceMatches('/[^\pL\pN ._()-]/u', '_', basename(str_replace('\\', '/', $file->getClientOriginalName()))), 0, 180);
|
|
$document = $supplier->documents()->create([
|
|
'tenant_id' => $supplier->tenant_id, 'uploaded_by' => $actor->id,
|
|
'document_type' => $data['document_type'], 'original_name' => $name,
|
|
'path' => $path, 'mime_type' => $file->getMimeType(), 'size' => $file->getSize(),
|
|
'sha256' => hash_file('sha256', $file->getPathname()),
|
|
]);
|
|
$supplier->update(['status' => 'pending', 'reviewed_by' => null, 'review_comment' => null, 'last_changed_by' => $actor->id, 'version' => $supplier->version + 1]);
|
|
$workflow->audit($actor, 'supplier.document_uploaded', null, $before, ['supplier' => $supplier->toArray(), 'document' => $document->toArray()]);
|
|
});
|
|
} catch (Throwable $exception) {
|
|
Storage::disk('supplier_documents')->delete($path);
|
|
throw $exception;
|
|
}
|
|
|
|
return back();
|
|
}
|
|
|
|
public function download(Request $request, Supplier $supplier, SupplierDocument $document, RequisitionWorkflow $workflow): StreamedResponse
|
|
{
|
|
$actor = $request->user('web');
|
|
abort_unless($actor instanceof User, 403);
|
|
$actor = User::query()->where('tenant_id', $actor->tenant_id)->findOrFail($actor->id);
|
|
Gate::forUser($actor)->authorize('viewDocuments', $supplier);
|
|
abort_unless($document->supplier_id === $supplier->id, 404);
|
|
$prefix = $supplier->tenant_id.'/'.$supplier->id.'/';
|
|
abort_unless(str_starts_with($document->path, $prefix)
|
|
&& preg_match('/\A[A-Za-z0-9_-]+\.[A-Za-z0-9]+\z/D', substr($document->path, strlen($prefix))), 409,
|
|
'Documento indisponível para descarga. Solicite conferência ao responsável.');
|
|
$disk = Storage::disk('supplier_documents');
|
|
abort_unless($disk->exists($document->path), 404);
|
|
$stream = $disk->readStream($document->path);
|
|
abort_unless(is_resource($stream), 404);
|
|
try {
|
|
// Capture at most the accepted upload size plus one byte. The response uses
|
|
// these verified bytes even if the underlying file subsequently changes.
|
|
$content = stream_get_contents($stream, 2097153);
|
|
} finally {
|
|
fclose($stream);
|
|
}
|
|
abort_unless(is_string($content) && strlen($content) > 0 && strlen($content) <= 2097152
|
|
&& strlen($content) === (int) $document->size
|
|
&& hash_equals($document->sha256, hash('sha256', $content)), 409,
|
|
'Documento indisponível para descarga. Solicite conferência ao responsável.');
|
|
$workflow->audit($actor, 'supplier.document_downloaded', null, null, ['supplier_id' => $supplier->id, 'document_id' => $document->id, 'name' => $document->original_name]);
|
|
|
|
return response()->streamDownload(static function () use ($content): void {
|
|
echo $content;
|
|
}, $document->original_name, [
|
|
'Content-Type' => 'application/octet-stream',
|
|
'X-Content-Type-Options' => 'nosniff',
|
|
'Cache-Control' => 'private, no-store',
|
|
'Content-Security-Policy' => "default-src 'none'; sandbox",
|
|
]);
|
|
}
|
|
}
|