From 46c0d1014aaf6731fa2bb185ccb10233f55797da Mon Sep 17 00:00:00 2001 From: Nils Date: Sun, 21 Jan 2024 23:35:29 +0100 Subject: [PATCH] file uploading and deleting working --- e-paper-project/e-paper-project.ino | 1 - e-paper-project/include/sd_card.h | 6 +- e-paper-project/sd_card.cpp | 40 +++--- e-paper-project/web_server.cpp | 214 ++-------------------------- 4 files changed, 40 insertions(+), 221 deletions(-) diff --git a/e-paper-project/e-paper-project.ino b/e-paper-project/e-paper-project.ino index 7609ffe..c3f5dba 100644 --- a/e-paper-project/e-paper-project.ino +++ b/e-paper-project/e-paper-project.ino @@ -33,7 +33,6 @@ void setup(){ SD_card_fs::init_SD(5); Web_server::init_wifi(); Web_server::init_server(); - } void loop() diff --git a/e-paper-project/include/sd_card.h b/e-paper-project/include/sd_card.h index 5544add..3bf689d 100644 --- a/e-paper-project/include/sd_card.h +++ b/e-paper-project/include/sd_card.h @@ -28,8 +28,10 @@ namespace SD_card_fs fs_error_t create_dir(const char* path); fs_error_t remove_dir(const char* path); std::unique_ptr get_file(const char* path); - fs_error_t write_file(const char* path, const char* data); // Make sure data is null-terminated - fs_error_t append_file(const char* path, const char* data); // Make sure data is null-terminated + fs_error_t write_to_file(const char* path, const uint8_t* data, uint32_t len); // Make sure data is null-terminated + fs_error_t close_file(); + fs_error_t open_file(const char* path); + fs_error_t append_to_file(const uint8_t* data, uint32_t len); // Make sure data is null-terminated fs_error_t rename_file(const char* path1, const char* path2); fs_error_t delete_file(const char* path); }; diff --git a/e-paper-project/sd_card.cpp b/e-paper-project/sd_card.cpp index e6a940f..f2bce73 100644 --- a/e-paper-project/sd_card.cpp +++ b/e-paper-project/sd_card.cpp @@ -1,5 +1,6 @@ #include "include/sd_card.h" +static File file; namespace SD_card_fs { fs_error_t init_SD(const uint16_t cs) @@ -51,39 +52,46 @@ namespace SD_card_fs return std::unique_ptr{file}; } - fs_error_t write_file(const char* path, const char* data) + fs_error_t open_file(const char* path) { - File file = SD.open(path, FILE_WRITE); + file = SD.open(path, FILE_WRITE); if(!file){ Serial.printf("failed to open file: %s\n", path); return FS_FAIL; } + return FS_OK; + } - if(file.print(data)){ + fs_error_t close_file() + { + file.close(); + return FS_OK; + } + + fs_error_t write_to_file(const char* path, const uint8_t* data, uint32_t len) + { + open_file(path); + + if(file.write(&data[0], len)){ Serial.printf("File written: %s\n", path); - file.close(); + close_file(); return FS_OK; } else { Serial.printf("Write failed: %s\n", path); - file.close(); + close_file(); return FS_FAIL; } } - fs_error_t append_file(const char* path, const char* data){ - File file = SD.open(path, FILE_APPEND); - if(!file){ - Serial.printf("failed to open file for appending: %s\n", path); - return FS_FAIL; - } - if(file.print(data)){ - Serial.printf("Message appended: %s\n", path); - file.close(); + + fs_error_t append_to_file(const uint8_t* data, uint32_t len) + { + if(file.write(&data[0], len)){ + Serial.printf("Message appended\n"); return FS_OK; } else { - Serial.printf("Append failed: %s\n", path); - file.close(); + Serial.printf("Append failed\n"); return FS_FAIL; } } diff --git a/e-paper-project/web_server.cpp b/e-paper-project/web_server.cpp index eeb8fb9..fcb660c 100644 --- a/e-paper-project/web_server.cpp +++ b/e-paper-project/web_server.cpp @@ -1,5 +1,6 @@ #include "include/web_server.h" #include "include/web_HTML.h" +#include "include/sd_card.h" #ifdef ESP8266 static ESP8266WebServer server{SERVER_PORT}; @@ -38,22 +39,31 @@ static void handle_file_upload() if (uploadfile.status == UPLOAD_FILE_START) { String filename {uploadfile.filename}; + if (!filename.startsWith("/")) filename = "/" + filename; Serial.print("Upload File Name: "); Serial.println(filename); - // SD.remove(filename); // Remove a previous version, otherwise data is appended the file again - // UploadFile = SD.open(filename, FILE_WRITE); // Open the file for writing in SPIFFS (create it, if doesn't exist) + SD_card_fs::delete_file(filename.c_str()); // Remove a previous version, otherwise data is appended the file again + SD_card_fs::open_file(filename.c_str()); // Open the file for writing in SPIFFS (create it, if doesn't exist) } else if (uploadfile.status == UPLOAD_FILE_WRITE) { Serial.println("Writing file somewhere"); + SD_card_fs::append_to_file(uploadfile.buf, uploadfile.currentSize); } else if (uploadfile.status == UPLOAD_FILE_END) { Serial.print("Upload Size: "); Serial.println(uploadfile.totalSize); + SD_card_fs::close_file(); + + home_page(); + } + else + { + server.send(500, "text/plain", "500: couldn't create file"); } } @@ -93,203 +103,3 @@ namespace Web_server }; - - - -/* -void Web_server::SD_dir() -{ - if (SD_present) - { - File root = SD.open("/"); - if (root) - { - root.rewindDirectory(); - SendHTML_Header(); - webpage += F("

