This commit is contained in:
jomjol
2020-12-22 08:08:07 +01:00
parent f5c28107d4
commit b418525b3b
39 changed files with 1173 additions and 501 deletions

View File

@@ -144,6 +144,7 @@ esp_err_t hello_main_handler(httpd_req_t *req)
struct stat file_stat;
printf("uri: %s\n", req->uri);
int _pos;
esp_err_t res;
char *base_path = (char*) req->user_ctx;
std::string filetosend(base_path);
@@ -182,20 +183,8 @@ esp_err_t hello_main_handler(httpd_req_t *req)
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
return ESP_FAIL;
}
if (stat(filetosend.c_str(), &file_stat) == -1) {
/* If file not present on SPIFFS check if URI
* corresponds to one of the hardcoded paths */
ESP_LOGE(TAG, "Failed to stat file : %s", filetosend.c_str());
/* Respond with 404 Not Found */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
return ESP_FAIL;
}
esp_err_t res;
res = httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
if (res != ESP_OK)
return res;
res = send_file(req, filetosend, &file_stat);
res = send_file(req, filetosend);
if (res != ESP_OK)
return res;
@@ -214,28 +203,13 @@ esp_err_t img_tmp_handler(httpd_req_t *req)
std::string filetosend(base_path);
const char *filename = get_path_from_uri(filepath, base_path,
req->uri + sizeof("/img_tmp") - 1, sizeof(filepath));
req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
filetosend = filetosend + "/img_tmp/" + std::string(filename);
printf("File to upload: %s\n", filetosend.c_str());
if (!filename) {
ESP_LOGE(TAG, "Filename is too long");
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
return ESP_FAIL;
}
if (stat(filetosend.c_str(), &file_stat) == -1) {
/* If file not present on SPIFFS check if URI
* corresponds to one of the hardcoded paths */
ESP_LOGE(TAG, "Failed to stat file : %s", filetosend.c_str());
/* Respond with 404 Not Found */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
return ESP_FAIL;
}
esp_err_t res = send_file(req, filetosend, &file_stat);
esp_err_t res = send_file(req, filetosend);
if (res != ESP_OK)
return res;
@@ -244,6 +218,58 @@ esp_err_t img_tmp_handler(httpd_req_t *req)
return ESP_OK;
}
esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
{
char filepath[50];
printf("uri: %s\n", req->uri);
char *base_path = (char*) req->user_ctx;
std::string filetosend(base_path);
const char *filename = get_path_from_uri(filepath, base_path,
req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
filetosend = std::string(filename);
printf("File to upload: %s\n", filetosend.c_str());
if (filetosend == "raw.jpg")
{
return GetRawJPG(req);
}
ImageData *zw = GetJPG(filetosend);
if (zw)
{
ESP_LOGI(TAG, "Sending file : %s (%d bytes)...", filetosend.c_str(), zw->size);
set_content_type_from_file(req, filetosend.c_str());
if (httpd_resp_send_chunk(req, (char*) &(zw->data), zw->size) != ESP_OK) {
ESP_LOGE(TAG, "File sending failed!");
httpd_resp_sendstr_chunk(req, NULL);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
delete zw;
return ESP_FAIL;
}
ESP_LOGI(TAG, "File sending complete");
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
delete zw;
return ESP_OK;
}
// File wird nicht intern bereit gestellt --> klassischer weg:
return img_tmp_handler(req);
}
esp_err_t sysinfo_handler(httpd_req_t *req)
{
const char* resp_str;
@@ -314,7 +340,7 @@ void register_server_main_uri(httpd_handle_t server, const char *base_path)
httpd_uri_t img_tmp_handle = {
.uri = "/img_tmp/*", // Match all URIs of type /path/to/file
.method = HTTP_GET,
.handler = img_tmp_handler,
.handler = img_tmp_virtual_handler,
.user_ctx = (void*) base_path // Pass server data as context
};
httpd_register_uri_handler(server, &img_tmp_handle);