Skip to content

Commit

Permalink
feat(php-laravel): refresh file config
Browse files Browse the repository at this point in the history
  • Loading branch information
sshakndr committed Dec 16, 2024
1 parent b68b543 commit 56ed3bd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Change Log

- php-laravel: refresh config
- php: refresh config
- ruby: refresh config
- python: refresh config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public function index(Request $request, JWT $jwt)

if ($user['id'] != 'uid-0') {
$historyLayout .= "// add mentions for not anonymous users
config.events['onRequestRefreshFile'] = onRequestRefreshFile;
config.events['onRequestClose'] = onRequestClose;
config.events['onRequestUsers'] = onRequestUsers;
config.events['onRequestSaveAs'] = onRequestSaveAs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

use App\Helpers\Path\Path;
use App\Helpers\Path\PathInfo;
use App\Helpers\URL\FileURL;
use App\Helpers\URL\URL;
use App\Repositories\FormatRepository;
use App\Services\JWT;
use App\Services\ServerConfig;
use App\Services\StorageConfig;
use App\UseCases\Common\Http\DownloadFileCommand;
Expand All @@ -41,6 +43,8 @@
use App\UseCases\Document\Find\FindDocumentHistoryQueryHandler;
use App\UseCases\Document\Find\FindDocumentQuery;
use App\UseCases\Document\Find\FindDocumentQueryHandler;
use App\UseCases\File\Find\FileExistsQuery;
use App\UseCases\File\Find\FileExistsQueryHandler;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -311,4 +315,62 @@ public function destroy(Request $request)

return response(status: 201);
}

public function config(Request $request, JWT $jwt)
{
try {
$request->validate([
'fileName' => 'string',
'directUrl' => 'nullable|string',
'permissions' => 'nullable|string',
]);

$fileName = $request->fileName;
$directUrl = $request->directUrl == 'true';

$fileExists = app(FileExistsQueryHandler::class)
->__invoke(new FileExistsQuery($fileName, $request->ip()));

if (! $fileExists) {
throw new Exception('File not found: '.$fileName);
}

$file = app(FindDocumentQueryHandler::class)
->__invoke(new FindDocumentQuery($fileName, $request->ip()));
$url = FileURL::download($fileName, $request->ip());

$config = [
'document' => [
'title' => $fileName,
'key' => $file['key'],
'url' => $url,
'directUrl' => $directUrl ? $url : null,
'permissions' => json_decode($request->permissions),
'referenceData' => [
'fileKey' => json_encode([
'fileName' => $fileName,
'userAddress' => $request->ip(),
]),
'instanceId' => $request->serverAddress,
],
],
'editorConfig' => [
'mode' => 'edit',
'callbackUrl' => FileURL::callback($fileName, $request->ip()),
],
];

if ($this->serverConfig->get('jwt.enabled')) {
$config['token'] = $jwt->encode($config);
}

return response()
->json($config);
} catch (Exception $e) {
return response()
->json([
'error' => $e->getMessage(),
], 500);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function __construct()
"Can't view chat",
'View file without collaboration',
'Can’t submit forms',
'Can’t refresh outdated file',
];
$this->users = [
new User(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@
});
};
var onRequestRefreshFile = function(event) {
let xhr = new XMLHttpRequest();
xhr.open("GET", "files/config?fileName=" + encodeURIComponent(config.document.title) +
"&directUrl=" + !!config.document.directUrl +
"&permissions=" + encodeURIComponent(JSON.stringify(config.document.permissions)));
xhr.send();
xhr.onload = function () {
innerAlert(xhr.responseText);
docEditor.refreshFile(JSON.parse(xhr.responseText));
};
};
var onRequestReferenceData = function(event) { // user refresh external data source
innerAlert("onRequestReferenceData");
Expand Down
1 change: 1 addition & 0 deletions web/documentserver-example/php-laravel/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Route::post('/saveas', [FileController::class, 'saveAs'])->name('saveas');
Route::get('/history', [FileController::class, 'history'])->name('history');
Route::post('/rename', [FileController::class, 'rename'])->name('rename');
Route::get('/config', [FileController::class, 'config'])->name('config');

Route::middleware(EnsureJWTTokenIsPresent::class)->group(function () {
Route::get('/download', [FileController::class, 'download'])->name('download');
Expand Down

0 comments on commit 56ed3bd

Please sign in to comment.