NAWA 0.8
Web Application Framework for C++
File.cpp
Go to the documentation of this file.
1
6/*
7 * Copyright (C) 2019-2021 Tobias Flaig.
8 *
9 * This file is part of nawa.
10 *
11 * nawa is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License,
13 * version 3, as published by the Free Software Foundation.
14 *
15 * nawa is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <cstring>
25#include <fstream>
26#include <nawa/Exception.h>
27#include <nawa/request/File.h>
28
29using namespace nawa;
30using namespace std;
31
32struct File::Data {
33 string filename;
34 string contentType;
35 // TODO better solution can be used when shifting fcgi handler to use MimeMultipart
36 shared_ptr<char[]> dataPtr;
37 size_t size = 0;
38
39 Data(shared_ptr<char[]> dataPtr, size_t size) : dataPtr(move(dataPtr)), size(size) {}
40};
41
43
45
47
49
51
53
54NAWA_COMPLEX_DATA_ACCESSORS_IMPL(File, contentType, string)
55
56File::File(std::shared_ptr<char[]> dataPtr, size_t size) {
57 data = make_unique<Data>(move(dataPtr), size);
58}
59
60File::File(std::string const& data) {
61 shared_ptr<char[]> dataPtr(new char[data.size()]);
62 memcpy(dataPtr.get(), data.c_str(), data.size());
63 this->data = make_unique<Data>(move(dataPtr), data.size());
64}
65
66size_t File::size() const noexcept {
67 return data->size;
68}
69
70string File::toString() const {
71 return string(data->dataPtr.get(), data->size);
72}
73
74void File::writeToDisk(string const& path) const {
75 ofstream outfile;
76 ios_base::iostate exceptionMask = outfile.exceptions() | ios::failbit;
77 outfile.exceptions(exceptionMask);
78 try {
79 outfile.open(path, ofstream::out | ofstream::binary);
80 outfile.write(data->dataPtr.get(), data->size);
81 outfile.close();
82 } catch (ios_base::failure const& e) {
83 throw Exception(__PRETTY_FUNCTION__, 1, "Could not write file to disk.", e.what());
84 }
85}
Exception class that can be used by apps to catch errors resulting from nawa function calls.
Container for (especially POST-submitted) files.
std::string toString() const
Definition: File.cpp:70
size_t size() const noexcept
Definition: File.cpp:66
void writeToDisk(std::string const &path) const
Definition: File.cpp:74
#define NAWA_DEFAULT_DESTRUCTOR_IMPL(Class)
Definition: macros.h:36
#define NAWA_COPY_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:46
#define NAWA_COMPLEX_DATA_ACCESSORS_IMPL(Class, Member, Type)
Definition: macros.h:144
#define NAWA_MOVE_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:98
#define NAWA_COPY_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:56
#define NAWA_MOVE_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:88
Definition: AppInit.h:31