diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2018-10-13 22:03:19 +0300 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2018-12-17 11:37:56 +0200 |
commit | a228f3d80af715665ee28627a1239998af0a52d9 (patch) | |
tree | ed60c2b3361c6f227aad36e7c0555491bcab35a5 | |
parent | f076a1fe39a466f9be91934e35ed179e45edece0 (diff) | |
download | qtlocation-mapboxgl-a228f3d80af715665ee28627a1239998af0a52d9.tar.gz |
[core] Refactor duplicated code in Asset and File sources
-rw-r--r-- | cmake/filesource-files.txt | 2 | ||||
-rw-r--r-- | platform/default/include/mbgl/storage/local_file_request.hpp | 13 | ||||
-rw-r--r-- | platform/default/src/mbgl/storage/asset_file_source.cpp | 29 | ||||
-rw-r--r-- | platform/default/src/mbgl/storage/local_file_request.cpp | 37 | ||||
-rw-r--r-- | platform/default/src/mbgl/storage/local_file_source.cpp | 34 |
5 files changed, 58 insertions, 57 deletions
diff --git a/cmake/filesource-files.txt b/cmake/filesource-files.txt index 1909885e47..4eef1a3873 100644 --- a/cmake/filesource-files.txt +++ b/cmake/filesource-files.txt @@ -10,6 +10,8 @@ src/mbgl/storage/asset_file_source.hpp platform/default/src/mbgl/storage/asset_file_source.cpp src/mbgl/storage/local_file_source.hpp platform/default/src/mbgl/storage/local_file_source.cpp +platform/default/include/mbgl/storage/local_file_request.hpp +platform/default/src/mbgl/storage/local_file_request.cpp # Offline include/mbgl/storage/offline.hpp diff --git a/platform/default/include/mbgl/storage/local_file_request.hpp b/platform/default/include/mbgl/storage/local_file_request.hpp new file mode 100644 index 0000000000..590ae8bbc7 --- /dev/null +++ b/platform/default/include/mbgl/storage/local_file_request.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <mbgl/util/string.hpp> + +namespace mbgl { + +template <typename> +class ActorRef; +class FileSourceRequest; + +void requestLocalFile(const std::string&, ActorRef<FileSourceRequest>); + +} // namespace mbgl diff --git a/platform/default/src/mbgl/storage/asset_file_source.cpp b/platform/default/src/mbgl/storage/asset_file_source.cpp index ed446f85a5..b14d73045f 100644 --- a/platform/default/src/mbgl/storage/asset_file_source.cpp +++ b/platform/default/src/mbgl/storage/asset_file_source.cpp @@ -1,14 +1,10 @@ #include <mbgl/storage/asset_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> -#include <mbgl/util/util.hpp> -#include <mbgl/util/io.hpp> - -#include <sys/types.h> -#include <sys/stat.h> namespace { @@ -25,9 +21,8 @@ public: } void request(const std::string& url, ActorRef<FileSourceRequest> req) { - Response response; - if (!acceptsURL(url)) { + Response response; response.error = std::make_unique<Response::Error>(Response::Error::Reason::Other, "Invalid asset URL"); req.invoke(&FileSourceRequest::setResponse, response); @@ -36,25 +31,7 @@ public: // Cut off the protocol and prefix with path. const auto path = root + "/" + mbgl::util::percentDecode(url.substr(assetProtocol.size())); - struct stat buf; - int result = stat(path.c_str(), &buf); - - if (result == 0 && (S_IFDIR & buf.st_mode)) { - response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); - } else if (result == -1 && errno == ENOENT) { - response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); - } else { - auto data = util::readFile(path); - if (!data) { - response.error = std::make_unique<Response::Error>( - Response::Error::Reason::Other, - std::string("Cannot read file ") + path); - } else { - response.data = std::make_shared<std::string>(std::move(*data)); - } - } - - req.invoke(&FileSourceRequest::setResponse, response); + requestLocalFile(path, std::move(req)); } private: diff --git a/platform/default/src/mbgl/storage/local_file_request.cpp b/platform/default/src/mbgl/storage/local_file_request.cpp new file mode 100644 index 0000000000..084e6ff1b0 --- /dev/null +++ b/platform/default/src/mbgl/storage/local_file_request.cpp @@ -0,0 +1,37 @@ +#include <mbgl/storage/file_source_request.hpp> +#include <mbgl/storage/response.hpp> +#include <mbgl/util/io.hpp> + +#include <sys/types.h> +#include <sys/stat.h> + +#if defined(_WINDOWS) && !defined(S_ISDIR) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#endif + +namespace mbgl { + +void requestLocalFile(const std::string& path, ActorRef<FileSourceRequest> req) { + Response response; + struct stat buf; + int result = stat(path.c_str(), &buf); + + if (result == 0 && (S_IFDIR & buf.st_mode)) { + response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); + } else if (result == -1 && errno == ENOENT) { + response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); + } else { + auto data = util::readFile(path); + if (!data) { + response.error = std::make_unique<Response::Error>( + Response::Error::Reason::Other, + std::string("Cannot read file ") + path); + } else { + response.data = std::make_shared<std::string>(std::move(*data)); + } + } + + req.invoke(&FileSourceRequest::setResponse, response); +} + +} // namespace mbgl diff --git a/platform/default/src/mbgl/storage/local_file_source.cpp b/platform/default/src/mbgl/storage/local_file_source.cpp index 1bc52fb419..ca2eedc7ba 100644 --- a/platform/default/src/mbgl/storage/local_file_source.cpp +++ b/platform/default/src/mbgl/storage/local_file_source.cpp @@ -1,18 +1,10 @@ #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> -#include <mbgl/util/util.hpp> -#include <mbgl/util/io.hpp> - -#include <sys/types.h> -#include <sys/stat.h> - -#if defined(_WINDOWS) && !defined(S_ISDIR) -#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) -#endif namespace { @@ -27,9 +19,8 @@ public: Impl(ActorRef<Impl>) {} void request(const std::string& url, ActorRef<FileSourceRequest> req) { - Response response; - if (!acceptsURL(url)) { + Response response; response.error = std::make_unique<Response::Error>(Response::Error::Reason::Other, "Invalid file URL"); req.invoke(&FileSourceRequest::setResponse, response); @@ -38,27 +29,8 @@ public: // Cut off the protocol and prefix with path. const auto path = mbgl::util::percentDecode(url.substr(fileProtocol.size())); - struct stat buf; - int result = stat(path.c_str(), &buf); - - if (result == 0 && S_ISDIR(buf.st_mode)) { - response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); - } else if (result == -1 && errno == ENOENT) { - response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound); - } else { - auto data = util::readFile(path); - if (!data) { - response.error = std::make_unique<Response::Error>( - Response::Error::Reason::Other, - std::string("Cannot read file ") + path); - } else { - response.data = std::make_shared<std::string>(std::move(*data)); - } - } - - req.invoke(&FileSourceRequest::setResponse, response); + requestLocalFile(path, std::move(req)); } - }; LocalFileSource::LocalFileSource() |