Make functions that create and return, remove the temporary dir.
This commit is contained in:
parent
399ee40571
commit
36ce7176c8
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "cgi.hpp"
|
#include "cgi.hpp"
|
||||||
|
#include "files.hpp"
|
||||||
#include "fs-compat.hpp"
|
#include "fs-compat.hpp"
|
||||||
|
|
||||||
#include <cgicc/Cgicc.h>
|
#include <cgicc/Cgicc.h>
|
||||||
|
@ -61,14 +62,8 @@ entry_type parse_formdata()
|
||||||
{
|
{
|
||||||
throw runtime_error{"Filesize too big"};
|
throw runtime_error{"Filesize too big"};
|
||||||
}
|
}
|
||||||
string filepath{fs::temp_directory_path()
|
|
||||||
/ "fediblock-backend-XXXXXX"};
|
|
||||||
if (mkstemp(&filepath[0]) == -1) // mkstemp() modifies filepath.
|
|
||||||
{
|
|
||||||
throw runtime_error{"Could not open temporary file: "
|
|
||||||
+ filepath};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
string filepath{get_tmpdir() / screenshot->getFilename()};
|
||||||
ofstream file{filepath, ios::binary};
|
ofstream file{filepath, ios::binary};
|
||||||
if (file.good())
|
if (file.good())
|
||||||
{
|
{
|
||||||
|
|
58
src/files.cpp
Normal file
58
src/files.cpp
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
/* This file is part of FediBlock-backend.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, version 3.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "files.hpp"
|
||||||
|
#include "fs-compat.hpp"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace FediBlock
|
||||||
|
{
|
||||||
|
|
||||||
|
using std::runtime_error;
|
||||||
|
using std::string;
|
||||||
|
using std::uintmax_t;
|
||||||
|
|
||||||
|
string _tmpdir;
|
||||||
|
|
||||||
|
fs::path get_tmpdir()
|
||||||
|
{
|
||||||
|
if (_tmpdir.empty())
|
||||||
|
{
|
||||||
|
_tmpdir = fs::temp_directory_path() / "fediblock-backend-XXXXXX";
|
||||||
|
if (mkdtemp(&_tmpdir[0]) == nullptr) // mkdtemp() modifies _tmpdir.
|
||||||
|
{
|
||||||
|
throw runtime_error{"Could not create temporary directory: "
|
||||||
|
+ _tmpdir};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _tmpdir;
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_tmpdir()
|
||||||
|
{
|
||||||
|
if (fs::remove_all(get_tmpdir()) == static_cast<uintmax_t>(-1))
|
||||||
|
{
|
||||||
|
throw runtime_error{"Couldn't remove temporary directory: "
|
||||||
|
+ get_tmpdir().string()};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace FediBlock
|
33
src/files.hpp
Normal file
33
src/files.hpp
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/* This file is part of FediBlock-backend.
|
||||||
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, version 3.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FEDIBLOCK_BACKEND_FILES_HPP
|
||||||
|
#define FEDIBLOCK_BACKEND_FILES_HPP
|
||||||
|
|
||||||
|
#include "fs-compat.hpp"
|
||||||
|
|
||||||
|
namespace FediBlock
|
||||||
|
{
|
||||||
|
|
||||||
|
// Creates and returns the temporary directory.
|
||||||
|
[[nodiscard]] fs::path get_tmpdir();
|
||||||
|
|
||||||
|
// Remove the temporary directory. Throws runtime_error on error.
|
||||||
|
void remove_tmpdir();
|
||||||
|
|
||||||
|
} // namespace FediBlock
|
||||||
|
|
||||||
|
#endif // FEDIBLOCK_BACKEND_FILES_HPP
|
13
src/main.cpp
13
src/main.cpp
|
@ -15,12 +15,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "cgi.hpp"
|
#include "cgi.hpp"
|
||||||
|
#include "files.hpp"
|
||||||
#include "fs-compat.hpp"
|
#include "fs-compat.hpp"
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
using std::cerr;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
using std::exception;
|
||||||
|
|
||||||
using namespace FediBlock;
|
using namespace FediBlock;
|
||||||
|
|
||||||
|
@ -57,7 +61,14 @@ int main()
|
||||||
|
|
||||||
print_debug(entry);
|
print_debug(entry);
|
||||||
|
|
||||||
fs::remove(entry.screenshot_filepath);
|
try
|
||||||
|
{
|
||||||
|
remove_tmpdir();
|
||||||
|
}
|
||||||
|
catch (const exception &e)
|
||||||
|
{
|
||||||
|
cerr << e.what() << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user