From 757871bb04ab5a3805d4f1643527302b8f64e61c Mon Sep 17 00:00:00 2001 From: SciLor Date: Mon, 16 Sep 2024 21:14:14 +0000 Subject: [PATCH] multifile encoding --- include/server_helpers.h | 1 + src/handler_api.c | 62 ++++++++++++++++++++++++++++------------ src/server_helpers.c | 43 +++++++++++++++++----------- 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/include/server_helpers.h b/include/server_helpers.h index d92da762..c9ba371f 100644 --- a/include/server_helpers.h +++ b/include/server_helpers.h @@ -18,6 +18,7 @@ error_t multipart_handle(HttpConnection *connection, multipart_cbr_t *cbr, void int urldecode(char *dest, size_t dest_max, const char *src); bool queryGet(const char *query, const char *key, char *data, size_t data_len); +bool queryGetMulti(const char *query, const char *key, char *data, size_t data_len, size_t skip_len); char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str); char_t *ipv6AddrToString(const Ipv6Addr *ipAddr, char_t *str); char_t *ipv4AddrToString(Ipv4Addr ipAddr, char_t *str); diff --git a/src/handler_api.c b/src/handler_api.c index a92cba0a..4014db64 100644 --- a/src/handler_api.c +++ b/src/handler_api.c @@ -1806,14 +1806,9 @@ error_t handleApiEncodeFile(HttpConnection *connection, const char_t *uri, const char multisource[99][PATH_LEN]; uint8_t multisource_size = 0; - char source[256 + 3]; - char target[256 + 3]; + char source[PATH_LEN]; + char target[PATH_LEN]; - if (!queryGet(post_data, "source", source, sizeof(source))) - { - TRACE_ERROR("source missing!\r\n"); - return ERROR_INVALID_REQUEST; - } if (!queryGet(post_data, "target", target, sizeof(target))) { TRACE_ERROR("target missing!\r\n"); @@ -1823,21 +1818,52 @@ error_t handleApiEncodeFile(HttpConnection *connection, const char_t *uri, const char *targetAbsolute = custom_asprintf("%s%c%s", rootPath, PATH_SEPARATOR, target); sanitizePath(targetAbsolute, false); - // TODO implement multiple sources - sanitizePath(source, false); - osSprintf(multisource[multisource_size++], "%s%c%s", rootPath, PATH_SEPARATOR, source); - sanitizePath(multisource[multisource_size - 1], false); - TRACE_INFO("Encode: '%s'\r\n", multisource[multisource_size - 1]); + char_t message[256]; + uint_t statusCode = 200; - TRACE_INFO("Encode %" PRIu8 " files to '%s'\r\n", multisource_size, target); - size_t current_source = 0; - error = ffmpeg_convert(multisource, multisource_size, ¤t_source, target, 0); - osFreeMem(targetAbsolute); - if (error != NO_ERROR) + if (fsFileExists(targetAbsolute)) { - TRACE_ERROR("ffmpeg_convert failed with error %s\r\n", error2text(error)); + TRACE_ERROR("File %s already exists!\r\n", targetAbsolute); + osSnprintf(message, sizeof(message), "File %s already exists!\r\n", targetAbsolute); + statusCode = 500; } + else + { + while (queryGetMulti(post_data, "source", source, sizeof(source), multisource_size)) + { + sanitizePath(source, false); + osSprintf(multisource[multisource_size], "%s%c%s", rootPath, PATH_SEPARATOR, source); + sanitizePath(multisource[multisource_size], false); + // TRACE_INFO("Source %s\r\n", multisource[multisource_size]); + multisource_size++; + } + if (multisource_size == 0) + { + TRACE_ERROR("Source missing!\r\n"); + osFreeMem(targetAbsolute); + return ERROR_INVALID_REQUEST; + } + TRACE_INFO("Encode %" PRIu8 " files to %s\r\n", multisource_size, targetAbsolute); + size_t current_source = 0; + error = ffmpeg_convert(multisource, multisource_size, ¤t_source, targetAbsolute, 0); + osFreeMem(targetAbsolute); + if (error != NO_ERROR) + { + TRACE_ERROR("ffmpeg_convert failed with error %s\r\n", error2text(error)); + statusCode = 500; + osSnprintf(message, sizeof(message), "ffmpeg_convert failed with error %s\r\n", error2text(error)); + } + else + { + osSnprintf(message, sizeof(message), "OK\r\n"); + } + } + + httpPrepareHeader(connection, "text/plain; charset=utf-8", osStrlen(message)); + connection->response.statusCode = statusCode; + + return httpWriteResponseString(connection, message, false); return error; } error_t handleApiPcmUpload(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx) diff --git a/src/server_helpers.c b/src/server_helpers.c index cb4bfc87..93bfc099 100644 --- a/src/server_helpers.c +++ b/src/server_helpers.c @@ -62,7 +62,7 @@ int urldecode(char *dest, size_t dest_max, const char *src) { char a = 0; char b = 0; - + if ((src[src_idx] == '%') && ((a = src[src_idx + 1]) && (b = src[src_idx + 2])) && (isxdigit(a) && isxdigit(b))) @@ -100,6 +100,10 @@ int urldecode(char *dest, size_t dest_max, const char *src) } bool queryGet(const char *query, const char *key, char *data, size_t data_len) +{ + return queryGetMulti(query, key, data, data_len, 0); +} +bool queryGetMulti(const char *query, const char *key, char *data, size_t data_len, size_t skip_len) { const char *q = query; size_t key_len = osStrlen(key); @@ -107,25 +111,32 @@ bool queryGet(const char *query, const char *key, char *data, size_t data_len) { if (q[key_len] == '=') { - // Found the key, let's start copying the value - q += key_len + 1; // Skip past the key and the '=' - char buffer[1024]; // Temporary buffer for decoding - char *b = buffer; - while (*q && *q != '&') + if (skip_len == 0) { - if (b - buffer < sizeof(buffer) - 1) - { // Prevent buffer overflow - *b++ = *q++; - } - else + // Found the key, let's start copying the value + q += key_len + 1; // Skip past the key and the '=' + char buffer[1024]; // Temporary buffer for decoding + char *b = buffer; + while (*q && *q != '&') { - // The value is too long, truncate it - break; + if (b - buffer < sizeof(buffer) - 1) + { // Prevent buffer overflow + *b++ = *q++; + } + else + { + // The value is too long, truncate it + break; + } } + *b = '\0'; // Null-terminate the buffer + urldecode(data, data_len, buffer); // Decode and copy the value + return true; + } + else + { + skip_len--; } - *b = '\0'; // Null-terminate the buffer - urldecode(data, data_len, buffer); // Decode and copy the value - return true; } q += key_len; // Skip past the key }