changeset 399:2beac4eb0722

add standalone build for the viewer plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Nov 2025 18:25:39 +0100
parents b0f4843da0fa
children 1df8095649df
files Resources/CMake/JavaScriptLibraries.cmake ViewerPlugin/CMakeLists.txt ViewerPlugin/Plugin.cpp
diffstat 3 files changed, 69 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Resources/CMake/JavaScriptLibraries.cmake	Tue Nov 04 18:04:57 2025 +0100
+++ b/Resources/CMake/JavaScriptLibraries.cmake	Tue Nov 04 18:25:39 2025 +0100
@@ -26,11 +26,14 @@
   "${BASE_URL}/WSI/openlayers-10.6.1-package.tar.gz"
   "openlayers-10.6.1-package")
 
+
 set(JAVASCRIPT_LIBS_DIR  ${CMAKE_CURRENT_BINARY_DIR}/javascript-libs)
 file(MAKE_DIRECTORY ${JAVASCRIPT_LIBS_DIR})
 
+
 file(COPY
   ${CMAKE_CURRENT_BINARY_DIR}/openlayers-10.6.1-package/dist/ol.js
+  ${CMAKE_CURRENT_BINARY_DIR}/openlayers-10.6.1-package/dist/ol.js.map
   DESTINATION
   ${JAVASCRIPT_LIBS_DIR}/js
   )
--- a/ViewerPlugin/CMakeLists.txt	Tue Nov 04 18:04:57 2025 +0100
+++ b/ViewerPlugin/CMakeLists.txt	Tue Nov 04 18:25:39 2025 +0100
@@ -32,6 +32,7 @@
 # Generic parameters
 SET(STATIC_BUILD OFF CACHE BOOL "Static build of the third-party libraries (necessary for Windows)")
 SET(ALLOW_DOWNLOADS OFF CACHE BOOL "Allow CMake to download packages")
+SET(STANDALONE_BUILD ON CACHE BOOL "Standalone build (all the resources are embedded, necessary for releases)")
 
 # Advanced parameters to fine-tune linking against system libraries
 SET(USE_SYSTEM_OPENJPEG ON CACHE BOOL "Use the system version of OpenJpeg")
@@ -146,11 +147,23 @@
 
 include(${CMAKE_SOURCE_DIR}/../Resources/CMake/JavaScriptLibraries.cmake)
 
+if (STANDALONE_BUILD)
+  add_definitions(-DORTHANC_STANDALONE=1)
+  set(VIEWER_RESOURCES
+    VIEWER_HTML          ${CMAKE_SOURCE_DIR}/viewer.html
+    VIEWER_JS            ${CMAKE_SOURCE_DIR}/viewer.js
+    )
+else()
+  add_definitions(
+    -DORTHANC_STANDALONE=0
+    -DPLUGIN_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
+    )
+endif()
+
 EmbedResources(
+  ${VIEWER_RESOURCES}
   JAVASCRIPT_LIBS      ${JAVASCRIPT_LIBS_DIR}
   ORTHANC_EXPLORER     ${CMAKE_SOURCE_DIR}/OrthancExplorer.js
-  VIEWER_HTML          ${CMAKE_SOURCE_DIR}/viewer.html
-  VIEWER_JS            ${CMAKE_SOURCE_DIR}/viewer.js
   MIRADOR_HTML         ${CMAKE_SOURCE_DIR}/mirador.html
   OPEN_SEADRAGON_HTML  ${CMAKE_SOURCE_DIR}/openseadragon.html
   )
--- a/ViewerPlugin/Plugin.cpp	Tue Nov 04 18:04:57 2025 +0100
+++ b/ViewerPlugin/Plugin.cpp	Tue Nov 04 18:25:39 2025 +0100
@@ -392,9 +392,9 @@
 }
 
 
