From 63035b2d74a8eb86f63e54e42c176464a065da80 Mon Sep 17 00:00:00 2001 From: Sergii Ladniuk Date: Thu, 2 Dec 2021 17:58:16 +0200 Subject: [PATCH] Add a TemporaryFile constructor accepting the file extension --- src/oatpp/core/data/resource/TemporaryFile.cpp | 13 ++++++++++--- src/oatpp/core/data/resource/TemporaryFile.hpp | 13 ++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/oatpp/core/data/resource/TemporaryFile.cpp b/src/oatpp/core/data/resource/TemporaryFile.cpp index 1700c96d..9333fbed 100644 --- a/src/oatpp/core/data/resource/TemporaryFile.cpp +++ b/src/oatpp/core/data/resource/TemporaryFile.cpp @@ -39,26 +39,33 @@ TemporaryFile::FileHandle::~FileHandle() { } } -oatpp::String TemporaryFile::constructRandomFilename(const oatpp::String& dir, v_int32 randomWordSizeBytes) { +oatpp::String TemporaryFile::constructRandomFilename(const oatpp::String &dir, v_int32 randomWordSizeBytes, const oatpp::String &extension) { std::unique_ptr buff(new v_char8[randomWordSizeBytes]); utils::random::Random::randomBytes(buff.get(), randomWordSizeBytes); data::stream::BufferOutputStream s(randomWordSizeBytes * 2 + 4); encoding::Hex::encode(&s, buff.get(), randomWordSizeBytes, encoding::Hex::ALPHABET_LOWER); - s << ".tmp"; + if (extension->at(0) != '.') { + s << "."; + } + s << extension; return File::concatDirAndName(dir, s.toString()); } TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, v_int32 randomWordSizeBytes) - : m_handle(std::make_shared(constructRandomFilename(tmpDirectory, randomWordSizeBytes))) + : m_handle(std::make_shared(constructRandomFilename(tmpDirectory, randomWordSizeBytes, "tmp"))) {} TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName) : m_handle(std::make_shared(File::concatDirAndName(tmpDirectory, tmpFileName))) {} +TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, v_int32 randomWordSizeBytes, const oatpp::String& extension) + : m_handle(std::make_shared(constructRandomFilename(tmpDirectory, randomWordSizeBytes, extension))) +{} + std::shared_ptr TemporaryFile::openOutputStream() { if(m_handle) { return std::make_shared(m_handle->fileName->c_str(), "wb", m_handle); diff --git a/src/oatpp/core/data/resource/TemporaryFile.hpp b/src/oatpp/core/data/resource/TemporaryFile.hpp index 2bbb2f08..1c967e67 100644 --- a/src/oatpp/core/data/resource/TemporaryFile.hpp +++ b/src/oatpp/core/data/resource/TemporaryFile.hpp @@ -58,7 +58,7 @@ private: }; private: - static oatpp::String constructRandomFilename(const oatpp::String& dir, v_int32 randomWordSizeBytes); + static oatpp::String constructRandomFilename(const oatpp::String &dir, v_int32 randomWordSizeBytes, const oatpp::String &extension); private: std::shared_ptr m_handle; public: @@ -86,6 +86,17 @@ public: */ TemporaryFile(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName); + /** + * Constructor.
+ * Create temporary file with a random name and specified extension in the `tmpDirectory`.
+ * The actual file will be created only after first write to that file.
+ * Example of the generated random file name: `f7c6ecd44024ff31.txt`. + * @param tmpDirectory - directory where to create a temporary file. + * @param randomWordSizeBytes - number of random bytes to generate file name. + * @param extension - extension of the temporary file, e.g. txt or .txt + */ + TemporaryFile(const oatpp::String& tmpDirectory, v_int32 randomWordSizeBytes, const oatpp::String& extension); + /** * Open output stream to a temporary file.
* *Note: stream also captures file-handle. The temporary file won't be deleted until the stream is deleted.*