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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#include <mbgl/storage/asset_file_source.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/url.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#include <zip.h>
#pragma GCC diagnostic pop
namespace {
struct ZipHolder {
ZipHolder(struct zip* archive_) : archive(archive_) {}
~ZipHolder() {
if (archive) ::zip_close(archive);
}
struct zip* archive;
};
struct ZipFileHolder {
ZipFileHolder(struct zip_file* file_) : file(file_) {}
~ZipFileHolder() {
if (file) ::zip_fclose(file);
}
struct zip_file* file;
};
}
namespace mbgl {
class AssetFileRequest : public AsyncRequest {
public:
AssetFileRequest(std::unique_ptr<WorkRequest> workRequest_)
: workRequest(std::move(workRequest_)) {
}
std::unique_ptr<WorkRequest> workRequest;
};
class AssetFileSource::Impl {
public:
Impl(const std::string& root_)
: root(root_) {
}
void request(const std::string& url, FileSource::Callback callback) {
ZipHolder archive = ::zip_open(root.c_str(), 0, nullptr);
if (!archive.archive) {
reportError(Response::Error::Reason::Other, "Could not open zip archive", callback);
return;
}
struct zip_stat stat;
::zip_stat_init(&stat);
std::string path = std::string("assets/") + mbgl::util::percentDecode(url.substr(8));
int ret = ::zip_stat(archive.archive, path.c_str(), 0, &stat);
if (ret < 0 || !(stat.valid & ZIP_STAT_SIZE)) {
reportError(Response::Error::Reason::NotFound, "Could not stat file in zip archive", callback);
return;
}
ZipFileHolder file = ::zip_fopen(archive.archive, path.c_str(), 0);
if (!file.file) {
reportError(Response::Error::Reason::NotFound, "Could not open file in zip archive", callback);
return;
}
std::shared_ptr<std::string> buf = std::make_shared<std::string>();
buf->resize(stat.size);
ret = ::zip_fread(file.file, &buf->front(), stat.size);
if (ret < 0) {
reportError(Response::Error::Reason::Other, "Could not read file in zip archive", callback);
return;
}
Response response;
response.data = buf;
callback(response);
}
void reportError(Response::Error::Reason reason, const char * message, FileSource::Callback callback) {
Response response;
response.error = std::make_unique<Response::Error>(reason, message);
callback(response);
}
private:
std::string root;
};
AssetFileSource::AssetFileSource(const std::string& root)
: thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{"AssetFileSource"},
root)) {
}
AssetFileSource::~AssetFileSource() = default;
std::unique_ptr<AsyncRequest> AssetFileSource::request(const Resource& resource, Callback callback) {
return thread->invokeWithCallback(&Impl::request, callback, resource.url);
}
}
|