switch back to boost::filesystem to avoid the requirement of c++17

This commit is contained in:
Chunting Gu
2020-09-07 19:01:31 +08:00
parent e7f88da2b2
commit 84476f75f7
31 changed files with 175 additions and 192 deletions
+8 -7
View File
@@ -2,12 +2,16 @@
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "json/json.h"
#include "webcc/string.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.
@@ -124,8 +128,7 @@ bool BookClient::Delete(const std::string& id) {
}
}
bool BookClient::GetPhoto(const std::string& id,
const std::filesystem::path& path) {
bool BookClient::GetPhoto(const std::string& id, const bfs::path& path) {
try {
auto r = session_.Send(WEBCC_GET(url_).
Path("books").Path(id).Path("photo")(),
@@ -145,8 +148,7 @@ bool BookClient::GetPhoto(const std::string& id,
}
}
bool BookClient::SetPhoto(const std::string& id,
const std::filesystem::path& path) {
bool BookClient::SetPhoto(const std::string& id, const bfs::path& path) {
try {
if (!CheckPhoto(path)) {
return false;
@@ -168,13 +170,12 @@ bool BookClient::SetPhoto(const std::string& id,
}
}
bool BookClient::CheckPhoto(const std::filesystem::path& photo) {
bool BookClient::CheckPhoto(const bfs::path& photo) {
if (photo.empty()) {
return false;
}
if (!std::filesystem::is_regular_file(photo) ||
!std::filesystem::exists(photo)) {
if (!bfs::is_regular_file(photo) || !bfs::exists(photo)) {
return false;
}
+5 -4
View File
@@ -1,10 +1,11 @@
#ifndef BOOK_CLIENT_H_
#define BOOK_CLIENT_H_
#include <filesystem>
#include <list>
#include <string>
#include "boost/filesystem/path.hpp"
#include "json/json-forwards.h"
#include "webcc/client_session.h"
@@ -28,13 +29,13 @@ public:
bool Delete(const std::string& id);
// Get photo, save to the given path.
bool GetPhoto(const std::string& id, const std::filesystem::path& path);
bool GetPhoto(const std::string& id, const boost::filesystem::path& path);
// Set photo using the file of the given path.
bool SetPhoto(const std::string& id, const std::filesystem::path& path);
bool SetPhoto(const std::string& id, const boost::filesystem::path& path);
private:
bool CheckPhoto(const std::filesystem::path& photo);
bool CheckPhoto(const boost::filesystem::path& photo);
// Check HTTP response status.
bool CheckStatus(webcc::ResponsePtr response, int expected_status);
+6 -4
View File
@@ -1,10 +1,13 @@
#include <filesystem>
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "webcc/logger.h"
#include "book_client.h"
namespace bfs = boost::filesystem;
// -----------------------------------------------------------------------------
void PrintSeparator() {
@@ -36,9 +39,8 @@ int main(int argc, char* argv[]) {
std::string url = argv[1];
std::filesystem::path photo_dir = argv[2];
if (!std::filesystem::is_directory(photo_dir) ||
!std::filesystem::exists(photo_dir)) {
bfs::path photo_dir = argv[2];
if (!bfs::is_directory(photo_dir) || !bfs::exists(photo_dir)) {
std::cerr << "Invalid photo dir!" << std::endl;
return 1;
}
+9 -7
View File
@@ -1,11 +1,14 @@
#include <filesystem>
#include <iostream>
#include "boost/filesystem/operations.hpp"
#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;
@@ -18,17 +21,16 @@ int main(int argc, char* argv[]) {
std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));
std::filesystem::path upload_dir = argv[2];
if (!std::filesystem::is_directory(upload_dir) ||
!std::filesystem::exists(upload_dir)) {
bfs::path upload_dir = argv[2];
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
std::cerr << "Invalid upload dir!" << std::endl;
return 1;
}
// Add a sub-dir for book photos.
std::filesystem::path photo_dir = upload_dir / "books";
if (!std::filesystem::exists(photo_dir)) {
std::filesystem::create_directory(photo_dir);
bfs::path photo_dir = upload_dir / "books";
if (!bfs::exists(photo_dir)) {
bfs::create_directory(photo_dir);
}
std::cout << "Book photos will be saved to: " << photo_dir << std::endl;
+12 -8
View File
@@ -1,5 +1,7 @@
#include "views.h"
#include "boost/filesystem/operations.hpp"
#include "json/json.h"
#include "webcc/response_builder.h"
@@ -8,6 +10,8 @@
#include "book_db.h"
#include "book_json.h"
namespace bfs = boost::filesystem;
// -----------------------------------------------------------------------------
static BookDB g_book_db;
@@ -53,7 +57,7 @@ webcc::ResponsePtr BookListView::Post(webcc::RequestPtr request) {
// -----------------------------------------------------------------------------
BookDetailView::BookDetailView(std::filesystem::path photo_dir)
BookDetailView::BookDetailView(bfs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
@@ -121,8 +125,8 @@ webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
// Delete the photo from file system.
if (!photo_name.empty()) {
std::error_code ec;
std::filesystem::remove(photo_dir_ / photo_name, ec);
boost::system::error_code ec;
bfs::remove(photo_dir_ / photo_name, ec);
}
return webcc::ResponseBuilder{}.OK()();
@@ -130,7 +134,7 @@ webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
// -----------------------------------------------------------------------------
BookPhotoView::BookPhotoView(std::filesystem::path photo_dir)
BookPhotoView::BookPhotoView(bfs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
@@ -160,8 +164,8 @@ webcc::ResponsePtr BookPhotoView::Get(webcc::RequestPtr request) {
return webcc::ResponseBuilder{}.NotFound()();
}
std::filesystem::path photo_path = photo_dir_ / book.photo;
if (!std::filesystem::exists(photo_path)) {
bfs::path photo_path = photo_dir_ / book.photo;
if (!bfs::exists(photo_path)) {
return webcc::ResponseBuilder{}.NotFound()();
}
@@ -215,8 +219,8 @@ webcc::ResponsePtr BookPhotoView::Delete(webcc::RequestPtr request) {
}
// Error handling is simplified.
std::error_code ec;
std::filesystem::remove(photo_dir_ / book.photo, ec);
boost::system::error_code ec;
bfs::remove(photo_dir_ / book.photo, ec);
return webcc::ResponseBuilder{}.OK()();
}
+5 -5
View File
@@ -1,7 +1,7 @@
#ifndef VIEWS_H_
#define VIEWS_H_
#include <filesystem>
#include "boost/filesystem/path.hpp"
#include "webcc/view.h"
@@ -25,7 +25,7 @@ private:
// URL: /books/{id}
class BookDetailView : public webcc::View {
public:
explicit BookDetailView(std::filesystem::path photo_dir);
explicit BookDetailView(boost::filesystem::path photo_dir);
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
@@ -40,7 +40,7 @@ private:
webcc::ResponsePtr Delete(webcc::RequestPtr request);
private:
std::filesystem::path photo_dir_;
boost::filesystem::path photo_dir_;
};
// -----------------------------------------------------------------------------
@@ -48,7 +48,7 @@ private:
// URL: /books/{id}/photo
class BookPhotoView : public webcc::View {
public:
explicit BookPhotoView(std::filesystem::path photo_dir);
explicit BookPhotoView(boost::filesystem::path photo_dir);
webcc::ResponsePtr Handle(webcc::RequestPtr request) override;
@@ -68,7 +68,7 @@ private:
webcc::ResponsePtr Delete(webcc::RequestPtr request);
private:
std::filesystem::path photo_dir_;
boost::filesystem::path photo_dir_;
};
#endif // VIEWS_H_
+5 -4
View File
@@ -1,12 +1,13 @@
// A client posting multipart form data.
#include <filesystem>
#include <iostream>
#include "boost/filesystem/operations.hpp"
#include "webcc/client_session.h"
#include "webcc/logger.h"
namespace sfs = std::filesystem;
namespace bfs = boost::filesystem;
int main(int argc, char* argv[]) {
if (argc < 2) {
@@ -23,7 +24,7 @@ int main(int argc, char* argv[]) {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
const sfs::path upload_dir(argv[1]);
const bfs::path upload_dir(argv[1]);
std::string url;
if (argc == 3) {
@@ -32,7 +33,7 @@ int main(int argc, char* argv[]) {
url = "http://httpbin.org/post";
}
if (!sfs::is_directory(upload_dir) || !sfs::exists(upload_dir)) {
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
std::cerr << "Invalid upload dir!" << std::endl;
return 1;
}