Worked on the upload stuff. It works, but the SD card seems to die after a few MB... :-/
This commit is contained in:
parent
8e5a3195b9
commit
9f442259e9
@ -16,6 +16,7 @@ private:
|
||||
void _handle_upload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final);
|
||||
uint16_t _chunk_length;
|
||||
uint8_t* _chunk;
|
||||
File _upload_file;
|
||||
uint32_t _file_size;
|
||||
uint32_t _file_size_done;
|
||||
bool _need_header;
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "http_server.h"
|
||||
#include "spi_master.h"
|
||||
#include <ESPmDNS.h>
|
||||
#include <SPIFFS.h>
|
||||
|
||||
@ -10,6 +11,7 @@ HTTPServer::HTTPServer(Player* p, Controller* c) {
|
||||
_server->addHandler(ws);
|
||||
ws->onEvent([&](AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){this->_onEvent(server, client, type, arg, data, len);});
|
||||
_server->on("/", [&](AsyncWebServerRequest* req) {req->send(SPIFFS, "/index.html", "text/html");});
|
||||
_server->on("/upload", HTTP_POST, [](AsyncWebServerRequest* req) {req->send(200); }, ([&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){this->_handle_upload(request, filename, index, data, len, final);}));
|
||||
_server->begin();
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
@ -40,7 +42,7 @@ void HTTPServer::_handle_upload(AsyncWebServerRequest* request, String filename,
|
||||
|
||||
if (_chunk_length == 512) {
|
||||
// Process chunk
|
||||
DEBUG("Chunk.\n");
|
||||
DEBUG(".");
|
||||
if (_need_header) {
|
||||
if (_chunk[257]=='u'&&_chunk[258]=='s'&&_chunk[259]=='t'&&_chunk[260]=='a'&&_chunk[261]=='r') {
|
||||
DEBUG("It is a valid header, starting at 0x%X!\n", _upload_position-512);
|
||||
@ -56,9 +58,26 @@ void HTTPServer::_handle_upload(AsyncWebServerRequest* request, String filename,
|
||||
DEBUG("filesize: %d\n", _file_size);
|
||||
uint8_t type = _chunk[156] - '0';
|
||||
if (type==0) {
|
||||
DEBUG("Is a file.\n");
|
||||
String path = "/";
|
||||
path += filename;
|
||||
DEBUG("Opening file %s\n", path.c_str());
|
||||
uint8_t state = SPIMaster::state;
|
||||
SPIMaster::disable();
|
||||
SPIMaster::select_sd();
|
||||
// Better safe than sorry. ;-)
|
||||
_upload_file.close();
|
||||
_upload_file = SD.open(path, "w");
|
||||
SPIMaster::set_state(state);
|
||||
} else if (type==5) {
|
||||
DEBUG("Is a directory.\n");
|
||||
String dirname = "/";
|
||||
dirname += filename;
|
||||
dirname.remove(dirname.length()-1);
|
||||
uint8_t state = SPIMaster::state;
|
||||
SPIMaster::disable();
|
||||
SPIMaster::select_sd();
|
||||
bool res = SD.mkdir(dirname);
|
||||
SPIMaster::set_state(state);
|
||||
DEBUG("Creating folder '%s' returned %d.\n", dirname.c_str(), res);
|
||||
} else {
|
||||
ERROR("Unknown file type %d\n", type);
|
||||
}
|
||||
@ -73,11 +92,18 @@ void HTTPServer::_handle_upload(AsyncWebServerRequest* request, String filename,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uint16_t bytes_to_write = _file_size - _file_size_done;
|
||||
uint32_t bytes_to_write = _file_size - _file_size_done;
|
||||
if (bytes_to_write > 512) bytes_to_write=512;
|
||||
// Write bytes...
|
||||
uint8_t state = SPIMaster::state;
|
||||
SPIMaster::disable();
|
||||
SPIMaster::select_sd();
|
||||
_upload_file.write(_chunk, bytes_to_write);
|
||||
_file_size_done += bytes_to_write;
|
||||
if (_file_size_done >= _file_size) _need_header = true;
|
||||
if (_file_size_done >= _file_size) {
|
||||
_upload_file.close();
|
||||
_need_header = true;
|
||||
}
|
||||
SPIMaster::set_state(state);
|
||||
}
|
||||
|
||||
_chunk_length = 0;
|
||||
@ -86,8 +112,13 @@ void HTTPServer::_handle_upload(AsyncWebServerRequest* request, String filename,
|
||||
}
|
||||
|
||||
if (final == true) {
|
||||
// Close the file
|
||||
uint8_t state = SPIMaster::state;
|
||||
SPIMaster::disable();
|
||||
SPIMaster::select_sd();
|
||||
_upload_file.close();
|
||||
SPIMaster::set_state(state);
|
||||
delete _chunk;
|
||||
_controller->update_playlist_manager();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user