diff --git a/include/matrix.hpp b/include/matrix.hpp index eead9b6..16678a4 100644 --- a/include/matrix.hpp +++ b/include/matrix.hpp @@ -45,7 +45,7 @@ namespace matrix{ struct file_info{ raii::rjp_string fileurl; raii::string filename; - raii::string filetype; + raii::string mimetype; size_t filesize; }; struct image_info : file_info{ diff --git a/include/raii/video_man.hpp b/include/raii/video_man.hpp index 9e123db..b8a003a 100644 --- a/include/raii/video_man.hpp +++ b/include/raii/video_man.hpp @@ -54,6 +54,7 @@ namespace raii{ int height(void)const; int width(void)const; + raii::string get_filetype(void)const; raii::string get_mimetype(void)const; packet_type create_jpg_thumbnail(void); diff --git a/src/fat_strings.cpp b/src/fat_strings.cpp index bde4fae..134d55a 100644 --- a/src/fat_strings.cpp +++ b/src/fat_strings.cpp @@ -60,7 +60,6 @@ namespace matrix::detail{ raii::string _image_body(const image_info& image){ - raii::string mimetype = "\"mimetype\":\"image/" + raii::json_escape(image.filetype) + "\""; raii::string url = raii::json_escape(image.fileurl); const raii::string_base* thumburl; if(image.thumburl) @@ -73,12 +72,12 @@ namespace matrix::detail{ "{" "\"body\":\"" + raii::json_escape(image.filename) + "\"," "\"info\":{" - "\"h\":" + itostr(image.height) + "," + - mimetype + "," + "\"h\":" + itostr(image.height) + "," + "\"mimetype\":\"" + image.mimetype + "\"," "\"size\":" + itostr(image.filesize) + "," "\"thumnail_info\":{" - "\"h\":" + itostr(image.thumb_height) + "," + - mimetype + "," + "\"h\":" + itostr(image.thumb_height) + "," + "\"mimetype\":\"" + image.mimetype + "\"," "\"size\":" + itostr(image.thumbsize) + "," "\"w\":" + itostr(image.thumb_width) + "}," @@ -96,7 +95,7 @@ namespace matrix::detail{ "\"body\":\"" + raii::json_escape(video.filename) + "\"," "\"info\":{" "\"h\":" + itostr(video.height) + "," - "\"mimetype\":\"video/" + raii::json_escape(video.filetype) + "\"," + "\"mimetype\":\"" + video.mimetype + "\"," "\"size\":" + itostr(video.filesize) + "," "\"thumnail_info\":{" "\"h\":" + itostr(video.thumb_height) + "," @@ -129,7 +128,7 @@ namespace matrix::detail{ "{" "\"body\":\"" + raii::json_escape(audio.filename) + "\"," "\"info\":{" - "\"mimetype\":\"audio/" + raii::json_escape(audio.filetype) + "\"," + "\"mimetype\":\"" + raii::json_escape(audio.mimetype) + "\"," "\"size\":" + itostr(audio.filesize) + "}," "\"msgtype\":\"m.audio\"," diff --git a/src/matrix.cpp b/src/matrix.cpp index 0f47b92..beee2de 100644 --- a/src/matrix.cpp +++ b/src/matrix.cpp @@ -141,39 +141,8 @@ namespace matrix{ return upload_image(filename, raii::static_string()); } #ifdef HAS_FREEIMAGE - image_info bot::upload_image(const raii::string_base& filename, const raii::string_base& alias){ - image_info ret; - - fipImage image; - auto formattotype = [](auto type) -> const char*{ - switch(type){ - case FIF_JPEG: return "jpeg"; - case FIF_PNG: return "png"; - case FIF_GIF: return "gif"; - default: - ; - }; - return nullptr; - }; - - //determine type of image. used to create thumbnail of the same type and verify this is an image. - auto type = fipImage::identifyFIF(filename.get()); - if(type == FIF_UNKNOWN){ - return {}; - } - - image.load(filename.get()); - //save fullsize image info - ret.width = image.getWidth(); - ret.height = image.getHeight(); - ret.filetype = formattotype(type); - ret.filename = alias ? alias : filename; - - raii::curl_llist header(raii::string("Content-Type: image/" + ret.filetype)); - - //create and upload thumbnail - image.makeThumbnail(500); - + static std::vector _create_image_thumbnail(fipImage& image, FREE_IMAGE_FORMAT type, size_t target_size){ + image.makeThumbnail(target_size); FreeImageIO fileout; std::vector buffer; fileout.write_proc = [](void* ptr, unsigned int size, unsigned int nmemb, void* fp) -> unsigned int{ @@ -181,42 +150,63 @@ namespace matrix{ buffer.insert(buffer.end(), (char*)ptr, ((char*)ptr)+size*nmemb); return size*nmemb; }; - bool b = false; switch(type){ case FIF_JPEG: - b = image.saveToHandle(type, &fileout, &buffer, JPEG_QUALITYGOOD | JPEG_SUBSAMPLING_411); - break; + return image.saveToHandle(type, &fileout, &buffer, JPEG_QUALITYGOOD | JPEG_SUBSAMPLING_411) ? buffer : std::vector(); case FIF_PNG: - b = image.saveToHandle(type, &fileout, &buffer, PNG_Z_BEST_COMPRESSION); - break; - case FIF_GIF: - b = image.saveToHandle(type, &fileout, &buffer); - break; + return image.saveToHandle(type, &fileout, &buffer, PNG_Z_BEST_COMPRESSION) ? buffer : std::vector(); default: - ; + return image.saveToHandle(type, &fileout, &buffer) ? buffer : std::vector(); }; - if(!b){ - fprintf(stderr, "Unable to create image thumbnail"); - ret.thumb_width = ret.width; - ret.thumb_height = ret.height; - ret.thumbsize = ret.filesize; + } + static std::vector _load_thumbnail_info(fipImage& image, image_info& info, FREE_IMAGE_FORMAT type){ + //create and upload thumbnail + if(info.width > 500 || info.height > 500){ + std::vector thumb_data = _create_image_thumbnail(image, type, 500); + if(!thumb_data.size()){ + info.thumb_width = info.width; + info.thumb_height = info.height; + info.thumbsize = info.filesize; + }else{ + info.thumb_width = image.getWidth(); + info.thumb_height = image.getHeight(); + info.thumbsize = thumb_data.size(); + return thumb_data; + } }else{ - ret.thumb_width = image.getWidth(); - ret.thumb_height = image.getHeight(); - ret.thumburl = _post_and_find(raii::static_string(buffer.data(), buffer.size()), m_urls.file_upload, header, "content_uri"_ss); - ret.thumbsize = buffer.size(); + info.thumb_width = info.width; + info.thumb_height = info.height; + info.thumbsize = info.filesize; } + return {}; + } + image_info bot::upload_image(const raii::string_base& filename, const raii::string_base& alias){ + image_info ret; + FREE_IMAGE_FORMAT type = fipImage::identifyFIF(filename.get()); + ret.mimetype = FreeImage_GetFIFMimeType(type); + raii::curl_llist header(raii::string("Content-Type: " + ret.mimetype)); - raii::filerd fd(filename, "rb"); - if(!fd) - return ret; + { + raii::filerd fd(filename, "rb"); + if(!fd) + return ret; - ret.filesize = fd.length(); - ret.fileurl = _upload_file(fd, header); - fd.reset(); + ret.filesize = fd.length(); + ret.fileurl = _upload_file(fd, header); + } + fipImage image; + image.load(filename.get()); + ret.width = image.getWidth(); + ret.height = image.getHeight(); + ret.filename = alias ? alias : filename; + std::vector thumb_data = _load_thumbnail_info(image, ret, type); + + if(thumb_data.size()) + ret.thumburl = _post_and_find(raii::static_string(thumb_data.data(), thumb_data.size()), m_urls.file_upload, header, "content_uri"_ss); return ret; } + #else //HAS_FREEIMAGE image_info bot::upload_image(const raii::string_base& filename, const raii::string_base& alias){ image_info ret = {}; @@ -224,7 +214,6 @@ namespace matrix{ return ret; } #endif //HAS_FREEIMAGE - video_info bot::upload_video(const raii::string_base& filename){ return upload_video(filename, raii::static_string()); } @@ -245,13 +234,13 @@ namespace matrix{ ret.thumb_height = context.height(); ret.width = context.width(); ret.height = context.height(); - ret.filetype = context.get_mimetype(); + ret.mimetype = context.get_mimetype(); context.reset(); //early cleanup to save memory ret.thumbsize = packet->size; ret.thumburl = _post_and_find(raii::static_string((char*)packet->data, packet->size), m_urls.file_upload, header, "content_uri"_ss); packet.reset(); - header = raii::curl_llist(raii::string("Content-Type: video/" + ret.filetype)); + header = raii::curl_llist(raii::string("Content-Type:" + ret.mimetype)); raii::filerd fd(filename); if(!fd) return {}; ret.filesize = fd.length(); @@ -265,9 +254,9 @@ namespace matrix{ raii::video_man context(filename); if(!context) return {}; - ret.filetype = context.get_mimetype(); + ret.mimetype = context.get_mimetype(); context.reset(); - raii::curl_llist header(raii::string("Content-Type: audio/" + ret.filetype)); + raii::curl_llist header(raii::string("Content-Type:" + ret.mimetype)); raii::filerd fd(filename); if(!fd) return {}; ret.filesize = fd.length(); diff --git a/src/raii/video_man.cpp b/src/raii/video_man.cpp index c2865a2..ed12fff 100644 --- a/src/raii/video_man.cpp +++ b/src/raii/video_man.cpp @@ -46,12 +46,18 @@ namespace raii{ int video_man::width(void)const{ return m_context->width; } - raii::string video_man::get_mimetype(void)const{ + raii::string video_man::get_filetype(void)const{ const char* first = strstr(m_vid->iformat->name, ","); if(first) return raii::string(raii::static_string(m_vid->iformat->name, first - m_vid->iformat->name)); return raii::string(raii::static_string(m_vid->iformat->name)); } + raii::string video_man::get_mimetype(void)const{ + const char* first = strstr(m_vid->iformat->name, ","); + if(first) + return raii::string("video/" + raii::static_string(m_vid->iformat->name, first - m_vid->iformat->name)); + return raii::string("video/" + raii::static_string(m_vid->iformat->name)); + } auto video_man::create_jpg_thumbnail(void) -> packet_type{ context_type jpg_context = _get_jpeg_context(m_context); frame_type frame(_init_frame(m_context->width, m_context->height, m_context->pix_fmt), _frame_deleter); diff --git a/src/test.cpp b/src/test.cpp index 777444b..d4cfefc 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -337,7 +337,7 @@ int main(){ { int retries = 5; do{ - reply = mybot.get_top_post("ProgrammerHumor"_ss, reply.name(), reddit::time::hour); + reply = mybot.get_hot_post("ProgrammerHumor"_ss, reply.name()); if(reply.type() != reddit::post_type::text && reply.type() != reddit::post_type::link) break; --retries; @@ -347,11 +347,11 @@ int main(){ }while(retries); } + write_to_file("post.log", reply.raw()); if(!reply){ fprintf(stderr, "Did not recieve a reply!\n"); return 3; } - write_to_file("post.log", reply.raw()); DEBUG_PRINT("name: %s\n", reply.name().get()); DEBUG_PRINT("title: %s\n", reply.title().get());