blob: 503d4dfb1d6a3be467a9636030adf33d1f08bb53 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <mbgl/util/filesource.hpp>
#include <mbgl/platform/platform.hpp>
#include <fstream>
#include <sstream>
namespace mbgl {
FileSource::FileSource() {}
void FileSource::setBase(const std::string &value) {
base = value;
}
const std::string &FileSource::getBase() const {
return base;
}
void FileSource::load(ResourceType /*type*/, const std::string &url, std::function<void(platform::Response *)> callback, const std::shared_ptr<uv::loop> loop) {
// convert relative URLs to absolute URLs
const std::string absoluteURL = [&]() -> std::string {
const size_t separator = url.find("://");
if (separator == std::string::npos) {
// Relative URL.
return base + url;
} else {
return url;
}
}();
const size_t separator = absoluteURL.find("://");
const std::string protocol = separator != std::string::npos ? absoluteURL.substr(0, separator) : "";
if (protocol == "file") {
// load from disk
const std::string path = absoluteURL.substr(separator + 3);
std::ifstream file(path);
platform::Response response(callback);
if (!file.good()) {
response.error_message = "file not found (" + path + ")";
} else {
std::stringstream data;
data << file.rdbuf();
response.code = 200;
response.body = data.str();
}
callback(&response);
} else {
// load from the internet
platform::request_http(absoluteURL, callback, loop);
}
}
}
|