add a cmake option to switch between std and boost filesystem

This commit is contained in:
Chunting Gu
2021-05-27 11:15:08 +08:00
parent 39719c4ded
commit df96f969bc
31 changed files with 227 additions and 195 deletions
+4 -7
View File
@@ -3,14 +3,11 @@
#include <iostream>
#include "boost/algorithm/string.hpp"
#include "boost/filesystem/operations.hpp"
#include "json/json.h"
#include "book_json.h"
namespace bfs = boost::filesystem;
BookClient::BookClient(const std::string& url, int timeout)
: url_(url), session_(timeout) {
// Default Content-Type for requests with a body.
@@ -127,7 +124,7 @@ bool BookClient::Delete(const std::string& id) {
}
}
bool BookClient::GetPhoto(const std::string& id, const bfs::path& path) {
bool BookClient::GetPhoto(const std::string& id, const webcc::fs::path& path) {
try {
auto r = session_.Send(WEBCC_GET(url_).
Path("books").Path(id).Path("photo")(),
@@ -147,7 +144,7 @@ bool BookClient::GetPhoto(const std::string& id, const bfs::path& path) {
}
}
bool BookClient::SetPhoto(const std::string& id, const bfs::path& path) {
bool BookClient::SetPhoto(const std::string& id, const webcc::fs::path& path) {
try {
if (!CheckPhoto(path)) {
return false;
@@ -169,12 +166,12 @@ bool BookClient::SetPhoto(const std::string& id, const bfs::path& path) {
}
}
bool BookClient::CheckPhoto(const bfs::path& photo) {
bool BookClient::CheckPhoto(const webcc::fs::path& photo) {
if (photo.empty()) {
return false;
}
if (!bfs::is_regular_file(photo) || !bfs::exists(photo)) {
if (!webcc::fs::is_regular_file(photo) || !webcc::fs::exists(photo)) {
return false;
}
+4 -5
View File
@@ -4,11 +4,10 @@
#include <list>
#include <string>
#include "boost/filesystem/path.hpp"
#include "json/json-forwards.h"
#include "webcc/client_session.h"
#include "webcc/fs.h"
#include "book.h"
@@ -29,13 +28,13 @@ public:
bool Delete(const std::string& id);
// Get photo, save to the given path.
bool GetPhoto(const std::string& id, const boost::filesystem::path& path);
bool GetPhoto(const std::string& id, const webcc::fs::path& path);
// Set photo using the file of the given path.
bool SetPhoto(const std::string& id, const boost::filesystem::path& path);
bool SetPhoto(const std::string& id, const webcc::fs::path& path);
private:
bool CheckPhoto(const boost::filesystem::path& photo);
bool CheckPhoto(const webcc::fs::path& photo);
// Check HTTP response status.
bool CheckStatus(webcc::ResponsePtr response, int expected_status);
+3 -6
View File
@@ -1,13 +1,10 @@
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "webcc/fs.h"
#include "webcc/logger.h"
#include "book_client.h"
namespace bfs = boost::filesystem;
// -----------------------------------------------------------------------------
void PrintSeparator() {
@@ -39,8 +36,8 @@ int main(int argc, char* argv[]) {
std::string url = argv[1];
bfs::path photo_dir = argv[2];
if (!bfs::is_directory(photo_dir) || !bfs::exists(photo_dir)) {
webcc::fs::path photo_dir = argv[2];
if (!webcc::fs::is_directory(photo_dir) || !webcc::fs::exists(photo_dir)) {
std::cerr << "Invalid photo dir!" << std::endl;
return 1;
}
+6 -9
View File
@@ -1,14 +1,11 @@
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "webcc/fs.h"
#include "webcc/logger.h"
#include "webcc/server.h"
#include "views.h"
namespace bfs = boost::filesystem;
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "usage: book_server <port> <upload_dir>" << std::endl;
@@ -21,16 +18,16 @@ int main(int argc, char* argv[]) {
std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));
bfs::path upload_dir = argv[2];
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
webcc::fs::path upload_dir = argv[2];
if (!webcc::fs::is_directory(upload_dir) || !webcc::fs::exists(upload_dir)) {
std::cerr << "Invalid upload dir!" << std::endl;
return 1;
}
// Add a sub-dir for book photos.
bfs::path photo_dir = upload_dir / "books";
if (!bfs::exists(photo_dir)) {
bfs::create_directory(photo_dir);
webcc::fs::path photo_dir = upload_dir / "books";
if (!webcc::fs::exists(photo_dir)) {
webcc::fs::create_directory(photo_dir);
}
std::cout << "Book photos will be saved to: " << photo_dir << std::endl;
+8 -12
View File
@@ -1,7 +1,5 @@
#include "views.h"
#include "boost/filesystem/operations.hpp"
#include "json/json.h"
#include "webcc/response_builder.h"
@@ -10,8 +8,6 @@
#include "book_db.h"
#include "book_json.h"
namespace bfs = boost::filesystem;
// -----------------------------------------------------------------------------
static BookDB g_book_db;
@@ -57,7 +53,7 @@ webcc::ResponsePtr BookListView::Post(webcc::RequestPtr request) {
// -----------------------------------------------------------------------------
BookDetailView::BookDetailView(bfs::path photo_dir)
BookDetailView::BookDetailView(webcc::fs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
@@ -125,8 +121,8 @@ webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
// Delete the photo from file system.
if (!photo_name.empty()) {
boost::system::error_code ec;
bfs::remove(photo_dir_ / photo_name, ec);
webcc::fs::error_code ec;
webcc::fs::remove(photo_dir_ / photo_name, ec);
}
return webcc::ResponseBuilder{}.OK()();
@@ -134,7 +130,7 @@ webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
// -----------------------------------------------------------------------------
BookPhotoView::BookPhotoView(bfs::path photo_dir)
BookPhotoView::BookPhotoView(webcc::fs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
@@ -164,8 +160,8 @@ webcc::ResponsePtr BookPhotoView::Get(webcc::RequestPtr request) {
return webcc::ResponseBuilder{}.NotFound()();
}
bfs::path photo_path = photo_dir_ / book.photo;
if (!bfs::exists(photo_path)) {
webcc::fs::path photo_path = photo_dir_ / book.photo;
if (!webcc::fs::exists(photo_path)) {
return webcc::ResponseBuilder{}.NotFound()();
}
@@ -219,8 +215,8 @@ webcc::ResponsePtr BookPhotoView::Delete(webcc::RequestPtr request) {
}
// Error handling is simplified.
boost::system::error_code ec;
bfs::remove(photo_dir_ / book.photo, ec);
webcc::fs::error_code ec;
webcc::fs::remove(photo_dir_ / book.photo, ec);
return webcc::ResponseBuilder{}.OK()();
}
+5 -6
View File
@@ -1,8 +1,7 @@
#ifndef VIEWS_H_
#define VIEWS_H_
#include "boost/filesystem/path.hpp"
#include "webcc/fs.h"
#include "webcc/view.h"
// -----------------------------------------------------------------------------
@@ -25,7 +24,7 @@ private:
// URL: /books/{id}
class BookDetailView : public webcc::View {
public:
explicit BookDetailView(boost::filesystem::path photo_dir);
explicit BookDetailView(webcc::fs::path photo_dir);
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
@@ -40,7 +39,7 @@ private:
webcc::ResponsePtr Delete(webcc::RequestPtr request);
private:
boost::filesystem::path photo_dir_;
webcc::fs::path photo_dir_;
};
// -----------------------------------------------------------------------------
@@ -48,7 +47,7 @@ private:
// URL: /books/{id}/photo
class BookPhotoView : public webcc::View {
public:
explicit BookPhotoView(boost::filesystem::path photo_dir);
explicit BookPhotoView(webcc::fs::path photo_dir);
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
@@ -68,7 +67,7 @@ private:
webcc::ResponsePtr Delete(webcc::RequestPtr request);
private:
boost::filesystem::path photo_dir_;
webcc::fs::path photo_dir_;
};
#endif // VIEWS_H_
+3 -6
View File
@@ -2,13 +2,10 @@
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "webcc/client_session.h"
#include "webcc/fs.h"
#include "webcc/logger.h"
namespace bfs = boost::filesystem;
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cout << "Usage: form_client <upload_dir> [url]" << std::endl;
@@ -29,7 +26,7 @@ int main(int argc, char* argv[]) {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
const bfs::path upload_dir(argv[1]);
const webcc::fs::path upload_dir(argv[1]);
std::string url;
if (argc == 3) {
@@ -38,7 +35,7 @@ int main(int argc, char* argv[]) {
url = "http://httpbin.org/post";
}
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
if (!webcc::fs::is_directory(upload_dir) || !webcc::fs::exists(upload_dir)) {
std::cerr << "Invalid upload dir!" << std::endl;
return 1;
}
-2
View File
@@ -5,8 +5,6 @@
#include "webcc/client_session.h"
#include "webcc/logger.h"
namespace bfs = boost::filesystem;
int main(int argc, char* argv[]) {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);