Skip to content

Commit

Permalink
Add aggregate to empty and binary data (#52)
Browse files Browse the repository at this point in the history
* Add agg to empty and binary data

* lint

* lint again

* moar lint
  • Loading branch information
royshil committed Nov 15, 2023
1 parent 3cf0b75 commit 6eec0cb
Show file tree
Hide file tree
Showing 9 changed files with 705 additions and 360 deletions.
10 changes: 9 additions & 1 deletion src/parsers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
target_sources(${CMAKE_PROJECT_NAME} PRIVATE jsonpointer.cpp jsonpath.cpp regex.cpp xml.cpp errors.cpp html.cpp)
target_sources(
${CMAKE_PROJECT_NAME}
PRIVATE jsonpointer.cpp
jsonpath.cpp
regex.cpp
xml.cpp
errors.cpp
html.cpp
binary-data.cpp)

# on linux, disable conversion errors
if(UNIX AND NOT APPLE)
Expand Down
82 changes: 82 additions & 0 deletions src/parsers/binary-data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "errors.h"
#include "request-data.h"
#include "plugin-support.h"
#include "string-util.h"

#include <obs-module.h>

#include <string>
#include <vector>
#include <fstream>
#include <filesystem>

std::string save_to_temp_file(const std::vector<uint8_t> &data, const std::string &extension)
{
// check if the config folder exists if it doesn't exist, create it.
char *config_path = obs_module_config_path("");
if (!std::filesystem::exists(config_path)) {
if (!std::filesystem::create_directory(config_path)) {
obs_log(LOG_ERROR, "Failed to create config directory %s", config_path);
bfree(config_path);
return "";
}
}
bfree(config_path);

// append the extension to the file name
std::string file_name = "temp." + extension;
char *temp_file_path = obs_module_config_path(file_name.c_str());

obs_log(LOG_INFO, "save to temp file %s, %d bytes", temp_file_path, data.size());

std::ofstream temp_file(temp_file_path, std::ios::binary);
bfree(temp_file_path);
temp_file.write((const char *)data.data(), data.size());
temp_file.close();
return std::string(temp_file_path);
}

struct request_data_handler_response parse_image_data(struct request_data_handler_response response,
const url_source_request_data *request_data)
{
UNUSED_PARAMETER(request_data);

// find the image type from the content type on the response.headers map
std::string content_type = response.headers["content-type"];
std::string image_type = content_type.substr(content_type.find("/") + 1);

// if the image type is not supported, return an error
if (image_type != "png" && image_type != "jpg" && image_type != "jpeg" &&
image_type != "gif") {
return make_fail_parse_response("Unsupported image type: " + image_type);
}

// save the image to a temporary file
std::string temp_file_path = save_to_temp_file(response.body_bytes, image_type);
response.body = temp_file_path;

return response;
}

struct request_data_handler_response parse_audio_data(struct request_data_handler_response response,
const url_source_request_data *request_data)
{
UNUSED_PARAMETER(request_data);

// find the audio type from the content type on the response.headers map
std::string content_type = response.headers["content-type"];
std::string audio_type = content_type.substr(content_type.find("/") + 1);
audio_type = trim(audio_type);

// if the audio type is not supported, return an error
if (!(audio_type == "mp3" || audio_type == "mpeg" || audio_type == "wav" ||
audio_type == "ogg" || audio_type == "flac" || audio_type == "aac")) {
return make_fail_parse_response("Unsupported audio type: " + audio_type);
}

// save the audio to a temporary file
std::string temp_file_path = save_to_temp_file(response.body_bytes, audio_type);
response.body = temp_file_path;

return response;
}
6 changes: 6 additions & 0 deletions src/parsers/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ struct request_data_handler_response parse_xml(struct request_data_handler_respo
struct request_data_handler_response parse_html(struct request_data_handler_response response,
const url_source_request_data *request_data);

struct request_data_handler_response parse_image_data(struct request_data_handler_response response,
const url_source_request_data *request_data);

struct request_data_handler_response parse_audio_data(struct request_data_handler_response response,
const url_source_request_data *request_data);

struct request_data_handler_response
parse_xml_by_xquery(struct request_data_handler_response response,
const url_source_request_data *request_data);
Expand Down

0 comments on commit 6eec0cb

Please sign in to comment.