Skip to content

Commit

Permalink
merge to main (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
nam20485 authored Oct 24, 2023
2 parents ceab10f + a40e909 commit 505e084
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 49 deletions.
10 changes: 9 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ out/
.git/
.gitignore
.gitattributes
Dockerfile
Dockerfile*
docker-compose.yml
*.code-workspace
vcpkg_installed/
ssl/
out/
cmake-build-*/
build/
.idea/
Testing/
*.code-workspace
6 changes: 3 additions & 3 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ jobs:

- name: Create Release Variables
run: |
ls -al ${{ github.workspace }}/artifacts
export RELEASE_VERSION="${{vars.RELEASE_VERSION_PREFIX}}.${{github.run_number}}"
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV
export RELEASE_TAG="v${RELEASE_VERSION}"
Expand All @@ -219,12 +218,13 @@ jobs:
github-token: "${{ secrets.GITHUB_TOKEN }}"
script: |
try {
const createResponse = await github.rest.repos.createRelease({
const createResponse = await github.rest.repos.createRelease({
generate_release_notes: true,
name: process.env.RELEASE_NAME,
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: process.env.RELEASE_TAG,
body: require('fs').readFileSync('${{ github.workspace }}/release/release-body.md', 'utf8'),
target_commitish: '${{ github.ref_name }}'
});
Expand All @@ -235,7 +235,7 @@ jobs:
];
for (const filename of filenames) {
const artifactsPath = 'artifacts';
const artifactsPath = '${{ github.workspace }}/artifacts';
const filePath = artifactsPath +'/' + filename;
const uploadResponse = await github.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile_OdbDesignServer
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ RUN cmake --build --preset linux-release

# much smaller runtime image
FROM debian:bookworm-20231009-slim AS run
LABEL org.opencontainers.image.source=https://github.com/nam20485/OdbDesign
LABEL org.opencontainers.image.authors="https://github.com/nam20485"
LABEL org.opencontainers.image.description = "The OdbDesign Docker image runs the OdbDesignServer REST API server executable, listening on port 8888."
LABEL org.opencontainers.image.licenses=MIT
EXPOSE 8888

RUN mkdir --parents /OdbDesign/bin
WORKDIR /OdbDesign/bin
Expand Down
5 changes: 5 additions & 0 deletions OdbDesignLib/Design.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace Odb::Lib::ProductModel
return true;
}

std::shared_ptr<FileModel::Design::FileArchive> Design::GetFileModel() const
{
return m_pFileModel;
}

bool Design::BuildComponents()
{
if (m_pFileModel == nullptr) return false;
Expand Down
2 changes: 2 additions & 0 deletions OdbDesignLib/Design.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Odb::Lib::ProductModel
bool Build(std::string path);
bool Build(std::shared_ptr<FileModel::Design::FileArchive> pFileModel);

std::shared_ptr<FileModel::Design::FileArchive> GetFileModel() const;

typedef std::vector<std::shared_ptr<Design>> Vector;
typedef std::map<std::string, std::shared_ptr<Design>> StringMap;

Expand Down
57 changes: 52 additions & 5 deletions OdbDesignLib/DesignCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <exception>

using namespace Utils;
using namespace std::filesystem;

namespace Odb::Lib
{
Expand Down Expand Up @@ -57,14 +58,56 @@ namespace Odb::Lib
return m_fileArchivesByName[designName];
}

std::vector<std::string> DesignCache::getLoadedDesignNames(const std::string& filter) const
{
std::vector<std::string> loadedDesigns;
for (const auto& kv : m_designsByName)
{
loadedDesigns.push_back(kv.second->GetFileModel()->GetFilename());
}
return loadedDesigns;
}

std::vector<std::string> DesignCache::getLoadedFileArchiveNames(const std::string& filter) const
{
std::vector<std::string> loadedFileArchives;
for (const auto& kv : m_fileArchivesByName)
{
loadedFileArchives.push_back(kv.second->GetFilename());
}
return loadedFileArchives;
}

std::vector<std::string> DesignCache::getUnloadedNames(const std::string& filter) const
{
std::vector<std::string> unloadedNames;

path dir(m_directory);
for (const auto& entry : directory_iterator(dir))
{
if (entry.is_regular_file())
{
unloadedNames.push_back(entry.path().filename().string());
}
}

return unloadedNames;
}

bool DesignCache::isQueryValid(const std::string& query) const
{
return false;
}

void DesignCache::Clear()
{
m_fileArchivesByName.clear();
m_designsByName.clear();
}

std::shared_ptr<ProductModel::Design> DesignCache::LoadDesign(const std::string& designName)
{
{
// no FileArchive with the same name is loaded, so load the Design from file
std::filesystem::path dir(m_directory);

for (const auto& entry : std::filesystem::directory_iterator(dir))
Expand All @@ -73,11 +116,15 @@ namespace Odb::Lib
{
if (entry.path().stem() == designName)
{
auto pDesign = std::make_shared<ProductModel::Design>();
if (pDesign->Build(entry.path().string()))
auto pFileModel = GetFileArchive(designName);
if (pFileModel != nullptr)
{
m_designsByName.emplace(designName, pDesign);
return pDesign;
auto pDesign = std::make_shared<ProductModel::Design>();
if (pDesign->Build(pFileModel))
{
m_designsByName.emplace(designName, pDesign);
return pDesign;
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions OdbDesignLib/DesignCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace Odb::Lib
std::shared_ptr<ProductModel::Design> GetDesign(const std::string& designName);
std::shared_ptr<FileModel::Design::FileArchive> GetFileArchive(const std::string& designName);

std::vector<std::string> getLoadedDesignNames(const std::string& filter = "") const;
std::vector<std::string> getLoadedFileArchiveNames(const std::string& filter = "") const;
std::vector<std::string> getUnloadedNames(const std::string& filter = "") const;

bool isQueryValid(const std::string& query) const;

void Clear();

private:
Expand Down
18 changes: 16 additions & 2 deletions OdbDesignLib/FileArchive.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "FileArchive.h"
#include "FileArchive.h"
#include <filesystem>
#include "ArchiveExtractor.h"
#include "MiscInfoFile.h"
#include <iostream>
#include "Logger.h"

using namespace Utils;
using namespace std::filesystem;

namespace Odb::Lib::FileModel::Design
{
Expand All @@ -30,7 +30,15 @@ namespace Odb::Lib::FileModel::Design
return m_productName;
}

const StepDirectory::StringMap& FileArchive::GetStepsByName() const { return m_stepsByName; }
std::string FileArchive::GetFilename() const
{
return path(m_path).filename().string();
}

const StepDirectory::StringMap& FileArchive::GetStepsByName() const
{
return m_stepsByName;
}

bool FileArchive::ParseFileModel()
{
Expand Down Expand Up @@ -99,6 +107,7 @@ namespace Odb::Lib::FileModel::Design
if (!std::filesystem::exists(path)) return false;
else if (!std::filesystem::is_directory(path)) return false;

// TODO: this should use path.stem() instead of path.filename()
m_productName = path.filename().string();

auto stepsPath = path / "steps";
Expand Down Expand Up @@ -167,6 +176,11 @@ namespace Odb::Lib::FileModel::Design
return m_matrixFile;
}

const StandardFontsFile& FileArchive::GetStandardFontsFile() const
{
return m_standardFontsFile;
}

//const EdaDataFile& FileModel::GetStepEdaDataFile(std::string stepName) const
//{
// auto findIt = m_stepsByName.find(stepName);
Expand Down
1 change: 1 addition & 0 deletions OdbDesignLib/FileArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Odb::Lib::FileModel::Design

std::string GetPath() const;
std::string GetProductName() const;
std::string GetFilename() const;

const StepDirectory::StringMap& GetStepsByName() const;
const MiscInfoFile& GetMiscInfoFile() const;
Expand Down
2 changes: 1 addition & 1 deletion OdbDesignLib/IOdbApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Odb::Lib
virtual ~IOdbApp() {}

virtual const OdbDesignArgs& args() const = 0;
virtual DesignCache& design_cache() = 0;
virtual DesignCache& designs() = 0;

virtual Utils::ExitCode Run() = 0;

Expand Down
2 changes: 1 addition & 1 deletion OdbDesignLib/OdbAppBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Odb::Lib
return m_commandLineArgs;
}

DesignCache& OdbAppBase::design_cache()
DesignCache& OdbAppBase::designs()
{
return m_designCache;
}
Expand Down
2 changes: 1 addition & 1 deletion OdbDesignLib/OdbAppBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Odb::Lib
virtual ~OdbAppBase();

const OdbDesignArgs& args() const override;
DesignCache& design_cache() override;
DesignCache& designs() override;

virtual Utils::ExitCode Run() override;

Expand Down
73 changes: 63 additions & 10 deletions OdbDesignServer/Controllers/FileUploadController.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "FileUploadController.h"

using namespace std::filesystem;

namespace Odb::App::Server
{
FileUploadController::FileUploadController(Odb::Lib::IOdbServerApp& serverApp)
Expand All @@ -8,7 +10,7 @@ namespace Odb::App::Server
}
void FileUploadController::register_routes()
{
CROW_ROUTE(m_serverApp.crow_app(), "/files/upload/<string>").methods(crow::HTTPMethod::POST)
CROW_ROUTE(m_serverApp.crow_app(), "/designs/upload/<string>").methods(crow::HTTPMethod::POST)
([&](const crow::request& req, std::string filename)
{
const auto& contentType = req.get_header_value("Content-Type");
Expand All @@ -25,22 +27,68 @@ namespace Odb::App::Server
return crow::response(crow::status::BAD_REQUEST, "incorrect content type");
}
});
}

CROW_ROUTE(m_serverApp.crow_app(), "/designs/list").methods(crow::HTTPMethod::GET)
([&](const crow::request& req)
{
auto designNames = m_serverApp.designs().getUnloadedNames();
if (designNames.empty())
{
return crow::response(crow::status::NOT_FOUND, "no designs found");
}

crow::json::wvalue jsonResponse;
jsonResponse["names"] = designNames;

#if defined(_DEBUG)
auto j = jsonResponse.dump();
#endif

return crow::response(jsonResponse);
});

CROW_ROUTE(m_serverApp.crow_app(), "/designs/list/<string>").methods(crow::HTTPMethod::GET)
([&](const crow::request& req, std::string query)
{
if (! m_serverApp.designs().isQueryValid(query))
{
return crow::response(crow::status::BAD_REQUEST, "invalid query");
}

auto designNames = m_serverApp.designs().getUnloadedNames(query);
if (designNames.empty())
{
return crow::response(crow::status::NOT_FOUND, "no matching design names found");
}

crow::json::wvalue jsonResponse;
jsonResponse["names"] = designNames;

#if defined(_DEBUG)
auto j = jsonResponse.dump();
#endif

return crow::response(jsonResponse);
});
}

crow::response FileUploadController::handleOctetStreamUpload(const std::string& filename, const crow::request& req)
{
// TODO: generate random temp filename
const auto tempFilename = "temp.upload";
std::ofstream outfile(tempFilename, std::ofstream::binary);
const auto tempPath = temp_directory_path() / std::tmpnam(nullptr);

Check warning on line 77 in OdbDesignServer/Controllers/FileUploadController.cpp

View workflow job for this annotation

GitHub Actions / CMake-Multi-Platform-Build (macos-12, macos-release)

'tmpnam' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead. [-Wdeprecated-declarations]

Check warning on line 77 in OdbDesignServer/Controllers/FileUploadController.cpp

View workflow job for this annotation

GitHub Actions / CMake-Multi-Platform-Build (windows-2022, x64-release)

'tmpnam': This function or variable may be unsafe. Consider using tmpnam_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Check warning on line 77 in OdbDesignServer/Controllers/FileUploadController.cpp

View workflow job for this annotation

GitHub Actions / CMake-Multi-Platform-Build (macos-12, macos-release)

'tmpnam' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead. [-Wdeprecated-declarations]

Check warning on line 77 in OdbDesignServer/Controllers/FileUploadController.cpp

View workflow job for this annotation

GitHub Actions / CMake-Multi-Platform-Build (windows-2022, x64-release)

'tmpnam': This function or variable may be unsafe. Consider using tmpnam_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
std::ofstream outfile(tempPath, std::ofstream::binary);
outfile << req.body;
outfile.close();

// TODO: sanitize filename
std::filesystem::path finalPath(m_serverApp.args().designsDir());
finalPath /= filename;
std::filesystem::rename(tempFilename, finalPath);
// TODO: sanitize provided filename
auto safeName = sanitizeFilename(filename);

return crow::response(crow::status::OK);
path finalPath(m_serverApp.args().designsDir());
finalPath /= safeName;
rename(tempPath, finalPath);

std::string responseBody = "{ \"filename\": \"" + safeName + "\" }";

return crow::response(crow::status::OK, responseBody);
}

crow::response FileUploadController::handleMultipartFormUpload(const std::string& filename, const crow::request& req)
Expand Down Expand Up @@ -99,4 +147,9 @@ namespace Odb::App::Server
}
return crow::response(200);
}

std::string FileUploadController::sanitizeFilename(const std::string& filename) const
{
return filename;
}
}
2 changes: 2 additions & 0 deletions OdbDesignServer/Controllers/FileUploadController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ namespace Odb::App::Server
crow::response handleOctetStreamUpload(const std::string& filename, const crow::request& req);
crow::response handleMultipartFormUpload(const std::string& filename, const crow::request& req);

std::string sanitizeFilename(const std::string& filename) const;

};
}
Loading

0 comments on commit 505e084

Please sign in to comment.