ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
upload-SD-files.cpp
Go to the documentation of this file.
1 /*
2  upload-SD-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(SD_DEVICE)
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_sd.h"
30 #include "../../authentication/authentication_service.h"
31 //SD files uploader handle
32 void HTTP_Server::SDFileupload ()
33 {
34  //get authentication status
36  static String filename;
37  static ESP_SDFile fsUploadFile;
38  static uint32_t timecheck;
39  //Guest cannot upload - only admin
40  if (auth_level == LEVEL_GUEST) {
41  pushError(ESP_ERROR_AUTHENTICATION, "Upload rejected", 401);
42  _upload_status=UPLOAD_STATUS_FAILED;
43  } else {
44  HTTPUpload& upload = _webserver->upload();
45  String upload_filename = upload.filename;
46  if ((_upload_status != UPLOAD_STATUS_FAILED) || (upload.status == UPLOAD_FILE_START)) {
47 
48  //Upload start
49  if (upload.status == UPLOAD_FILE_START) {
50  timecheck= millis();
51  _upload_status = UPLOAD_STATUS_ONGOING;
52  if (upload_filename[0] != '/') {
53  filename = "/" + upload_filename;
54  } else {
55  filename = upload.filename;
56  }
57  //Sanity check
58  if (ESP_SD::exists (filename.c_str()) ) {
59  ESP_SD::remove (filename.c_str());
60  }
61  if (fsUploadFile.isOpen() ) {
62  fsUploadFile.close();
63  }
64  String sizeargname = upload.filename + "S";
65  //TODO add busy state and handle it for upload
66  if (ESP_SD::getState(true) != ESP_SDCARD_IDLE) {
67  _upload_status=UPLOAD_STATUS_FAILED;
68  }
69  if (_upload_status!=UPLOAD_STATUS_FAILED) {
70  if (_webserver->hasArg (sizeargname.c_str()) ) {
71  size_t freespace = ESP_SD::totalBytes() - ESP_SD::usedBytes();
72  size_t filesize = _webserver->arg (sizeargname.c_str()).toInt();
73  if (freespace < filesize ) {
74  _upload_status=UPLOAD_STATUS_FAILED;
75  pushError(ESP_ERROR_NOT_ENOUGH_SPACE, "Upload rejected");
76  }
77  }
78  }
79  if (_upload_status!=UPLOAD_STATUS_FAILED) {
80  //create file
81  fsUploadFile = ESP_SD::open(filename.c_str(), ESP_FILE_WRITE);
82  //check If creation succeed
83  if (fsUploadFile) {
84  //if yes upload is started
85  _upload_status= UPLOAD_STATUS_ONGOING;
86  } else {
87  //if no set cancel flag
88  _upload_status=UPLOAD_STATUS_FAILED;
89  pushError(ESP_ERROR_FILE_CREATION, "File creation failed");
90  }
91 
92  }
93  //Upload write
94  } else if(upload.status == UPLOAD_FILE_WRITE) {
95  //check if file is available and no error
96  if(fsUploadFile && _upload_status == UPLOAD_STATUS_ONGOING) {
97  //no error so write post date
98  if(upload.currentSize != fsUploadFile.write(upload.buf, upload.currentSize)) {
99  //we have a problem set flag UPLOAD_STATUS_FAILED
100  _upload_status=UPLOAD_STATUS_FAILED;
101  pushError(ESP_ERROR_FILE_WRITE, "File write failed");
102  }
103  } else {
104  //we have a problem set flag UPLOAD_STATUS_FAILED
105  _upload_status=UPLOAD_STATUS_FAILED;
106  pushError(ESP_ERROR_FILE_WRITE, "File write failed");
107  }
108  //Upload end
109  } else if(upload.status == UPLOAD_FILE_END) {
110  uint32_t filesize = 0;
111  //check if file is still open
112  if(fsUploadFile) {
113  //close it
114  fsUploadFile.close();
115  //check size
116  String sizeargname = upload.filename + "S";
117  //fsUploadFile = ESP_SD::open (filename, ESP_FILE_READ);
118  filesize = fsUploadFile.size();
119  _upload_status = UPLOAD_STATUS_SUCCESSFUL;
120  if (_webserver->hasArg (sizeargname.c_str()) ) {
121  if (_webserver->arg (sizeargname.c_str()) != String(filesize)) {
122  _upload_status = UPLOAD_STATUS_FAILED;
123  pushError(ESP_ERROR_SIZE, "File upload failed");
124  }
125  }
126  if (_upload_status == UPLOAD_STATUS_ONGOING) {
127  _upload_status = UPLOAD_STATUS_SUCCESSFUL;
128  }
129  } else {
130  //we have a problem set flag UPLOAD_STATUS_FAILED
131  _upload_status=UPLOAD_STATUS_FAILED;
132  pushError(ESP_ERROR_FILE_CLOSE, "File close failed");
133  }
134  Serial.print(filesize);
135  Serial.print(" B in ");
136  Serial.print((millis()-timecheck) / 1000);
137  Serial.println(" sec");
138  //Upload cancelled
139  } else {
140  if (_upload_status == UPLOAD_STATUS_ONGOING) {
141  _upload_status = UPLOAD_STATUS_FAILED;
142  }
143  }
144  }
145  }
146 
147  if(_upload_status == UPLOAD_STATUS_FAILED) {
148  cancelUpload();
149  if(fsUploadFile) {
150  fsUploadFile.close();
151  }
152  if (auth_level != LEVEL_GUEST) {
153  if (ESP_SD::exists (filename.c_str())) {
154  ESP_SD::remove (filename.c_str());
155  }
156  }
157  }
158 }
159 #endif //HTTP_FEATURE && SD_DEVICE
ESP_SD::open
static ESP_SDFile open(const char *path, uint8_t mode=ESP_FILE_READ)
ESP_ERROR_SIZE
#define ESP_ERROR_SIZE
Definition: defines.h:117
ESP_FILE_WRITE
#define ESP_FILE_WRITE
Definition: defines.h:121
ESP_SD::totalBytes
static uint64_t totalBytes()
ESP_SDFile
Definition: esp_sd.h:31
ESP_ERROR_FILE_WRITE
#define ESP_ERROR_FILE_WRITE
Definition: defines.h:107
ESP_SD::remove
static bool remove(const char *path)
ESP_SDFile::write
size_t write(uint8_t i)
Definition: esp_sd.cpp:148
ESP_SDFile::size
size_t size()
Definition: esp_sd.cpp:130
ESP_ERROR_FILE_CLOSE
#define ESP_ERROR_FILE_CLOSE
Definition: defines.h:111
ESP_SDCARD_IDLE
#define ESP_SDCARD_IDLE
Definition: defines.h:80
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
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_SD::getState
static uint8_t getState(bool refresh)
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP_SDFile::isOpen
bool isOpen()
Definition: esp_sd.cpp:110
ESP_SDFile::close
void close()
ESP_ERROR_AUTHENTICATION
#define ESP_ERROR_AUTHENTICATION
Definition: defines.h:105
ESP_SD::exists
static bool exists(const char *path)
ESP_SD::usedBytes
static uint64_t usedBytes()
UPLOAD_STATUS_SUCCESSFUL
@ UPLOAD_STATUS_SUCCESSFUL
Definition: http_server.h:41