Mercurial > hg > orthanc-stl
comparison Sources/Plugin.cpp @ 70:a5c1be2ec26a
new configuration option: STL.3DHOP.CanvasStyle
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 Oct 2024 17:12:49 +0200 |
parents | 054e36474998 |
children | e94f177c3653 |
comparison
equal
deleted
inserted
replaced
69:c42bac260709 | 70:a5c1be2ec26a |
---|---|
117 * script, we use a cache to maintain the uncompressed assets in order | 117 * script, we use a cache to maintain the uncompressed assets in order |
118 * to avoid multiple gzip decodings. | 118 * to avoid multiple gzip decodings. |
119 **/ | 119 **/ |
120 class ResourcesCache : public boost::noncopyable | 120 class ResourcesCache : public boost::noncopyable |
121 { | 121 { |
122 public: | |
123 class IHandler : public boost::noncopyable | |
124 { | |
125 public: | |
126 virtual ~IHandler() | |
127 { | |
128 } | |
129 | |
130 virtual void Apply(const std::string& resource) = 0; | |
131 }; | |
132 | |
122 private: | 133 private: |
123 typedef std::map<std::string, std::string*> Content; | 134 typedef std::map<std::string, std::string*> Content; |
124 | 135 |
125 boost::shared_mutex mutex_; | 136 boost::shared_mutex mutex_; |
126 Content content_; | 137 Content content_; |
127 | 138 |
139 class RestOutputHandler : public IHandler | |
140 { | |
141 private: | |
142 OrthancPluginRestOutput* output_; | |
143 std::string mime_; | |
144 | |
145 public: | |
146 RestOutputHandler(OrthancPluginRestOutput* output, | |
147 const std::string& mime) : | |
148 output_(output), | |
149 mime_(mime) | |
150 { | |
151 } | |
152 | |
153 virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE | |
154 { | |
155 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_, | |
156 resource.c_str(), resource.size(), mime_.c_str()); | |
157 } | |
158 }; | |
159 | |
160 class StoreResourceIntoString : public IHandler | |
161 { | |
162 private: | |
163 std::string& target_; | |
164 | |
165 public: | |
166 StoreResourceIntoString(std::string& target) : | |
167 target_(target) | |
168 { | |
169 } | |
170 | |
171 virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE | |
172 { | |
173 target_ = resource; | |
174 } | |
175 }; | |
176 | |
128 public: | 177 public: |
129 ~ResourcesCache() | 178 ~ResourcesCache() |
130 { | 179 { |
131 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | 180 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) |
132 { | 181 { |
133 assert(it->second != NULL); | 182 assert(it->second != NULL); |
134 delete it->second; | 183 delete it->second; |
135 } | 184 } |
136 } | 185 } |
137 | 186 |
138 void Answer(OrthancPluginRestOutput* output, | 187 void Apply(IHandler& handler, |
139 const std::string& path) | 188 const std::string& path) |
140 { | 189 { |
141 const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path)); | |
142 | |
143 { | 190 { |
144 // Check whether the cache already contains the resource | 191 // Check whether the cache already contains the resource |
145 boost::shared_lock<boost::shared_mutex> lock(mutex_); | 192 boost::shared_lock<boost::shared_mutex> lock(mutex_); |
146 | 193 |
147 Content::const_iterator found = content_.find(path); | 194 Content::const_iterator found = content_.find(path); |
148 | 195 |
149 if (found != content_.end()) | 196 if (found != content_.end()) |
150 { | 197 { |
151 assert(found->second != NULL); | 198 assert(found->second != NULL); |
152 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, found->second->c_str(), found->second->size(), mime.c_str()); | 199 handler.Apply(*found->second); |
153 return; | 200 return; |
154 } | 201 } |
155 } | 202 } |
156 | 203 |
157 // This resource has not been cached yet | 204 // This resource has not been cached yet |
158 | 205 |
159 std::unique_ptr<std::string> item(new std::string); | 206 std::unique_ptr<std::string> item(new std::string); |
160 ReadStaticAsset(*item, path); | 207 ReadStaticAsset(*item, path); |
161 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, item->c_str(), item->size(), mime.c_str()); | 208 handler.Apply(*item); |
162 | 209 |
163 { | 210 { |
164 // Store the resource into the cache | 211 // Store the resource into the cache |
165 boost::unique_lock<boost::shared_mutex> lock(mutex_); | 212 boost::unique_lock<boost::shared_mutex> lock(mutex_); |
166 | 213 |
167 if (content_.find(path) == content_.end()) | 214 if (content_.find(path) == content_.end()) |
168 { | 215 { |
169 content_[path] = item.release(); | 216 content_[path] = item.release(); |
170 } | 217 } |
171 } | 218 } |
219 } | |
220 | |
221 void Answer(OrthancPluginRestOutput* output, | |
222 const std::string& path) | |
223 { | |
224 const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path)); | |
225 | |
226 RestOutputHandler handler(output, mime); | |
227 Apply(handler, path); | |
228 } | |
229 | |
230 void ReadResource(std::string& target, | |
231 const std::string& path) | |
232 { | |
233 StoreResourceIntoString handler(target); | |
234 Apply(handler, path); | |
172 } | 235 } |
173 }; | 236 }; |
174 | 237 |
175 | 238 |
176 static ResourcesCache cache_; | 239 static ResourcesCache cache_; |
979 #endif | 1042 #endif |
980 | 1043 |
981 | 1044 |
982 #if ORTHANC_ENABLE_3DHOP == 1 | 1045 #if ORTHANC_ENABLE_3DHOP == 1 |
983 | 1046 |
1047 // This is the default background style of 3DHOP | |
1048 static std::string canvasStyle3DHOP_ = "background-image: url(skins/backgrounds/light.jpg)"; | |
1049 | |
984 void Serve3DHOPAssets(OrthancPluginRestOutput* output, | 1050 void Serve3DHOPAssets(OrthancPluginRestOutput* output, |
985 const char* url, | 1051 const char* url, |
986 const OrthancPluginHttpRequest* request) | 1052 const OrthancPluginHttpRequest* request) |
987 { | 1053 { |
988 if (request->method != OrthancPluginHttpMethod_Get) | 1054 if (request->method != OrthancPluginHttpMethod_Get) |
990 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); | 1056 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
991 return; | 1057 return; |
992 } | 1058 } |
993 | 1059 |
994 const std::string file = request->groups[0]; | 1060 const std::string file = request->groups[0]; |
995 cache_.Answer(output, "3dhop/" + file); | 1061 const std::string resourceId = "3dhop/" + file; |
1062 | |
1063 if (file == "3DHOP_all_tools.html") | |
1064 { | |
1065 std::string resource; | |
1066 cache_.ReadResource(resource, resourceId); | |
1067 | |
1068 boost::replace_all(resource, "${{CANVAS_STYLE}}", canvasStyle3DHOP_); | |
1069 | |
1070 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, resource.c_str(), | |
1071 resource.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Html)); | |
1072 } | |
1073 else | |
1074 { | |
1075 cache_.Answer(output, resourceId); | |
1076 } | |
996 } | 1077 } |
997 | 1078 |
998 #endif | 1079 #endif |
999 | 1080 |
1000 | 1081 |
1050 OrthancPlugins::OrthancConfiguration globalConfiguration; | 1131 OrthancPlugins::OrthancConfiguration globalConfiguration; |
1051 OrthancPlugins::OrthancConfiguration configuration; | 1132 OrthancPlugins::OrthancConfiguration configuration; |
1052 globalConfiguration.GetSection(configuration, "STL"); | 1133 globalConfiguration.GetSection(configuration, "STL"); |
1053 | 1134 |
1054 #if ORTHANC_ENABLE_NEXUS == 1 | 1135 #if ORTHANC_ENABLE_NEXUS == 1 |
1136 OrthancPlugins::OrthancConfiguration configuration3DHOP; | |
1137 configuration.GetSection(configuration3DHOP, "3DHOP"); | |
1138 | |
1055 const bool enableNexus = configuration.GetBooleanValue("EnableNexus", false); | 1139 const bool enableNexus = configuration.GetBooleanValue("EnableNexus", false); |
1056 | 1140 |
1057 if (enableNexus) | 1141 if (enableNexus) |
1058 { | 1142 { |
1059 LOG(INFO) << "Support for Nexus is enabled"; | 1143 LOG(INFO) << "Support for Nexus is enabled"; |
1069 * as can be seen in the "minimal/js/presenter.js" file. Furthermore, it | 1153 * as can be seen in the "minimal/js/presenter.js" file. Furthermore, it |
1070 * requires the extension to be part of the URL. | 1154 * requires the extension to be part of the URL. |
1071 **/ | 1155 **/ |
1072 OrthancPlugins::RegisterRestCallback<ExtractNexusModel>("/stl/3dhop-instances/([0-9a-f-]+).nxz", true); | 1156 OrthancPlugins::RegisterRestCallback<ExtractNexusModel>("/stl/3dhop-instances/([0-9a-f-]+).nxz", true); |
1073 #endif | 1157 #endif |
1158 | |
1159 // New in release 1.3 | |
1160 canvasStyle3DHOP_ = configuration3DHOP.GetStringValue( | |
1161 "CanvasStyle", | |
1162 "background-image: url(skins/backgrounds/light.jpg)"); // This is the default background style of 3DHOP | |
1074 | 1163 |
1075 const bool hasCreateNexus_ = OrthancPlugins::CheckMinimalOrthancVersion(1, 9, 4); | 1164 const bool hasCreateNexus_ = OrthancPlugins::CheckMinimalOrthancVersion(1, 9, 4); |
1076 | 1165 |
1077 if (hasCreateNexus_) | 1166 if (hasCreateNexus_) |
1078 { | 1167 { |