Laravel 13 + Livewire/Volt app for managing client organizations, documents, passwords, flexible assets, virtual machines, contacts, domains, todos, shortcuts, and role-based user administration.
73 lines
2.0 KiB
PHP
73 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\OnlyOffice;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\User;
|
|
use Firebase\JWT\JWT;
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
class DocumentConfigBuilder
|
|
{
|
|
public function build(Document $document, User $user, bool $editable = true): array
|
|
{
|
|
$config = [
|
|
'document' => [
|
|
'fileType' => $document->office_extension,
|
|
'key' => $document->office_key,
|
|
'title' => $document->name.'.'.$document->office_extension,
|
|
'url' => $this->downloadUrl($document),
|
|
],
|
|
'documentType' => $this->documentType($document->office_extension),
|
|
'editorConfig' => [
|
|
'mode' => $editable ? 'edit' : 'view',
|
|
'callbackUrl' => $this->callbackUrl($document),
|
|
'user' => [
|
|
'id' => (string) $user->id,
|
|
'name' => $user->name,
|
|
],
|
|
'lang' => 'fr',
|
|
],
|
|
];
|
|
|
|
$secret = config('onlyoffice.jwt_secret');
|
|
|
|
if (filled($secret)) {
|
|
$config['token'] = JWT::encode($config, $secret, 'HS256');
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
protected function downloadUrl(Document $document): string
|
|
{
|
|
$signed = URL::temporarySignedRoute(
|
|
'onlyoffice.download',
|
|
now()->addMinutes(5),
|
|
['document' => $document->id],
|
|
absolute: false
|
|
);
|
|
|
|
return $this->baseUrl().$signed;
|
|
}
|
|
|
|
protected function callbackUrl(Document $document): string
|
|
{
|
|
return $this->baseUrl().route('onlyoffice.callback', $document, absolute: false);
|
|
}
|
|
|
|
protected function baseUrl(): string
|
|
{
|
|
return rtrim(config('onlyoffice.callback_base_url'), '/');
|
|
}
|
|
|
|
protected function documentType(?string $extension): string
|
|
{
|
|
return match ($extension) {
|
|
'xlsx' => 'cell',
|
|
'pptx' => 'slide',
|
|
default => 'word',
|
|
};
|
|
}
|
|
}
|