24
24
25
25
#include " Resources.hpp"
26
26
27
+ #include " oatpp/data/resource/File.hpp"
28
+ #include " oatpp/data/resource/InMemoryData.hpp"
27
29
#include " oatpp/base/Log.hpp"
28
30
29
- #include < stdio.h>
30
31
#include < fstream>
31
32
32
33
namespace oatpp { namespace swagger {
33
34
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 ()) {
37
41
throw std::runtime_error (" [oatpp::swagger::Resources::Resources()]: Invalid resDir path. Please specify full path to oatpp-swagger/res folder" );
38
42
}
39
-
40
- m_resDir = resDir;
41
- if (m_resDir->data ()[m_resDir->size () - 1 ] != ' /' ) {
42
- m_resDir = m_resDir + " /" ;
43
- }
44
43
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" );
46
62
47
63
}
48
64
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);
67
77
}
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
-
72
78
}
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 {
75
85
76
86
auto it = m_resources.find (filename);
77
87
if (it != m_resources.end ()) {
78
88
return it->second ;
79
89
}
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. "
82
91
" Please make sure: "
83
92
" 1. You are using correct version of oatpp-swagger. "
84
93
" 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" );
87
95
}
88
96
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 ();
100
101
}
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 ();
108
106
}
109
107
110
108
bool Resources::hasEnding (std::string fullString, std::string const &ending) const {
@@ -129,4 +127,8 @@ std::string Resources::getMimeType(const std::string &filename) const {
129
127
return " text/plain" ;
130
128
}
131
129
130
+ bool Resources::isStreaming () const {
131
+ return m_streaming;
132
+ }
133
+
132
134
}}
0 commit comments