From 4ec3cff71fbb7b3430b3570b7dad95cd1f3f2a49 Mon Sep 17 00:00:00 2001 From: SciLor Date: Mon, 16 Sep 2024 20:18:30 +0000 Subject: [PATCH] experimental backend TAF encoder --- include/handler_api.h | 1 + src/handler_api.c | 60 +++++++++++++++++++++++++++++++++++++++++-- src/server.c | 1 + 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/include/handler_api.h b/include/handler_api.h index 303bdfc4..cc57c8f7 100644 --- a/include/handler_api.h +++ b/include/handler_api.h @@ -40,6 +40,7 @@ error_t handleApiFileDelete(HttpConnection *connection, const char_t *uri, const error_t handleApiFileMove(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); error_t handleApiDirectoryDelete(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); error_t handleApiAssignUnknown(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); +error_t handleApiEncodeFile(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); error_t handleApiPcmUpload(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); error_t handleApiContent(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); error_t handleApiContentDownload(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); diff --git a/src/handler_api.c b/src/handler_api.c index e2bde9b6..a92cba0a 100644 --- a/src/handler_api.c +++ b/src/handler_api.c @@ -1786,6 +1786,60 @@ error_t taf_encode_end(void *in_ctx) return NO_ERROR; } +error_t handleApiEncodeFile(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx) +{ + char overlay[16]; + const char *rootPath = NULL; + + if (queryPrepare(queryString, &rootPath, overlay, sizeof(overlay), &client_ctx->settings) != NO_ERROR) + { + return ERROR_FAILURE; + } + + char_t post_data[BODY_BUFFER_SIZE]; + error_t error = parsePostData(connection, post_data, BODY_BUFFER_SIZE); + if (error != NO_ERROR) + { + TRACE_ERROR("parsePostData failed with error %s\r\n", error2text(error)); + return error; + } + + char multisource[99][PATH_LEN]; + uint8_t multisource_size = 0; + char source[256 + 3]; + char target[256 + 3]; + + 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"); + return ERROR_INVALID_REQUEST; + } + sanitizePath(target, false); + 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]); + + 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) + { + TRACE_ERROR("ffmpeg_convert failed with error %s\r\n", error2text(error)); + } + + return error; +} error_t handleApiPcmUpload(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx) { char overlay[16]; @@ -2065,11 +2119,13 @@ error_t handleApiFileMove(HttpConnection *connection, const char_t *uri, const c char source[256 + 3]; char target[256 + 3]; - if (!queryGet(post_data, "source", source, sizeof(source))) { + 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))) { + if (!queryGet(post_data, "target", target, sizeof(target))) + { TRACE_ERROR("target missing!\r\n"); return ERROR_INVALID_REQUEST; } diff --git a/src/server.c b/src/server.c index 10764c5c..dfa864f1 100644 --- a/src/server.c +++ b/src/server.c @@ -126,6 +126,7 @@ request_type_t request_paths[] = { {REQ_POST, "/api/uploadFirmware", SERTY_WEB, &handleApiUploadFirmware}, {REQ_GET, "/api/patchFirmware", SERTY_WEB, &handleApiPatchFirmware}, {REQ_POST, "/api/fileUpload", SERTY_WEB, &handleApiFileUpload}, + {REQ_POST, "/api/fileEncode", SERTY_WEB, &handleApiEncodeFile}, {REQ_POST, "/api/pcmUpload", SERTY_WEB, &handleApiPcmUpload}, {REQ_GET, "/api/fileIndexV2", SERTY_WEB, &handleApiFileIndexV2}, {REQ_GET, "/api/fileIndex", SERTY_WEB, &handleApiFileIndex},