From f522d2d70efeb5e1e7e027322e4ca1379322de6b Mon Sep 17 00:00:00 2001 From: An Tao Date: Mon, 29 Nov 2021 18:15:18 +0800 Subject: [PATCH] Create path if it doesn't exist (#1095) --- lib/src/HttpFileImpl.cc | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/src/HttpFileImpl.cc b/lib/src/HttpFileImpl.cc index 68b8725b..ad9c692a 100644 --- a/lib/src/HttpFileImpl.cc +++ b/lib/src/HttpFileImpl.cc @@ -41,6 +41,17 @@ int HttpFileImpl::save(const std::string &path) const fsPath = fsUploadPath / fsPath; } filesystem::path fsFileName(utils::toNativePath(fileName_)); + if (!filesystem::exists(fsPath)) + { + LOG_TRACE << "create path:" << fsPath; + drogon::error_code err; + filesystem::create_directories(fsPath, err); + if (err) + { + LOG_SYSERR; + return -1; + } + } return saveTo(fsPath / fsFileName); } int HttpFileImpl::saveAs(const std::string &fileName) const @@ -55,12 +66,17 @@ int HttpFileImpl::saveAs(const std::string &fileName) const HttpAppFrameworkImpl::instance().getUploadPath())); fsFileName = fsUploadPath / fsFileName; } - if (fsFileName.has_parent_path()) + if (fsFileName.has_parent_path() && + !filesystem::exists(fsFileName.parent_path())) { + LOG_TRACE << "create path:" << fsFileName.parent_path(); drogon::error_code err; filesystem::create_directories(fsFileName.parent_path(), err); if (err) + { + LOG_SYSERR; return -1; + } } return saveTo(fsFileName); }