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 
29 using namespace nawa;
30 using namespace std;
31 
32 struct File::Data {
33  string filename;
34  string contentType;
35  shared_ptr<char[]> dataPtr;
36  size_t size = 0;
37 
38  Data(shared_ptr<char[]> dataPtr, size_t size) : dataPtr(move(dataPtr)), size(size) {}
39 };
40 
42 
44 
46 
48 
50 
51 NAWA_COMPLEX_DATA_ACCESSORS_IMPL(File, filename, string)
52 
53 NAWA_COMPLEX_DATA_ACCESSORS_IMPL(File, contentType, string)
54 
55 File::File(std::shared_ptr<char[]> dataPtr, size_t size) {
56  data = make_unique<Data>(move(dataPtr), size);
57 }
58 
59 File::File(std::string const& data) {
60  shared_ptr<char[]> dataPtr(new char[data.size()]);
61  memcpy(dataPtr.get(), data.c_str(), data.size());
62  this->data = make_unique<Data>(move(dataPtr), data.size());
63 }
64 
65 size_t File::size() const noexcept {
66  return data->size;
67 }
68 
69 string File::toString() const {
70  return string(data->dataPtr.get(), data->size);
71 }
72 
73 void File::writeToDisk(string const& path) const {
74  ofstream outfile;
75  ios_base::iostate exceptionMask = outfile.exceptions() | ios::failbit;
76  outfile.exceptions(exceptionMask);
77  try {
78  outfile.open(path, ofstream::out | ofstream::binary);
79  outfile.write(data->dataPtr.get(), data->size);
80  outfile.close();
81  } catch (ios_base::failure const& e) {
82  throw Exception(__PRETTY_FUNCTION__, 1, "Could not write file to disk.", e.what());
83  }
84 }
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:69
size_t size() const noexcept
Definition: File.cpp:65
File(std::shared_ptr< char[]> dataPtr, size_t size)
Definition: File.cpp:55
void writeToDisk(std::string const &path) const
Definition: File.cpp:73
#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