mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
Add a TemporaryFile constructor accepting the file extension
This commit is contained in:
parent
4a40edc0c3
commit
63035b2d74
@ -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<v_char8[]> 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<FileHandle>(constructRandomFilename(tmpDirectory, randomWordSizeBytes)))
|
||||
: m_handle(std::make_shared<FileHandle>(constructRandomFilename(tmpDirectory, randomWordSizeBytes, "tmp")))
|
||||
{}
|
||||
|
||||
TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName)
|
||||
: m_handle(std::make_shared<FileHandle>(File::concatDirAndName(tmpDirectory, tmpFileName)))
|
||||
{}
|
||||
|
||||
TemporaryFile::TemporaryFile(const oatpp::String& tmpDirectory, v_int32 randomWordSizeBytes, const oatpp::String& extension)
|
||||
: m_handle(std::make_shared<FileHandle>(constructRandomFilename(tmpDirectory, randomWordSizeBytes, extension)))
|
||||
{}
|
||||
|
||||
std::shared_ptr<data::stream::OutputStream> TemporaryFile::openOutputStream() {
|
||||
if(m_handle) {
|
||||
return std::make_shared<data::stream::FileOutputStream>(m_handle->fileName->c_str(), "wb", m_handle);
|
||||
|
@ -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<FileHandle> m_handle;
|
||||
public:
|
||||
@ -86,6 +86,17 @@ public:
|
||||
*/
|
||||
TemporaryFile(const oatpp::String& tmpDirectory, const oatpp::String& tmpFileName);
|
||||
|
||||
/**
|
||||
* Constructor. <br>
|
||||
* Create temporary file with a random name and specified extension in the `tmpDirectory`. <br>
|
||||
* The actual file will be created only after first write to that file. <br>
|
||||
* 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. <br>
|
||||
* *Note: stream also captures file-handle. The temporary file won't be deleted until the stream is deleted.*
|
||||
|
Loading…
Reference in New Issue
Block a user