From 216e8f6c9bacff514a1e596bcd3132b94c80a71b Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Sat, 13 Oct 2018 22:03:19 +0300 Subject: [core] Refactor duplicated code in Asset and File sources --- cmake/filesource-files.txt | 2 ++ .../include/mbgl/storage/local_file_request.hpp | 13 ++++++++ .../default/src/mbgl/storage/asset_file_source.cpp | 29 ++--------------- .../src/mbgl/storage/local_file_request.cpp | 37 ++++++++++++++++++++++ .../default/src/mbgl/storage/local_file_source.cpp | 34 ++------------------ 5 files changed, 58 insertions(+), 57 deletions(-) create mode 100644 platform/default/include/mbgl/storage/local_file_request.hpp create mode 100644 platform/default/src/mbgl/storage/local_file_request.cpp 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 + +namespace mbgl { + +template +class ActorRef; +class FileSourceRequest; + +void requestLocalFile(const std::string&, ActorRef); + +} // 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 #include +#include #include #include #include #include -#include -#include - -#include -#include namespace { @@ -25,9 +21,8 @@ public: } void request(const std::string& url, ActorRef req) { - Response response; - if (!acceptsURL(url)) { + Response response; response.error = std::make_unique(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::Reason::NotFound); - } else if (result == -1 && errno == ENOENT) { - response.error = std::make_unique(Response::Error::Reason::NotFound); - } else { - auto data = util::readFile(path); - if (!data) { - response.error = std::make_unique( - Response::Error::Reason::Other, - std::string("Cannot read file ") + path); - } else { - response.data = std::make_shared(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 +#include +#include + +#include +#include + +#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 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::Reason::NotFound); + } else if (result == -1 && errno == ENOENT) { + response.error = std::make_unique(Response::Error::Reason::NotFound); + } else { + auto data = util::readFile(path); + if (!data) { + response.error = std::make_unique( + Response::Error::Reason::Other, + std::string("Cannot read file ") + path); + } else { + response.data = std::make_shared(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 #include +#include #include #include #include #include -#include -#include - -#include -#include - -#if defined(_WINDOWS) && !defined(S_ISDIR) -#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) -#endif namespace { @@ -27,9 +19,8 @@ public: Impl(ActorRef) {} void request(const std::string& url, ActorRef req) { - Response response; - if (!acceptsURL(url)) { + Response response; response.error = std::make_unique(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::Reason::NotFound); - } else if (result == -1 && errno == ENOENT) { - response.error = std::make_unique(Response::Error::Reason::NotFound); - } else { - auto data = util::readFile(path); - if (!data) { - response.error = std::make_unique( - Response::Error::Reason::Other, - std::string("Cannot read file ") + path); - } else { - response.data = std::make_shared(std::move(*data)); - } - } - - req.invoke(&FileSourceRequest::setResponse, response); + requestLocalFile(path, std::move(req)); } - }; LocalFileSource::LocalFileSource() -- cgit v1.2.1