-void ServeFile(OrthancPluginRestOutput* output,
-               const char* url,
-               const OrthancPluginHttpRequest* request)
+void ServeEmbeddedFile(OrthancPluginRestOutput* output,
+                       const char* url,
+                       const OrthancPluginHttpRequest* request)
 {
   Orthanc::EmbeddedResources::FileResourceId resource;
 
@@ -403,12 +403,22 @@
 
   if (f == "viewer.html")
   {
+#if ORTHANC_STANDALONE == 0
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+#else
     resource = Orthanc::EmbeddedResources::VIEWER_HTML;
+#endif
+
     mime = "text/html";
   }
   else if (f == "viewer.js")
   {
+#if ORTHANC_STANDALONE == 0
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+#else
     resource = Orthanc::EmbeddedResources::VIEWER_JS;
+#endif
+
     mime = "application/javascript";
   }
   else if (f == "mirador.html")
@@ -433,6 +443,33 @@
 }
 
 
+#if ORTHANC_STANDALONE == 0
+void ServeSourceFile(OrthancPluginRestOutput* output,
+                     const char* url,
+                     const OrthancPluginHttpRequest* request)
+{
+  // This method should only be used during the development to speed up compilation
+
+  std::string filename(request->groups[0]);
+
+  if (filename != "viewer.html" &&
+      filename != "viewer.js")
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
+  }
+
+  const boost::filesystem::path path = Orthanc::SystemToolbox::InterpretRelativePath(
+    Orthanc::SystemToolbox::PathFromUtf8(PLUGIN_SOURCE_DIR), filename);
+  const char* mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(filename));
+
+  std::string content;
+  Orthanc::SystemToolbox::ReadFile(content, path);
+
+  OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, content.c_str(), content.size(), mime);
+}
+#endif
+
+
 extern "C"
 {
   ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
@@ -538,8 +575,15 @@
     OrthancPluginRegisterOnChangeCallback(OrthancPlugins::GetGlobalContext(), OnChangeCallback);
 
     OrthancPlugins::RegisterRestCallback<ServeJavaScriptLibraries>("/wsi/libs/(.*)", true);
-    OrthancPlugins::RegisterRestCallback<ServeFile>("/wsi/app/(viewer.html)", true);
-    OrthancPlugins::RegisterRestCallback<ServeFile>("/wsi/app/(viewer.js)", true);
+
+#if ORTHANC_STANDALONE == 1
+    OrthancPlugins::RegisterRestCallback<ServeEmbeddedFile>("/wsi/app/(viewer.html)", true);
+    OrthancPlugins::RegisterRestCallback<ServeEmbeddedFile>("/wsi/app/(viewer.js)", true);
+#else
+    OrthancPlugins::RegisterRestCallback<ServeSourceFile>("/wsi/app/(viewer.html)", true);
+    OrthancPlugins::RegisterRestCallback<ServeSourceFile>("/wsi/app/(viewer.js)", true);
+#endif
+
     OrthancPlugins::RegisterRestCallback<ServePyramid>("/wsi/pyramids/([0-9a-f-]+)", true);
     OrthancPlugins::RegisterRestCallback<ServeTile>("/wsi/tiles/([0-9a-f-]+)/([0-9-]+)/([0-9-]+)/([0-9-]+)", true);
     OrthancPlugins::RegisterRestCallback<ServeFramePyramid>("/wsi/frames-pyramids/([0-9a-f-]+)/([0-9-]+)", true);
@@ -598,12 +642,12 @@
 
     if (serveMirador)
     {
-      OrthancPlugins::RegisterRestCallback<ServeFile>("/wsi/app/(mirador.html)", true);
+      OrthancPlugins::RegisterRestCallback<ServeEmbeddedFile>("/wsi/app/(mirador.html)", true);
     }
 
     if (serveOpenSeadragon)
     {
-      OrthancPlugins::RegisterRestCallback<ServeFile>("/wsi/app/(openseadragon.html)", true);
+      OrthancPlugins::RegisterRestCallback<ServeEmbeddedFile>("/wsi/app/(openseadragon.html)", true);
     }
 
     {