#include "include/sd_card.h"
+static File file;
namespace SD_card_fs
{
fs_error_t init_SD(const uint16_t cs)
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;
}
}
#include "include/web_server.h"
#include "include/web_HTML.h"
+#include "include/sd_card.h"
#ifdef ESP8266
static ESP8266WebServer server{SERVER_PORT};
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");
}
}
};
-
-
-
-/*
-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