file uploading and deleting working
authorNils <forssennils@gmail.com>
Sun, 21 Jan 2024 22:35:29 +0000 (23:35 +0100)
committerNils <forssennils@gmail.com>
Sun, 21 Jan 2024 22:35:29 +0000 (23:35 +0100)
e-paper-project/e-paper-project.ino
e-paper-project/include/sd_card.h
e-paper-project/sd_card.cpp
e-paper-project/web_server.cpp

index 7609ffe4d0d58729ccf61e8006a733e0b48e099d..c3f5dba51b7e06fe397388f80f6035ad87445ca5 100644 (file)
@@ -33,7 +33,6 @@ void setup(){
     SD_card_fs::init_SD(5);
     Web_server::init_wifi();
     Web_server::init_server();
-
 }
 
 void loop()
index 5544add8098fac345ccc2bd4f231821d473b8987..3bf689d5a78009eadede20414c4e2a3d41efebff 100644 (file)
@@ -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<File> 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);
 };
index e6a940fe727da8fdfbe04efff7ad83611d033859..f2bce73fda246cd9238ae81b63cdde476ac938dd 100644 (file)
@@ -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>{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;
         }
     }
index eeb8fb9192f1be8f24203d1910f968feaa5fb2e1..fcb660c4dfc4fd34b74b94b114386bc6dea80c0f 100644 (file)
@@ -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("<h3 class='rcorners_m'>SD Card Contents</h3><br>");
-            webpage += F("<table align='center'>");
-            webpage += F("<tr><th>Name/Type</th><th style='width:20%'>Type File/Dir</th><th>File Size</th></tr>");
-            printDirectory("/", 0);
-            webpage += F("</table>");
-            SendHTML_Content();
-            root.close();
-        }
-        else
-        {
-            SendHTML_Header();
-            webpage += F("<h3>No Files Found</h3>");
-        }
-        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 += "<tr><td>" + String(file.isDirectory() ? "Dir" : "File") + "</td><td>" + String(file.name()) + "</td><td></td></tr>";
-                printDirectory(file.name(), levels - 1);
-            }
-            else
-            {
-                // Serial.print(String(file.name())+"\t");
-                webpage += "<tr><td>" + String(file.name()) + "</td>";
-                Serial.print(String(file.isDirectory() ? "Dir " : "File ") + String(file.name()) + "\t");
-                webpage += "<td>" + String(file.isDirectory() ? "Dir" : "File") + "</td>";
-                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 += "<td>" + fsize + "</td></tr>";
-                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 += "<h3>File '" + filename + "' has been erased</h3>";
-                    webpage += F("<a href='/delete'>[Back]</a><br><br>");
-                }
-                else
-                {
-                    webpage += F("<h3>File was not deleted - error</h3>");
-                    webpage += F("<a href='delete'>[Back]</a><br><br>");
-                }
-            }
-            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("<h3>No SD Card present</h3>");
-        webpage += F("<a href='/'>[Back]</a><br><br>");
-        append_page_footer();
-        SendHTML_Content();
-        SendHTML_Stop();
-    }
-
-    void ReportFileNotPresent(String target)
-    {
-        SendHTML_Header();
-        webpage += F("<h3>File does not exist</h3>");
-        webpage += F("<a href='/");
-        webpage += target + "'>[Back]</a><br><br>";
-        append_page_footer();
-        SendHTML_Content();
-        SendHTML_Stop();
-    }
-
-    void ReportCouldNotCreateFile(String target)
-    {
-        SendHTML_Header();
-        webpage += F("<h3>Could Not Create Uploaded File (write-protected?)</h3>");
-        webpage += F("<a href='/");
-        webpage += target + "'>[Back]</a><br><br>";
-        append_page_footer();
-        SendHTML_Content();
-        SendHTML_Stop();
-    }
-}
-*/
\ No newline at end of file