comparison Sources/ResourcesCache.cpp @ 72:e94f177c3653

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Oct 2024 17:25:46 +0200
parents
children 1963619e3e87
comparison
equal deleted inserted replaced
71:f3bbafc067d0 72:e94f177c3653
1 /**
2 * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * STL plugin for Orthanc
8 * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, Belgium
9 *
10 * This program is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **/
23
24
25 #include "ResourcesCache.h"
26
27 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
28
29 #include <Compatibility.h>
30 #include <SystemToolbox.h>
31
32
33 // Forward declaration
34 void ReadStaticAsset(std::string& target,
35 const std::string& path);
36
37
38 class ResourcesCache::RestOutputHandler : public IHandler
39 {
40 private:
41 OrthancPluginRestOutput* output_;
42 std::string mime_;
43
44 public:
45 RestOutputHandler(OrthancPluginRestOutput* output,
46 const std::string& mime) :
47 output_(output),
48 mime_(mime)
49 {
50 }
51
52 virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
53 {
54 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_,
55 resource.c_str(), resource.size(), mime_.c_str());
56 }
57 };
58
59
60 class ResourcesCache::StoreResourceIntoString : public IHandler
61 {
62 private:
63 std::string& target_;
64
65 public:
66 StoreResourceIntoString(std::string& target) :
67 target_(target)
68 {
69 }
70
71 virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
72 {
73 target_ = resource;
74 }
75 };
76
77
78 ResourcesCache::~ResourcesCache()
79 {
80 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
81 {
82 assert(it->second != NULL);
83 delete it->second;
84 }
85 }
86
87
88 void ResourcesCache::Apply(IHandler& handler,
89 const std::string& path)
90 {
91 {
92 // Check whether the cache already contains the resource
93 boost::shared_lock<boost::shared_mutex> lock(mutex_);
94
95 Content::const_iterator found = content_.find(path);
96
97 if (found != content_.end())
98 {
99 assert(found->second != NULL);
100 handler.Apply(*found->second);
101 return;
102 }
103 }
104
105 // This resource has not been cached yet
106
107 std::unique_ptr<std::string> item(new std::string);
108 ReadStaticAsset(*item, path);
109 handler.Apply(*item);
110
111 {
112 // Store the resource into the cache
113 boost::unique_lock<boost::shared_mutex> lock(mutex_);
114
115 if (content_.find(path) == content_.end())
116 {
117 content_[path] = item.release();
118 }
119 }
120 }
121
122
123 void ResourcesCache::Answer(OrthancPluginRestOutput* output,
124 const std::string& path)
125 {
126 const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
127
128 RestOutputHandler handler(output, mime);
129 Apply(handler, path);
130 }
131
132
133 void ResourcesCache::ReadResource(std::string& target,
134 const std::string& path)
135 {
136 StoreResourceIntoString handler(target);
137 Apply(handler, path);
138 }