diff --git a/controllers/excalidraw.php b/controllers/excalidraw.php index f6758d96746a2883cfe9b9fc46c202ebcd25085a..0b18ab2c97fece97648e3d01c30ae1c9db76ee1e 100755 --- a/controllers/excalidraw.php +++ b/controllers/excalidraw.php @@ -11,18 +11,34 @@ class ExcalidrawController extends PluginController // The base of the URL is statically configured when building the app. // But we are building a plugin for Stud.IP which will be built once and run at many // different universities. So the question is, what can we do? - // We must provide a base URL, so we provide the base URL '.' (the path of the current page). + // We provide CRA with the base URL '.', causing our app to use relative paths for resources. + // Then, we use before_filter to redirect the requests it makes to the correct URLs. + // There are two cases to handle: + + // Case 1: The action which displays Excalidraw has been requested with an argument. // This causes our app to make requests for resources which look like this: // https://<studip-base-url>/plugins.php/excalidrawplugin/<current controller>/<current action>/static/media/<filename>.excalidrawlib - // We want them to look like this: + + // Case 2: The action which displays Excalidraw has been requested without an argument. + // The requests our app makes will look slightly different: + // https://<studip-base-url>/plugins.php/excalidrawplugin/<current controller>/static/media/<filename>.excalidrawlib + + // In both cases, we want to redirect the requests to the following URL: // https://<studip-base-url>/plugins_packages/UOL/ExcalidrawPlugin/excalidraw-selfhosted/build/static/media/<filename>.excalidrawlib - // We provide a 302 redirect to the appropriate url. - if (count($args) === 3 && $args[0] === 'static' && $args[1] === 'media' && str_ends_with( - $_SERVER['REQUEST_URI'], - 'excalidrawlib' - )) { - $url = $this->plugin->getPluginUrl( - ) . '/excalidraw-selfhosted/build/static/media/' . $args[2] . '.excalidrawlib'; + + $isCase1 = count($args) === 3 && $args[0] === 'static' && $args[1] === 'media' && + str_ends_with($_SERVER['REQUEST_URI'], 'excalidrawlib'); + $isCase2 = $action === 'static' && count($args) === 2 && $args[0] === 'media' && + str_ends_with($_SERVER['REQUEST_URI'], 'excalidrawlib'); + if ($isCase1) { + $url = $this->plugin->getPluginUrl() . '/excalidraw-selfhosted/build/static/media/' . + $args[2] . '.excalidrawlib'; + $this->redirect($url); + return; + } + if ($isCase2) { + $url = $this->plugin->getPluginUrl() . '/excalidraw-selfhosted/build/static/media/' . + $args[1] . '.excalidrawlib'; $this->redirect($url); return; }