comparison OrthancServer/Plugins/Samples/StorageArea/Plugin.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Plugins/Samples/StorageArea/Plugin.cpp@94f4a18a79cc
children d9473bd5ed43
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include <orthanc/OrthancCPlugin.h>
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <string>
27
28 static OrthancPluginContext* context = NULL;
29
30
31 static std::string GetPath(const char* uuid)
32 {
33 return "plugin_" + std::string(uuid);
34 }
35
36
37 static OrthancPluginErrorCode StorageCreate(const char* uuid,
38 const void* content,
39 int64_t size,
40 OrthancPluginContentType type)
41 {
42 std::string path = GetPath(uuid);
43
44 FILE* fp = fopen(path.c_str(), "wb");
45 if (!fp)
46 {
47 return OrthancPluginErrorCode_StorageAreaPlugin;
48 }
49
50 bool ok = fwrite(content, size, 1, fp) == 1;
51 fclose(fp);
52
53 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_StorageAreaPlugin;
54 }
55
56
57 static OrthancPluginErrorCode StorageRead(void** content,
58 int64_t* size,
59 const char* uuid,
60 OrthancPluginContentType type)
61 {
62 std::string path = GetPath(uuid);
63
64 FILE* fp = fopen(path.c_str(), "rb");
65 if (!fp)
66 {
67 return OrthancPluginErrorCode_StorageAreaPlugin;
68 }
69
70 if (fseek(fp, 0, SEEK_END) < 0)
71 {
72 fclose(fp);
73 return OrthancPluginErrorCode_StorageAreaPlugin;
74 }
75
76 *size = ftell(fp);
77
78 if (fseek(fp, 0, SEEK_SET) < 0)
79 {
80 fclose(fp);
81 return OrthancPluginErrorCode_StorageAreaPlugin;
82 }
83
84 bool ok = true;
85
86 if (*size == 0)
87 {
88 *content = NULL;
89 }
90 else
91 {
92 *content = malloc(*size);
93 if (*content == NULL ||
94 fread(*content, *size, 1, fp) != 1)
95 {
96 ok = false;
97 }
98 }
99
100 fclose(fp);
101
102 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_StorageAreaPlugin;
103 }
104
105
106 static OrthancPluginErrorCode StorageRemove(const char* uuid,
107 OrthancPluginContentType type)
108 {
109 std::string path = GetPath(uuid);
110
111 if (remove(path.c_str()) == 0)
112 {
113 return OrthancPluginErrorCode_Success;
114 }
115 else
116 {
117 return OrthancPluginErrorCode_StorageAreaPlugin;
118 }
119 }
120
121
122 extern "C"
123 {
124 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
125 {
126 context = c;
127 OrthancPluginLogWarning(context, "Storage plugin is initializing");
128
129 /* Check the version of the Orthanc core */
130 if (OrthancPluginCheckVersion(c) == 0)
131 {
132 char info[1024];
133 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
134 c->orthancVersion,
135 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
136 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
137 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
138 OrthancPluginLogError(context, info);
139 return -1;
140 }
141
142 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove);
143
144 return 0;
145 }
146
147
148 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
149 {
150 OrthancPluginLogWarning(context, "Storage plugin is finalizing");
151 }
152
153
154 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
155 {
156 return "storage";
157 }
158
159
160 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
161 {
162 return "1.0";
163 }
164 }