diff --git a/api b/api index a9ac550..4753d01 160000 --- a/api +++ b/api @@ -1 +1 @@ -Subproject commit a9ac5508d86af0d319bd2be9036afde5224429d9 +Subproject commit 4753d01056e135aeec672424fd0572218fc1f6ee diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 492f64b..5db44dc 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -15,6 +15,8 @@ add_library( STATIC filetransfer_service_upload.cpp filetransfer_service_download.cpp + filetransfer_service_delete.cpp + filetransfer_service_info.cpp sha1_digest.cpp exception_handling.cpp ) diff --git a/src/lib/filetransfer_service.h b/src/lib/filetransfer_service.h index 038fd62..bbb8b9e 100644 --- a/src/lib/filetransfer_service.h +++ b/src/lib/filetransfer_service.h @@ -87,6 +87,31 @@ class FileTransferServiceImpl final : public ::ansys::api::tools::filetransfer:: ::ansys::api::tools::filetransfer::v1::UploadFileRequest>* stream ) -> ::grpc::Status override; + /** + * @brief Implements the "DeleteFile" operation. + * @param context Server context to use. + * @param request Request to process. + * @param response Response to populate. + */ + virtual auto DeleteFile( + ::grpc::ServerContext* context, + const ::ansys::api::tools::filetransfer::v1::DeleteFileRequest* request, + ::ansys::api::tools::filetransfer::v1::DeleteFileResponse* response + ) -> ::grpc::Status override; + + /** + * @brief Implements the "GetFileInfo" operation. + * @param context Server context to use. + * @param request Request to process. + * @param response Response to populate. + */ + virtual auto GetFileInfo( + ::grpc::ServerContext* context, + const ::ansys::api::tools::filetransfer::v1::GetFileInfoRequest* + request, + ::ansys::api::tools::filetransfer::v1::GetFileInfoResponse* response + ) -> ::grpc::Status override; + private: }; diff --git a/src/lib/filetransfer_service_delete.cpp b/src/lib/filetransfer_service_delete.cpp new file mode 100644 index 0000000..769f578 --- /dev/null +++ b/src/lib/filetransfer_service_delete.cpp @@ -0,0 +1,79 @@ +// Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates. +// SPDX-License-Identifier: MIT +// +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "filetransfer_service.h" + +#ifdef _MSC_VER +#pragma warning(push, 3) +#else +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + +#include +#include +#include + +#ifdef _MSC_VER +#pragma warning(pop) +#else +#pragma GCC diagnostic pop +#endif + +#include "exception_handling.h" +#include "exception_types.h" + +namespace file_transfer { + +auto FileTransferServiceImpl::DeleteFile( + ::grpc::ServerContext* /* unused */, + const ::ansys::api::tools::filetransfer::v1::DeleteFileRequest* request, + ::ansys::api::tools::filetransfer::v1::DeleteFileResponse* /* unused */ +) -> ::grpc::Status { + + return exceptions::convert_exceptions_to_status_codes( + std::function([&]() { + const auto& filename = request->filename(); + const auto file_path = boost::filesystem::path{filename}; + + BOOST_LOG_TRIVIAL(info) << "Got deletion request for file: " + << file_path.generic_string(); + + if (!boost::filesystem::exists(file_path)) { + throw exceptions::failed_precondition( + "File does not exist: " + filename + ); + } + if (!boost::filesystem::is_regular_file(file_path)) { + throw exceptions::failed_precondition( + "Path " + filename + " is not a regular file." + ); + } + + boost::filesystem::remove(file_path); + BOOST_LOG_TRIVIAL(info) + << "Deleted file: " << file_path.generic_string(); + }) + ); +} + +} // namespace file_transfer diff --git a/src/lib/filetransfer_service_info.cpp b/src/lib/filetransfer_service_info.cpp new file mode 100644 index 0000000..6424e12 --- /dev/null +++ b/src/lib/filetransfer_service_info.cpp @@ -0,0 +1,80 @@ +// Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates. +// SPDX-License-Identifier: MIT +// +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "filetransfer_service.h" + +#ifdef _MSC_VER +#pragma warning(push, 3) +#else +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma warning(pop) +#else +#pragma GCC diagnostic pop +#endif + +#include "exception_handling.h" +#include "exception_types.h" +#include "sha1_digest.h" + +namespace file_transfer { + +auto FileTransferServiceImpl::GetFileInfo( + ::grpc::ServerContext* /* unused */, + const ::ansys::api::tools::filetransfer::v1::GetFileInfoRequest* request, + ::ansys::api::tools::filetransfer::v1::GetFileInfoResponse* response +) -> ::grpc::Status { + + return exceptions::convert_exceptions_to_status_codes( + std::function([&]() { + const auto& filename = request->filename(); + const auto file_path = boost::filesystem::path{filename}; + const auto file_exists = boost::filesystem::exists(file_path); + response->set_exists(file_exists); + + if (file_exists) { + auto& file_info = *(response->mutable_file_info()); + if (request->compute_sha1_checksum()) { + const auto hex_digest = + detail::get_sha1_hex_digest(file_path); + file_info.mutable_sha1()->set_hex_digest(hex_digest); + } + + file_info.set_name(file_path.string()); + const std::size_t file_size = + boost::filesystem::file_size(file_path); + file_info.set_size(boost::numeric_cast(file_size) + ); + } + }) + ); +} + +} // namespace file_transfer