Skip to content

Commit 8cb460b

Browse files
committed
Resources: use oatpp::data::resource::File/InMemoryData
1 parent f39e6a5 commit 8cb460b

File tree

5 files changed

+101
-183
lines changed

5 files changed

+101
-183
lines changed

src/oatpp-swagger/AsyncController.hpp

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,7 @@ class AsyncController : public oatpp::web::server::api::ApiController {
124124
ENDPOINT_ASYNC_INIT(GetUIRoot)
125125

126126
Action act() override {
127-
std::string ui;
128-
if(controller->m_resources->isStreaming()) {
129-
v_char8 buffer[1024];
130-
auto fileStream = controller->m_resources->getResourceStream("index.html");
131-
oatpp::data::stream::BufferOutputStream s(1024);
132-
oatpp::data::stream::transfer(fileStream, &s, 0, buffer, 1024);
133-
ui = s.toString();
134-
} else {
135-
ui = * controller->m_resources->getResource("index.html"); // * - copy of the index.html
136-
}
127+
auto ui = controller->m_resources->getResourceData("index.html");
137128
return _return(controller->createResponse(Status::CODE_200, ui));
138129
}
139130

@@ -144,16 +135,7 @@ class AsyncController : public oatpp::web::server::api::ApiController {
144135
ENDPOINT_ASYNC_INIT(GetInitializer)
145136

146137
Action act() override {
147-
std::string ui;
148-
if(controller->m_resources->isStreaming()) {
149-
v_char8 buffer[1024];
150-
auto fileStream = controller->m_resources->getResourceStream("swagger-initializer.js");
151-
oatpp::data::stream::BufferOutputStream s(1024);
152-
oatpp::data::stream::transfer(fileStream, &s, 0, buffer, 1024);
153-
ui = s.toString();
154-
} else {
155-
ui = * controller->m_resources->getResource("swagger-initializer.js"); // * - copy of the index.html
156-
}
138+
std::string ui = controller->m_resources->getResourceData("swagger-initializer.js");
157139
ui.replace(ui.find("%%API.JSON%%"), 12, controller->m_paths.apiJson);
158140
return _return(controller->createResponse(Status::CODE_200, ui));
159141
}
@@ -167,18 +149,14 @@ class AsyncController : public oatpp::web::server::api::ApiController {
167149
Action act() override {
168150
auto filename = request->getPathVariable("filename");
169151
OATPP_ASSERT_HTTP(filename, Status::CODE_400, "filename should not be null")
170-
if(controller->m_resources->isStreaming()) {
171-
auto body = std::make_shared<oatpp::web::protocol::http::outgoing::StreamingBody>(
172-
controller->m_resources->getResourceStream(filename->c_str())
173-
);
174-
auto resp = OutgoingResponse::createShared(Status::CODE_200, body);
175-
resp->putHeader("Content-Type", controller->m_resources->getMimeType(filename));
176-
return _return(resp);
177-
}
178-
auto resp = controller->createResponse(Status::CODE_200,
179-
controller->m_resources->getResource(filename->c_str()));
152+
153+
auto body = std::make_shared<oatpp::web::protocol::http::outgoing::StreamingBody>(
154+
controller->m_resources->getResource(filename)->openInputStream()
155+
);
156+
auto resp = OutgoingResponse::createShared(Status::CODE_200, body);
180157
resp->putHeader("Content-Type", controller->m_resources->getMimeType(filename));
181158
return _return(resp);
159+
182160
}
183161

184162
};

src/oatpp-swagger/Controller.hpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,44 +112,20 @@ class Controller : public oatpp::web::server::api::ApiController {
112112
}
113113

114114
ENDPOINT("GET", m_paths.ui, getUIRoot) {
115-
std::string ui;
116-
if(m_resources->isStreaming()) {
117-
v_char8 buffer[1024];
118-
auto fileStream = m_resources->getResourceStream("index.html");
119-
oatpp::data::stream::BufferOutputStream s(1024);
120-
oatpp::data::stream::transfer(fileStream, &s, 0, buffer, 1024);
121-
ui = s.toString();
122-
} else {
123-
ui = *m_resources->getResource("index.html"); // * - copy of the index.html
124-
}
125-
return createResponse(Status::CODE_200, ui);
115+
return createResponse(Status::CODE_200, m_resources->getResourceData("index.html"));
126116
}
127117

128118
ENDPOINT("GET", m_paths.initializer, getInitializer) {
129-
std::string ui;
130-
if(m_resources->isStreaming()) {
131-
v_char8 buffer[1024];
132-
auto fileStream = m_resources->getResourceStream("swagger-initializer.js");
133-
oatpp::data::stream::BufferOutputStream s(1024);
134-
oatpp::data::stream::transfer(fileStream, &s, 0, buffer, 1024);
135-
ui = s.toString();
136-
} else {
137-
ui = *m_resources->getResource("swagger-initializer.js"); // * - copy of the index.html
138-
}
119+
std::string ui = m_resources->getResourceData("swagger-initializer.js");
139120
ui.replace(ui.find("%%API.JSON%%"), 12, m_paths.apiJson);
140121
return createResponse(Status::CODE_200, ui);
141122
}
142123

143124
ENDPOINT("GET", m_paths.uiResources, getUIResource, PATH(String, filename)) {
144-
if(m_resources->isStreaming()) {
145-
auto body = std::make_shared<oatpp::web::protocol::http::outgoing::StreamingBody>(
146-
m_resources->getResourceStream(filename->c_str())
147-
);
148-
auto resp = OutgoingResponse::createShared(Status::CODE_200, body);
149-
resp->putHeader("Content-Type", m_resources->getMimeType(filename));
150-
return resp;
151-
}
152-
auto resp = createResponse(Status::CODE_200, m_resources->getResource(filename->c_str()));
125+
auto body = std::make_shared<oatpp::web::protocol::http::outgoing::StreamingBody>(
126+
m_resources->getResource(filename)->openInputStream()
127+
);
128+
auto resp = OutgoingResponse::createShared(Status::CODE_200, body);
153129
resp->putHeader("Content-Type", m_resources->getMimeType(filename));
154130
return resp;
155131
}

src/oatpp-swagger/Resources.cpp

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,87 +24,85 @@
2424

2525
#include "Resources.hpp"
2626

27+
#include "oatpp/data/resource/File.hpp"
28+
#include "oatpp/data/resource/InMemoryData.hpp"
2729
#include "oatpp/base/Log.hpp"
2830

29-
#include <stdio.h>
3031
#include <fstream>
3132

3233
namespace oatpp { namespace swagger {
3334

34-
Resources::Resources(const oatpp::String& resDir, bool streaming) {
35-
36-
if(!resDir || resDir->size() == 0) {
35+
Resources::Resources(const oatpp::String& resDir, bool streaming)
36+
: m_resDir(resDir)
37+
, m_streaming(streaming)
38+
{
39+
40+
if(!resDir || resDir->empty()) {
3741
throw std::runtime_error("[oatpp::swagger::Resources::Resources()]: Invalid resDir path. Please specify full path to oatpp-swagger/res folder");
3842
}
39-
40-
m_resDir = resDir;
41-
if(m_resDir->data()[m_resDir->size() - 1] != '/') {
42-
m_resDir = m_resDir + "/";
43-
}
4443

45-
m_streaming = streaming;
44+
addResource("favicon-16x16.png");
45+
addResource("favicon-32x32.png");
46+
addResource("index.css");
47+
addResource("index.html");
48+
addResource("oauth2-redirect.html");
49+
addResource("swagger-initializer.js");
50+
addResource("swagger-ui-bundle.js");
51+
addResource("swagger-ui-bundle.js.map");
52+
addResource("swagger-ui-es-bundle-core.js");
53+
addResource("swagger-ui-es-bundle-core.js.map");
54+
addResource("swagger-ui-es-bundle.js");
55+
addResource("swagger-ui-es-bundle.js.map");
56+
addResource("swagger-ui-standalone-preset.js");
57+
addResource("swagger-ui-standalone-preset.js.map");
58+
addResource("swagger-ui.css");
59+
addResource("swagger-ui.css.map");
60+
addResource("swagger-ui.js");
61+
addResource("swagger-ui.js.map");
4662

4763
}
4864

49-
void Resources::cacheResource(const char* fileName) {
50-
m_resources[fileName] = loadFromFile(fileName);
51-
}
52-
53-
oatpp::String Resources::loadFromFile(const char* fileName) {
54-
55-
auto fullFilename = m_resDir + fileName;
56-
57-
std::ifstream file (fullFilename->c_str(), std::ios::in|std::ios::binary|std::ios::ate);
58-
59-
if (file.is_open()) {
60-
61-
auto result = oatpp::String((v_int32) file.tellg());
62-
file.seekg(0, std::ios::beg);
63-
file.read((char*)result->data(), result->size());
64-
file.close();
65-
return result;
66-
65+
void Resources::addResource(const oatpp::String& fileName) {
66+
67+
if(m_streaming) {
68+
m_resources[fileName] = std::make_shared<data::resource::File>(m_resDir, fileName);
69+
} else {
70+
auto path = data::resource::File::concatDirAndName(m_resDir, fileName);
71+
auto data = oatpp::String::loadFromFile(path->c_str());
72+
if(!data) {
73+
OATPP_LOGe("oatpp::swagger::Resources::addResource()", "Can't load file '{}'", path);
74+
throw std::runtime_error("[oatpp::swagger::Resources::addResource()]: Can't load file. Please make sure you specified full path to oatpp-swagger/res folder");
75+
}
76+
m_resources[fileName] = std::make_shared<data::resource::InMemoryData>(data);
6777
}
68-
69-
OATPP_LOGe("oatpp::swagger::Resources::loadFromFile()", "Can't load file '{}'", fullFilename);
70-
throw std::runtime_error("[oatpp::swagger::Resources::loadFromFile(...)]: Can't load file. Please make sure you specified full path to oatpp-swagger/res folder");
71-
7278
}
73-
74-
oatpp::String Resources::getResource(const oatpp::String& filename) {
79+
80+
void Resources::overrideResource(const oatpp::String& filename, const std::shared_ptr<data::resource::Resource>& resource) {
81+
m_resources[filename] = resource;
82+
}
83+
84+
std::shared_ptr<data::resource::Resource> Resources::getResource(const oatpp::String& filename) const {
7585

7686
auto it = m_resources.find(filename);
7787
if(it != m_resources.end()) {
7888
return it->second;
7989
}
80-
throw std::runtime_error(
81-
"[oatpp::swagger::Resources::getResource(...)]: Resource file not found. "
90+
throw std::runtime_error("[oatpp::swagger::Resources::getResource()]: Resource file not found. "
8291
"Please make sure: "
8392
"1. You are using correct version of oatpp-swagger. "
8493
"2. oatpp-swagger/res is not empty. "
85-
"3. You specified correct full path to oatpp-swagger/res folder"
86-
);
94+
"3. You specified correct full path to oatpp-swagger/res folder");
8795
}
8896

89-
std::shared_ptr<Resources::ReadCallback> Resources::getResourceStream(const oatpp::String &filename) {
90-
try {
91-
return std::make_shared<ReadCallback>(m_resDir + filename);
92-
} catch(std::runtime_error &e) {
93-
throw std::runtime_error(
94-
"[oatpp::swagger::Resources::getResource(...)]: Resource file not found. "
95-
"Please make sure: "
96-
"1. You are using correct version of oatpp-swagger. "
97-
"2. oatpp-swagger/res is not empty. "
98-
"3. You specified correct full path to oatpp-swagger/res folder"
99-
);
97+
oatpp::String Resources::getResourceData(const oatpp::String& filename) const {
98+
auto resource = getResource(filename);
99+
if(resource->getInMemoryData() && resource->getKnownSize() > 0) {
100+
return resource->getInMemoryData();
100101
}
101-
}
102-
103-
Resources::ReadCallback::ReadCallback(const oatpp::String &file) : m_file(file), m_stream(file->c_str())
104-
{}
105-
106-
v_io_size Resources::ReadCallback::read(void *buffer, v_buff_size count, async::Action& action) {
107-
return m_stream.read(buffer, count, action);
102+
v_char8 buffer[1024];
103+
oatpp::data::stream::BufferOutputStream ss(1024);
104+
oatpp::data::stream::transfer(resource->openInputStream(), &ss, 0, buffer, 1024);
105+
return ss.toString();
108106
}
109107

110108
bool Resources::hasEnding(std::string fullString, std::string const &ending) const {
@@ -129,4 +127,8 @@ std::string Resources::getMimeType(const std::string &filename) const {
129127
return "text/plain";
130128
}
131129

130+
bool Resources::isStreaming() const {
131+
return m_streaming;
132+
}
133+
132134
}}

0 commit comments

Comments
 (0)