ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
upload-files.cpp
Go to the documentation of this file.
1 /*
2  upload-files.cpp - ESP3D http handle
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../../include/esp3d_config.h"
21 #if defined (HTTP_FEATURE) && defined(FILESYSTEM_FEATURE)
22 #include "../http_server.h"
23 #if defined (ARDUINO_ARCH_ESP32)
24 #include <WebServer.h>
25 #endif //ARDUINO_ARCH_ESP32
26 #if defined (ARDUINO_ARCH_ESP8266)
27 #include <ESP8266WebServer.h>
28 #endif //ARDUINO_ARCH_ESP8266
29 #include "../../filesystem/esp_filesystem.h"
30 #include "../../authentication/authentication_service.h"
31 //FS files uploader handle
32 void HTTP_Server::FSFileupload ()
33 {
34  //get authentication status
36  static String filename;
37  static ESP_File fsUploadFile;
38  //Guest cannot upload - only admin
39  if (auth_level == LEVEL_GUEST) {
40  pushError(ESP_ERROR_AUTHENTICATION, "Upload rejected", 401);
41  _upload_status=UPLOAD_STATUS_FAILED;
42  } else {
43  HTTPUpload& upload = _webserver->upload();
44  String upload_filename = upload.filename;
45  if ((_upload_status != UPLOAD_STATUS_FAILED) || (upload.status == UPLOAD_FILE_START)) {
46 
47  //Upload start
48  if (upload.status == UPLOAD_FILE_START) {
49  _upload_status = UPLOAD_STATUS_ONGOING;
50  if (upload_filename[0] != '/') {
51  filename = "/" + upload_filename;
52  } else {
53  filename = upload.filename;
54  }
55  //Sanity check
56  if (ESP_FileSystem::exists (filename.c_str()) ) {
57  ESP_FileSystem::remove (filename.c_str());
58  }
59  if (fsUploadFile.isOpen() ) {
60  fsUploadFile.close();
61  }
62  String sizeargname = upload.filename + "S";
63  if (_webserver->hasArg (sizeargname.c_str()) ) {
65  size_t filesize = _webserver->arg (sizeargname.c_str()).toInt();
66  if (freespace < filesize ) {
67  _upload_status=UPLOAD_STATUS_FAILED;
68  pushError(ESP_ERROR_NOT_ENOUGH_SPACE, "Upload rejected");
69  }
70  }
71  if (_upload_status!=UPLOAD_STATUS_FAILED) {
72  //create file
73  fsUploadFile = ESP_FileSystem::open(filename.c_str(), ESP_FILE_WRITE);
74  //check If creation succeed
75  if (fsUploadFile) {
76  //if yes upload is started
77  _upload_status= UPLOAD_STATUS_ONGOING;
78  } else {
79  //if no set cancel flag
80  _upload_status=UPLOAD_STATUS_FAILED;
81  pushError(ESP_ERROR_FILE_CREATION, "File creation failed");
82  }
83 
84  }
85  //Upload write
86  } else if(upload.status == UPLOAD_FILE_WRITE) {
87  //check if file is available and no error
88  if(fsUploadFile && _upload_status == UPLOAD_STATUS_ONGOING) {
89  //no error so write post date
90  if(upload.currentSize != fsUploadFile.write(upload.buf, upload.currentSize)) {
91  //we have a problem set flag UPLOAD_STATUS_FAILED
92  _upload_status=UPLOAD_STATUS_FAILED;
93  pushError(ESP_ERROR_FILE_WRITE, "File write failed");
94  }
95  } else {
96  //we have a problem set flag UPLOAD_STATUS_FAILED
97  _upload_status=UPLOAD_STATUS_FAILED;
98  pushError(ESP_ERROR_FILE_WRITE, "File write failed");
99  }
100  //Upload end
101  } else if(upload.status == UPLOAD_FILE_END) {
102  //check if file is still open
103  if(fsUploadFile) {
104  //close it
105  fsUploadFile.close();
106  //check size
107  String sizeargname = upload.filename + "S";
108  //fsUploadFile = ESP_FileSystem::open (filename, ESP_FILE_READ);
109  uint32_t filesize = fsUploadFile.size();
110  _upload_status = UPLOAD_STATUS_SUCCESSFUL;
111  if (_webserver->hasArg (sizeargname.c_str()) ) {
112  if (_webserver->arg (sizeargname.c_str()) != String(filesize)) {
113  _upload_status = UPLOAD_STATUS_FAILED;
114  pushError(ESP_ERROR_SIZE, "File upload failed");
115  }
116  }
117  if (_upload_status == UPLOAD_STATUS_ONGOING) {
118  _upload_status = UPLOAD_STATUS_SUCCESSFUL;
119  }
120  } else {
121  //we have a problem set flag UPLOAD_STATUS_FAILED
122  _upload_status=UPLOAD_STATUS_FAILED;
123  pushError(ESP_ERROR_FILE_CLOSE, "File close failed");
124  }
125  //Upload cancelled
126  } else {
127  if (_upload_status == UPLOAD_STATUS_ONGOING) {
128  _upload_status = UPLOAD_STATUS_FAILED;
129  }
130  }
131  }
132  }
133 
134  if(_upload_status == UPLOAD_STATUS_FAILED) {
135  cancelUpload();
136  if(fsUploadFile) {
137  fsUploadFile.close();
138  }
139  if (auth_level != LEVEL_GUEST) {
140  if (ESP_FileSystem::exists (filename.c_str())) {
141  ESP_FileSystem::remove (filename.c_str());
142  }
143  }
144  }
145 }
146 #endif //HTTP_FEATURE && FILESYSTEM_FEATURE
ESP_File::close
void close()
ESP_ERROR_SIZE
#define ESP_ERROR_SIZE
Definition: defines.h:117
ESP_FILE_WRITE
#define ESP_FILE_WRITE
Definition: defines.h:121
ESP_FileSystem::usedBytes
static size_t usedBytes()
ESP_ERROR_FILE_WRITE
#define ESP_ERROR_FILE_WRITE
Definition: defines.h:107
ESP_FileSystem::remove
static bool remove(const char *path)
ESP_FileSystem::totalBytes
static size_t totalBytes()
ESP_ERROR_FILE_CLOSE
#define ESP_ERROR_FILE_CLOSE
Definition: defines.h:111
AuthenticationService::authenticated_level
static level_authenticate_type authenticated_level(const char *pwd=nullptr)
Definition: authentication_service.cpp:61
ESP_ERROR_FILE_CREATION
#define ESP_ERROR_FILE_CREATION
Definition: defines.h:106
UPLOAD_STATUS_FAILED
@ UPLOAD_STATUS_FAILED
Definition: http_server.h:39
UPLOAD_STATUS_ONGOING
@ UPLOAD_STATUS_ONGOING
Definition: http_server.h:42
ESP_File::isOpen
bool isOpen()
Definition: esp_filesystem.cpp:118
LEVEL_GUEST
@ LEVEL_GUEST
Definition: authentication_service.h:26
ESP_ERROR_NOT_ENOUGH_SPACE
#define ESP_ERROR_NOT_ENOUGH_SPACE
Definition: defines.h:109
ESP_File
Definition: esp_filesystem.h:30
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP_ERROR_AUTHENTICATION
#define ESP_ERROR_AUTHENTICATION
Definition: defines.h:105
ESP_File::write
size_t write(uint8_t i)
Definition: esp_filesystem.cpp:156
ESP_File::size
size_t size()
Definition: esp_filesystem.cpp:138
ESP_FileSystem::exists
static bool exists(const char *path)
ESP_FileSystem::open
static ESP_File open(const char *path, uint8_t mode=ESP_FILE_READ)
UPLOAD_STATUS_SUCCESSFUL
@ UPLOAD_STATUS_SUCCESSFUL
Definition: http_server.h:41