feat(php): refresh file config

This commit is contained in:
sshakndr
2024-12-13 16:00:18 +07:00
parent 31649e7203
commit b68b543605
6 changed files with 66 additions and 1 deletions

View File

@ -171,6 +171,11 @@ function routers()
echo json_encode($response);
return;
}
if (str_starts_with($path, '/config')) {
$response = config();
echo json_encode($response);
return;
}
http_response_code(HTTPStatus::NotFound->value);
}

View File

@ -707,3 +707,48 @@ function formats()
];
}
}
function config()
{
try {
$fileName = $_GET["fileName"];
$directUrl = $_GET["directUrl"] == "true";
$permissions = $_GET["permissions"];
if (!file_exists(getStoragePath($fileName))) {
throw new Exception("File not found ".$fileName);
}
$config = [
"document" => [
"title" => $fileName,
"key" => getDocEditorKey($fileName),
"url" => getDownloadUrl($fileName),
"directUrl" => $directUrl ? getDownloadUrl($fileName, false) : null,
"permissions" => json_decode($permissions),
"referenceData" => [
"fileKey" => json_encode([
"fileName" => $fileName,
"userAddress" => getCurUserHostAddress()
]),
"instanceId" => serverPath(),
]
],
"editorConfig" => [
"mode" => "edit",
"callbackUrl" => getCallbackUrl($fileName)
]
];
$jwtManager = new JwtManager();
if ($jwtManager->isJwtEnabled()) {
$config["token"] = $jwtManager->jwtEncode($config);
}
return $config;
} catch (Exception $error) {
return [
'error' => $error->getMessage()
];
}
}

View File

@ -78,7 +78,8 @@ final class ExampleUsers
"Can't rename files from the editor",
"Can't view chat",
"View file without collaboration",
"Cant submit forms"
"Cant submit forms",
"Cant refresh outdated file"
];
$this->users = [
new Users(

View File

@ -254,6 +254,7 @@ final class DocEditorView extends View
if ($user->id != "uid-0") {
$historyLayout .= "// add mentions for not anonymous users
config.events['onRequestRefreshFile'] = onRequestRefreshFile;
config.events['onRequestUsers'] = onRequestUsers;
config.events['onRequestSaveAs'] = onRequestSaveAs;
// the user is mentioned in a comment

View File

@ -116,6 +116,18 @@
});
};
var onRequestRefreshFile = function(event) {
let xhr = new XMLHttpRequest();
xhr.open("GET", "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");