Skip to content

Commit

Permalink
Added ESP32 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeio committed Sep 29, 2021
1 parent 2cd1146 commit 0a564ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
30 changes: 23 additions & 7 deletions http_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#ifdef STDIO_FS
#include <sys/unistd.h>
#endif

#include "http_upload.h"
#include "multipartparser.h"
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{

Expand All @@ -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;
}

Expand Down
15 changes: 13 additions & 2 deletions http_upload.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@

#include <stdlib.h>

#ifdef ESP_PLATFORM
#include <esp_http_server.h>
#include <esp_vfs_fat.h>
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
{
Expand All @@ -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;
Expand All @@ -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

0 comments on commit 0a564ec

Please sign in to comment.