diff --git a/http_upload.c b/http_upload.c index 12b74b2..deba3bd 100644 --- a/http_upload.c +++ b/http_upload.c @@ -35,6 +35,9 @@ #include #include #include +#ifdef STDIO_FS +#include +#endif #include "http_upload.h" #include "multipartparser.h" @@ -43,6 +46,7 @@ static struct multipartparser parser; static struct multipartparser_callbacks *sd_callbacks = NULL; +static http_upload_filename_parsed_ptr on_filename_parsed; static void do_cleanup (file_upload_t *upload) { @@ -94,11 +98,7 @@ static void on_header_done (struct multipartparser *parser) upload->state = Upload_GetPath; *upload->path = '\0'; } else if((name = strstr(upload->header_value, "filename=\""))) { - if(!upload->to_fatfs) - strcpy(upload->filename, "/spiffs"); - else - *upload->filename = '\0'; - strcat(upload->filename, name + 10); + strcpy(upload->filename, name + 10); upload->filename[strlen(upload->filename) - 1] = '\0'; if(*upload->size_str) upload->size = atoi(upload->size_str); @@ -110,6 +110,9 @@ static void on_header_done (struct multipartparser *parser) if(*upload->filename && strstr(upload->header_name, "Content-Type")) { + if(on_filename_parsed) + on_filename_parsed(upload->filename); + if(upload->to_fatfs) { upload->file.fatfs_handle = &upload->fatfs_fd; if(f_open(upload->file.fatfs_handle, upload->filename, FA_WRITE|FA_CREATE_ALWAYS) == FR_OK) @@ -225,6 +228,11 @@ static int on_body_end (struct multipartparser *parser) return 0; } +void http_upload_on_filename_parsed (http_upload_filename_parsed_ptr fn) +{ + on_filename_parsed = fn; +} + bool http_upload_start (http_request_t *request, const char* boundary, bool to_fatfs) { @@ -250,14 +258,22 @@ bool http_upload_start (http_request_t *request, const char* boundary, bool to_f if(sd_callbacks) { multipartparser_init(&parser, boundary); - - if((parser.data = request->private_data = malloc(sizeof(file_upload_t)))) { + if((parser.data = malloc(sizeof(file_upload_t)))) { +#ifdef ESP_PLATFORM + request->free_ctx = cleanup; + request->sess_ctx = parser.data; +#else + request->private_data = parser.data; request->on_request_completed = cleanup; +#endif memset(parser.data, 0, sizeof(file_upload_t)); ((file_upload_t *)parser.data)->to_fatfs = to_fatfs; } + } + on_filename_parsed = NULL; + return parser.data != NULL; } diff --git a/http_upload.h b/http_upload.h index 996fe14..8b2cdc3 100644 --- a/http_upload.h +++ b/http_upload.h @@ -26,8 +26,16 @@ #include +#ifdef ESP_PLATFORM +#include +#include +typedef httpd_req_t http_request_t; +#else #include "networking/httpd.h" #include "networking/vfs.h" +#endif + +#define HTTP_UPLOAD_MAX_PATHLENGTH 100 typedef enum { @@ -49,8 +57,8 @@ typedef struct { bool to_fatfs; char header_name[100]; char header_value[100]; - char filename[100]; - char path[100]; + char filename[HTTP_UPLOAD_MAX_PATHLENGTH + 1]; + char path[HTTP_UPLOAD_MAX_PATHLENGTH + 1]; char size_str[15]; file_handle_t file; http_request_t *req; @@ -59,8 +67,11 @@ typedef struct { size_t uploaded; } file_upload_t; +typedef void (*http_upload_filename_parsed_ptr)(char *name); + bool http_upload_start (http_request_t *req, const char* boundary, bool to_fatfs); size_t http_upload_chunk (http_request_t *req, const char* data, size_t size); +void http_upload_on_filename_parsed (http_upload_filename_parsed_ptr fn); #endif