PeriDyno 1.0.0
Loading...
Searching...
No Matches
WFileWidget.cpp
Go to the documentation of this file.
1#include "WFileWidget.h"
2
4{
5 layout = this->setLayout(std::make_unique<Wt::WVBoxLayout>());
6 layout->setContentsMargins(0, 0, 0, 0);
7 //layout->setSpacing(0);
8
9 setValue(field);
10 mfield = field;
11
12 upload = layout->addWidget(std::make_unique<Wt::WFileUpload>());
13 //uploadButton = layout->addWidget(std::make_unique<Wt::WPushButton>("Upload"));
14
15 upload->setMultiple(true);
16
17 mfilename->changed().connect(this, &WFileWidget::updateField);
18 //uploadButton->checked().connect(upload, &Wt::WFileUpload::upload);
19 //uploadButton->checked().connect(uploadButton, &Wt::WPushButton::disable);
20 upload->changed().connect(upload, &Wt::WFileUpload::upload);
21 upload->uploaded().connect(this, &WFileWidget::uploadFile);
22 upload->fileTooLarge().connect(this, &WFileWidget::fileTooLarge);
23}
24
28
30{
31 if (upload->uploadedFiles().size() > 0)
32 {
33 for (const auto& file : upload->uploadedFiles())
34 {
35 Wt::log("info") << file.spoolFileName();
36 std::string savePath = getAssetPath() + "WebUploadFiles/" + file.clientFileName();
37 std::string tempFilePath = file.spoolFileName();
38 std::ifstream src(tempFilePath, std::ios::binary);
39 std::ofstream dst(savePath, std::ios::binary);
40
41 dst << src.rdbuf();
42
43 if (dst)
44 {
45 Wt::WMessageBox::show("Success", "File save path WebUploadFiles/.", Wt::StandardButton::Ok);
46 }
47 else
48 {
49 Wt::WMessageBox::show("Error", "File save failure.", Wt::StandardButton::Ok);
50 }
51
52 src.close();
53 dst.close();
54 }
55 }
56}
57
59{
60 Wt::WMessageBox::show("Error", "File Too Large!", Wt::StandardButton::Ok);
61}
62
63std::string WFileWidget::shortFilePath(std::string str)
64{
65 std::string removeStr = getAssetPath();
66 size_t pos = str.find(removeStr);
67 while (pos != std::string::npos)
68 {
69 str.erase(pos, removeStr.length());
70 pos = str.find(removeStr);
71 }
72 return str;
73}
74
75bool WFileWidget::hasFile(std::string path)
76{
77 if (std::filesystem::exists(path) && std::filesystem::is_regular_file(path))
78 {
79 return true;
80 }
81 else
82 {
83 return false;
84 }
85 return false;
86}
87
89{
91 if (f == nullptr)
92 return;
93
94 mfilename = layout->addWidget(std::make_unique<Wt::WLineEdit>());
95 std::string filepath = shortFilePath(f->getValue().string());
96 mfilename->setText(filepath);
97}
98
100{
102 if (f == nullptr)
103 {
104 return;
105 }
106 auto path = f->getValue();
107 std::string filePath = getAssetPath() + mfilename->text().toUTF8();
108 if (hasFile(filePath))
109 {
110 path.set_path(filePath);
111 f->setValue(path);
112 f->update();
113 changeValue_.emit(1);
114 }
115 else
116 {
117 Wt::WMessageBox::show("Error", "file does not exist!", Wt::StandardButton::Ok);
118 return;
119 }
120
121}
std::string shortFilePath(std::string str)
void setValue(dyno::FBase *)
dyno::FBase * mfield
Definition WFileWidget.h:36
WFileWidget(dyno::FBase *)
void updateField()
Wt::WLineEdit * mfilename
Definition WFileWidget.h:38
bool hasFile(std::string)
Wt::WVBoxLayout * layout
Definition WFileWidget.h:37
Wt::Signal< int > changeValue_
Definition WFileWidget.h:41
void uploadFile()
void fileTooLarge()
Wt::WFileUpload * upload
Definition WFileWidget.h:40
T getValue()
Definition Field.h:130
TA * cast(TB *b)
Definition Typedef.inl:286