Create path if it doesn't exist (#1095)

This commit is contained in:
An Tao 2021-11-29 18:15:18 +08:00 committed by GitHub
parent bc18f56d57
commit f522d2d70e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -41,6 +41,17 @@ int HttpFileImpl::save(const std::string &path) const
fsPath = fsUploadPath / fsPath; fsPath = fsUploadPath / fsPath;
} }
filesystem::path fsFileName(utils::toNativePath(fileName_)); 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); return saveTo(fsPath / fsFileName);
} }
int HttpFileImpl::saveAs(const std::string &fileName) const int HttpFileImpl::saveAs(const std::string &fileName) const
@ -55,13 +66,18 @@ int HttpFileImpl::saveAs(const std::string &fileName) const
HttpAppFrameworkImpl::instance().getUploadPath())); HttpAppFrameworkImpl::instance().getUploadPath()));
fsFileName = fsUploadPath / fsFileName; 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; drogon::error_code err;
filesystem::create_directories(fsFileName.parent_path(), err); filesystem::create_directories(fsFileName.parent_path(), err);
if (err) if (err)
{
LOG_SYSERR;
return -1; return -1;
} }
}
return saveTo(fsFileName); return saveTo(fsFileName);
} }
int HttpFileImpl::saveTo(const filesystem::path &pathAndFileName) const int HttpFileImpl::saveTo(const filesystem::path &pathAndFileName) const