SD Card Contents


"); - webpage += F(""); - webpage += F(""); - printDirectory("/", 0); - webpage += F("
Name/TypeType File/DirFile Size
"); - SendHTML_Content(); - root.close(); - } - else - { - SendHTML_Header(); - webpage += F("

No Files Found

"); - } - append_page_footer(); - SendHTML_Content(); - SendHTML_Stop(); // Stop is needed because no content length was sent - } - else - ReportSDNotPresent(); -} - - void printDirectory(const char *dirname, uint8_t levels) - { - File root = SD.open(dirname); - #ifdef ESP8266 - root.rewindDirectory(); // Only needed for ESP8266 - #endif - if (!root) - { - return; - } - if (!root.isDirectory()) - { - return; - } - File file = root.openNextFile(); - while (file) - { - if (webpage.length() > 1000) - { - SendHTML_Content(); - } - if (file.isDirectory()) - { - Serial.println(String(file.isDirectory() ? "Dir " : "File ") + String(file.name())); - webpage += "" + String(file.isDirectory() ? "Dir" : "File") + "" + String(file.name()) + ""; - printDirectory(file.name(), levels - 1); - } - else - { - // Serial.print(String(file.name())+"\t"); - webpage += "" + String(file.name()) + ""; - Serial.print(String(file.isDirectory() ? "Dir " : "File ") + String(file.name()) + "\t"); - webpage += "" + String(file.isDirectory() ? "Dir" : "File") + ""; - int bytes = file.size(); - String fsize = ""; - if (bytes < 1024) - fsize = String(bytes) + " B"; - else if (bytes < (1024 * 1024)) - fsize = String(bytes / 1024.0, 3) + " KB"; - else if (bytes < (1024 * 1024 * 1024)) - fsize = String(bytes / 1024.0 / 1024.0, 3) + " MB"; - else - fsize = String(bytes / 1024.0 / 1024.0 / 1024.0, 3) + " GB"; - webpage += "" + fsize + ""; - Serial.println(String(fsize)); - } - file = root.openNextFile(); - } - file.close(); - } - - void SD_file_stream(String filename) - { - if (SD_present) - { - File dataFile = SD.open("/" + filename, FILE_READ); // Now read data from SD Card - Serial.print("Streaming file: "); - Serial.println(filename); - if (dataFile) - { - if (dataFile.available()) - { // If data is available and present - String dataType = "application/octet-stream"; - if (server.streamFile(dataFile, dataType) != dataFile.size()) - { - Serial.print(F("Sent less data than expected!")); - } - } - dataFile.close(); // close the file: - } - else - ReportFileNotPresent("Cstream"); - } - else - ReportSDNotPresent(); - } - - void File_Delete() - { - if (server.args() > 0) - { // Arguments were received - if (server.hasArg("delete")) - SD_file_delete(server.arg(0)); - } - else - SelectInput("Select a File to Delete", "delete", "delete"); - } - - void SD_file_delete(String filename) - { // Delete the file - if (SD_present) - { - SendHTML_Header(); - File dataFile = SD.open("/" + filename, FILE_READ); // Now read data from SD Card - Serial.print("Deleting file: "); - Serial.println(filename); - if (dataFile) - { - if (SD.remove("/" + filename)) - { - Serial.println(F("File deleted successfully")); - webpage += "

File '" + filename + "' has been erased

"; - webpage += F("[Back]

"); - } - else - { - webpage += F("

File was not deleted - error

"); - webpage += F("[Back]

"); - } - } - else - ReportFileNotPresent("delete"); - append_page_footer(); - SendHTML_Content(); - SendHTML_Stop(); - } - else - ReportSDNotPresent(); - } - - void SendHTML_Header() - { - server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - server.sendHeader("Pragma", "no-cache"); - server.sendHeader("Expires", "-1"); - server.setContentLength(CONTENT_LENGTH_UNKNOWN); - server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. - append_page_header(); - server.sendContent(webpage); - webpage = ""; - } - - - void ReportSDNotPresent() - { - SendHTML_Header(); - webpage += F("

No SD Card present

"); - webpage += F("[Back]

"); - append_page_footer(); - SendHTML_Content(); - SendHTML_Stop(); - } - - void ReportFileNotPresent(String target) - { - SendHTML_Header(); - webpage += F("

File does not exist

"); - webpage += F("[Back]

"; - append_page_footer(); - SendHTML_Content(); - SendHTML_Stop(); - } - - void ReportCouldNotCreateFile(String target) - { - SendHTML_Header(); - webpage += F("

Could Not Create Uploaded File (write-protected?)

"); - webpage += F("[Back]

"; - append_page_footer(); - SendHTML_Content(); - SendHTML_Stop(); - } -} -*/ \ No newline at end of file -- 2.30.2