Skip to content

Commit

Permalink
multifile encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Sep 16, 2024
1 parent 4ec3cff commit 757871b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 34 deletions.
1 change: 1 addition & 0 deletions include/server_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
62 changes: 44 additions & 18 deletions src/handler_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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, &current_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, &current_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)
Expand Down
43 changes: 27 additions & 16 deletions src/server_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -100,32 +100,43 @@ 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);
while ((q = strstr(q, key)))
{
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
}
Expand Down

0 comments on commit 757871b

Please sign in to comment.