blob: ca2eedc7ba0c57881edb3830469a5718118006b6 (
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
|
#include <mbgl/storage/local_file_source.hpp>
#include <mbgl/storage/file_source_request.hpp>
#include <mbgl/storage/local_file_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/url.hpp>
namespace {
const std::string fileProtocol = "file://";
} // namespace
namespace mbgl {
class LocalFileSource::Impl {
public:
Impl(ActorRef<Impl>) {}
void request(const std::string& url, ActorRef<FileSourceRequest> req) {
if (!acceptsURL(url)) {
Response response;
response.error = std::make_unique<Response::Error>(Response::Error::Reason::Other,
"Invalid file URL");
req.invoke(&FileSourceRequest::setResponse, response);
return;
}
// Cut off the protocol and prefix with path.
const auto path = mbgl::util::percentDecode(url.substr(fileProtocol.size()));
requestLocalFile(path, std::move(req));
}
};
LocalFileSource::LocalFileSource()
: impl(std::make_unique<util::Thread<Impl>>("LocalFileSource")) {
}
LocalFileSource::~LocalFileSource() = default;
std::unique_ptr<AsyncRequest> LocalFileSource::request(const Resource& resource, Callback callback) {
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
impl->actor().invoke(&Impl::request, resource.url, req->actor());
return std::move(req);
}
bool LocalFileSource::acceptsURL(const std::string& url) {
return 0 == url.rfind(fileProtocol, 0);
}
} // namespace mbgl
|