Renamed uploaded file structs

This commit is contained in:
rexy712 2019-09-18 18:10:27 -07:00
parent a729c4d2f3
commit 262dca00ef
11 changed files with 261 additions and 112 deletions

View File

@ -107,32 +107,32 @@ namespace matrix{
* Returns: file_info struct containing the url of the file on the homeserver. * Returns: file_info struct containing the url of the file on the homeserver.
* Note check the file_info::fileurl field to check if the upload succeeded. * Note check the file_info::fileurl field to check if the upload succeeded.
*/ */
file_info upload_file(const file_details& file)const; uploaded_file upload_file(const file_details& file)const;
/* /*
* Upload a file as an image. * Upload a file as an image.
* Returns: image_info struct containing the url of the image on the homeserver. * Returns: image_info struct containing the url of the image on the homeserver.
* Note check the image_info::fileurl field to check if the upload succeeded. * Note check the image_info::fileurl field to check if the upload succeeded.
*/ */
image_info upload_image(const image_details& file)const; uploaded_image upload_image(const image_details& file)const;
/* /*
* Upload data as a video. * Upload data as a video.
* Returns: video_info struct containing the url of the image on the homeserver. * Returns: video_info struct containing the url of the image on the homeserver.
* Note check the video_info::fileurl field to check if the upload succeeded. * Note check the video_info::fileurl field to check if the upload succeeded.
*/ */
video_info upload_video(const video_details& file)const; uploaded_video upload_video(const video_details& file)const;
/* /*
* Upload a file as an audio file. * Upload a file as an audio file.
* Returns: audio_info struct containing the url of the image on the homeserver. * Returns: audio_info struct containing the url of the image on the homeserver.
* Note check the audio_info::fileurl field to check if the upload succeeded. * Note check the audio_info::fileurl field to check if the upload succeeded.
*/ */
audio_info upload_audio(const audio_details& file)const; uploaded_audio upload_audio(const audio_details& file)const;
bool create_thumbnail(image_info& info)const; bool create_thumbnail(uploaded_image& info)const;
bool create_thumbnail(video_info& video)const; bool create_thumbnail(uploaded_video& video)const;
private: private:
file_info _upload_file(const file_details& file, const raii::curl_llist& header)const; uploaded_file _upload_file(const file_details& file, const raii::curl_llist& header)const;
}; };
} }

View File

@ -25,18 +25,14 @@
//but will rarely need rebuilt //but will rarely need rebuilt
#include "raii/string.hpp" #include "raii/string.hpp"
#include "matrix/upload_info.hpp"
struct image_info;
struct video_info;
struct file_info;
struct audio_info;
namespace matrix::detail{ namespace matrix::detail{
raii::string _image_body(const image_info& image); raii::string _image_body(const uploaded_image& image);
raii::string _video_body(const video_info& video); raii::string _video_body(const uploaded_video& video);
raii::string _file_body(const file_info& video); raii::string _file_body(const uploaded_file& video);
raii::string _audio_body(const audio_info& audio); raii::string _audio_body(const uploaded_audio& audio);
raii::string _message_body(const raii::string_base& text); raii::string _message_body(const raii::string_base& text);
} }

View File

@ -59,10 +59,10 @@ namespace matrix{
//sending events //sending events
raii::rjp_string send_custom_event(const raii::string_base& event, const raii::string_base& eventtype)const; raii::rjp_string send_custom_event(const raii::string_base& event, const raii::string_base& eventtype)const;
raii::rjp_string send_message(const raii::string_base& text)const; raii::rjp_string send_message(const raii::string_base& text)const;
raii::rjp_string send_file(const file_info& file)const; raii::rjp_string send_file(const uploaded_file& file)const;
raii::rjp_string send_image(const image_info& image)const; raii::rjp_string send_image(const uploaded_image& image)const;
raii::rjp_string send_video(const video_info& video)const; raii::rjp_string send_video(const uploaded_video& video)const;
raii::rjp_string send_audio(const audio_info& audio)const; raii::rjp_string send_audio(const uploaded_audio& audio)const;
bool send_typing(bool active, int timeout = 5000)const; bool send_typing(bool active, int timeout = 5000)const;
bool send_read_receipt(const raii::string_base& eventid)const; bool send_read_receipt(const raii::string_base& eventid)const;

View File

@ -26,6 +26,8 @@
namespace matrix{ namespace matrix{
struct client;
struct file_details{ struct file_details{
raii::binary data; raii::binary data;
raii::string name; raii::string name;
@ -39,28 +41,107 @@ namespace matrix{
raii::string mimetype; raii::string mimetype;
}; };
struct file_info{ class uploaded_file
raii::rjp_string fileurl; {
raii::string filename; friend class matrix::client;
raii::string mimetype; protected:
size_t filesize; raii::rjp_string m_fileurl;
}; raii::string m_filename;
struct image_info : public file_info{ raii::string m_mimetype;
using file_info::operator=; size_t m_filesize = 0;
size_t width;
size_t height;
raii::rjp_string thumburl; public:
raii::string thumbmime; constexpr uploaded_file(void) = default;
size_t thumb_width; uploaded_file(const uploaded_file&) = default;
size_t thumb_height; uploaded_file(uploaded_file&&) = default;
size_t thumbsize; ~uploaded_file(void) = default;
uploaded_file& operator=(const uploaded_file&) = default;
uploaded_file& operator=(uploaded_file&&) = default;
constexpr operator bool(void)const{return m_fileurl;}
const raii::string& mimetype(void)const{return m_mimetype;}
const raii::string& name(void)const{return m_filename;}
const raii::rjp_string& url(void)const{return m_fileurl;}
constexpr size_t size(void)const{return m_filesize;}
}; };
struct video_info : public image_info{
using image_info::operator=; class thumbnail_info
{
friend class matrix::client;
protected:
raii::rjp_string m_url;
raii::string m_mimetype;
size_t m_width = 0;
size_t m_height = 0;
size_t m_size = 0;
public:
constexpr thumbnail_info(void) = default;
thumbnail_info(const thumbnail_info&) = default;
thumbnail_info(thumbnail_info&&) = default;
~thumbnail_info(void) = default;
thumbnail_info& operator=(const thumbnail_info&) = default;
thumbnail_info& operator=(thumbnail_info&&) = default;
constexpr operator bool(void)const{return m_url;}
constexpr size_t width(void)const{return m_width;}
constexpr size_t height(void)const{return m_height;}
constexpr size_t size(void)const{return m_size;}
const raii::rjp_string& url(void)const{return m_url;}
const raii::string& mimetype(void)const{return m_mimetype;}
}; };
struct audio_info : public file_info{ class uploaded_image : public uploaded_file
using file_info::operator=; {
friend class matrix::client;
protected:
size_t m_width = 0;
size_t m_height = 0;
thumbnail_info m_thumb;
public:
constexpr uploaded_image(void) = default;
uploaded_image(const uploaded_image&) = default;
uploaded_image(uploaded_image&&) = default;
~uploaded_image(void) = default;
uploaded_image& operator=(const uploaded_image&) = default;
uploaded_image& operator=(uploaded_image&&) = default;
/*uploaded_image& operator=(const uploaded_file& f){
uploaded_file::operator=(f);
return *this;
}
uploaded_image& operator=(uploaded_file&& f){
uploaded_file::operator=(std::move(f));
return *this;
}*/
using uploaded_file::operator=;
constexpr size_t width(void)const{return m_width;}
constexpr size_t height(void)const{return m_height;}
constexpr auto thumb_width(void)const{return m_thumb.width();}
constexpr auto thumb_height(void)const{return m_thumb.height();}
constexpr auto thumb_size(void)const{return m_thumb.size();}
decltype(auto) thumb_url(void)const{return m_thumb.url();}
decltype(auto) thumb_mimetype(void)const{return m_thumb.mimetype();}
};
struct uploaded_video : public uploaded_image{
friend class matrix::client;
uploaded_video& operator=(const uploaded_file& f){
uploaded_image::operator=(f);
return *this;
}
uploaded_video& operator=(uploaded_file&& f){
uploaded_image::operator=(std::move(f));
return *this;
}
};
struct uploaded_audio : public uploaded_file{
friend class matrix::client;
using uploaded_file::operator=;
}; };
} }

View File

@ -1,3 +1,21 @@
/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RAII_BINARY_HPP #ifndef RAII_BINARY_HPP
#define RAII_BINARY_HPP #define RAII_BINARY_HPP

View File

@ -1,3 +1,21 @@
/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RAII_BINARY_STRING_CONV_HPP #ifndef RAII_BINARY_STRING_CONV_HPP
#define RAII_BINARY_STRING_CONV_HPP #define RAII_BINARY_STRING_CONV_HPP

View File

@ -1,3 +1,21 @@
/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RAII_DEFAULT_ALLOCATOR_HPP #ifndef RAII_DEFAULT_ALLOCATOR_HPP
#define RAII_DEFAULT_ALLOCATOR_HPP #define RAII_DEFAULT_ALLOCATOR_HPP

View File

@ -88,32 +88,32 @@ namespace matrix{
} }
//upload media //upload media
file_info client::upload_file(const file_details& file)const{ uploaded_file client::upload_file(const file_details& file)const{
return _upload_file(file, raii::curl_llist{}); return _upload_file(file, raii::curl_llist{});
} }
image_info client::upload_image(const image_details& file)const{ uploaded_image client::upload_image(const image_details& file)const{
image_info ret = {}; uploaded_image ret = {};
raii::curl_llist headers(raii::string("Content-Type: "_ss + file.mimetype)); raii::curl_llist headers(raii::string("Content-Type: "_ss + file.mimetype));
ret = _upload_file(file, headers); ret = _upload_file(file, headers);
ret.width = file.width; ret.m_width = file.width;
ret.height = file.height; ret.m_height = file.height;
ret.mimetype = file.mimetype; ret.m_mimetype = file.mimetype;
return ret; return ret;
} }
audio_info client::upload_audio(const audio_details& file)const{ uploaded_audio client::upload_audio(const audio_details& file)const{
audio_info ret = {}; uploaded_audio ret = {};
raii::curl_llist headers(raii::string("Content-Type: "_ss + file.mimetype)); raii::curl_llist headers(raii::string("Content-Type: "_ss + file.mimetype));
ret = _upload_file(file, headers); ret = _upload_file(file, headers);
ret.mimetype = file.mimetype; ret.m_mimetype = file.mimetype;
return ret; return ret;
} }
video_info client::upload_video(const video_details& file)const{ uploaded_video client::upload_video(const video_details& file)const{
video_info ret = {}; uploaded_video ret = {};
raii::curl_llist headers(raii::string("Content-Type: "_ss + file.mimetype)); raii::curl_llist headers(raii::string("Content-Type: "_ss + file.mimetype));
ret = _upload_file(file, headers); ret = _upload_file(file, headers);
ret.width = file.width; ret.m_width = file.width;
ret.height = file.height; ret.m_height = file.height;
ret.mimetype = file.mimetype; ret.m_mimetype = file.mimetype;
return ret; return ret;
} }
@ -126,35 +126,35 @@ namespace matrix{
return size*nmemb; return size*nmemb;
} }
bool client::create_thumbnail(image_info& info)const{ bool client::create_thumbnail(uploaded_image& info)const{
image_details i; image_details i;
raii::string reply_header; raii::string reply_header;
if(info.thumb_width > info.width || info.thumb_height > info.height){ if(info.thumb_width() > info.width() || info.thumb_height() > info.height()){
info.thumburl = info.fileurl; info.m_thumb.m_url = info.m_fileurl;
info.thumbsize = info.filesize; info.m_thumb.m_size = info.m_filesize;
info.thumbmime = info.mimetype; info.m_thumb.m_mimetype = info.m_mimetype;
} }
m_curl.setopt(CURLOPT_HEADERFUNCTION, _thumbnail_header_callback); m_curl.setopt(CURLOPT_HEADERFUNCTION, _thumbnail_header_callback);
m_curl.setopt(CURLOPT_HEADERDATA, &reply_header); m_curl.setopt(CURLOPT_HEADERDATA, &reply_header);
i.data = _get_curl_binary(m_ses->urls.file_thumbnail(m_ses->homeserver, info.fileurl, info.thumb_width, info.thumb_height, "crop"_ss)); i.data = _get_curl_binary(m_ses->urls.file_thumbnail(m_ses->homeserver, info.m_fileurl, info.m_thumb.m_width, info.m_thumb.m_height, "crop"_ss));
m_curl.setopt(CURLOPT_HEADERFUNCTION, NULL); m_curl.setopt(CURLOPT_HEADERFUNCTION, NULL);
m_curl.setopt(CURLOPT_HEADERDATA, NULL); m_curl.setopt(CURLOPT_HEADERDATA, NULL);
if(!i.data){ if(!i.data){
return false; return false;
} }
i.width = info.thumb_width; i.width = info.thumb_width();
i.height = info.thumb_height; i.height = info.thumb_height();
i.mimetype = std::move(reply_header); i.mimetype = std::move(reply_header);
image_info thumb_data = upload_image(i); uploaded_image thumb_data = upload_image(i);
if(!thumb_data.fileurl) if(!thumb_data.m_fileurl)
return false; return false;
info.thumburl = std::move(thumb_data.fileurl); info.m_thumb.m_url = std::move(thumb_data.m_fileurl);
info.thumbsize = thumb_data.filesize; info.m_thumb.m_size = thumb_data.m_filesize;
info.thumbmime = std::move(thumb_data.mimetype); info.m_thumb.m_mimetype = std::move(thumb_data.m_mimetype);
return true; return true;
} }
bool client::create_thumbnail(video_info& info)const{ bool client::create_thumbnail(uploaded_video& info)const{
return create_thumbnail(static_cast<image_info&>(info)); return create_thumbnail(static_cast<uploaded_image&>(info));
} }
/******************************* /*******************************
@ -182,9 +182,9 @@ namespace matrix{
memcpy(data->get()+oldlen, ptr, size*nmemb); memcpy(data->get()+oldlen, ptr, size*nmemb);
return size*nmemb; return size*nmemb;
} }
file_info client::_upload_file(const file_details& file, const raii::curl_llist& header)const{ uploaded_file client::_upload_file(const file_details& file, const raii::curl_llist& header)const{
raii::string fileurl; raii::string fileurl;
file_info retval = {}; uploaded_file retval = {};
internal_upload_data upload_data = {file.data.get(), file.data.size()}; internal_upload_data upload_data = {file.data.get(), file.data.size()};
m_curl.postreq(); m_curl.postreq();
m_curl.setopt(CURLOPT_POSTFIELDS, NULL); m_curl.setopt(CURLOPT_POSTFIELDS, NULL);
@ -211,9 +211,9 @@ namespace matrix{
RJP_search_res res = rjp_search_member(root.get(), "content_uri", 0); RJP_search_res res = rjp_search_member(root.get(), "content_uri", 0);
if(!res.value) if(!res.value)
return {}; return {};
retval.fileurl = res.value; retval.m_fileurl = res.value;
retval.filename = file.name; retval.m_filename = file.name;
retval.filesize = file.data.size(); retval.m_filesize = file.data.size();
return retval; return retval;
} }
} }

View File

@ -23,81 +23,81 @@
namespace matrix::detail{ namespace matrix::detail{
raii::string _image_body(const image_info& image){ raii::string _image_body(const uploaded_image& image){
raii::string url = raii::json_escape(image.fileurl); raii::string url = raii::json_escape(image.url());
const raii::string_base* thumburl; const raii::string_base* thumburl;
if(image.thumburl) if(image.thumb_url())
thumburl = &image.thumburl; thumburl = &image.thumb_url();
else else
thumburl = &url; thumburl = &url;
//compiler intensive //compiler intensive
return raii::string( return raii::string(
"{" "{"
"\"body\":\"" + raii::json_escape(image.filename) + "\"," "\"body\":\"" + raii::json_escape(image.name()) + "\","
"\"info\":{" "\"info\":{"
"\"h\":" + raii::itostr(image.height) + "," "\"h\":" + raii::itostr(image.height()) + ","
"\"mimetype\":\"" + image.mimetype + "\"," "\"mimetype\":\"" + image.mimetype() + "\","
"\"size\":" + raii::itostr(image.filesize) + "," "\"size\":" + raii::itostr(image.size()) + ","
"\"thumnail_info\":{" "\"thumnail_info\":{"
"\"h\":" + raii::itostr(image.thumb_height) + "," "\"h\":" + raii::itostr(image.thumb_height()) + ","
"\"mimetype\":\"" + image.mimetype + "\"," "\"mimetype\":\"" + image.mimetype() + "\","
"\"size\":" + raii::itostr(image.thumbsize) + "," "\"size\":" + raii::itostr(image.thumb_size()) + ","
"\"w\":" + raii::itostr(image.thumb_width) + "\"w\":" + raii::itostr(image.thumb_width()) +
"}," "},"
"\"thumbnail_url\":\"" + (*thumburl) + "\"," "\"thumbnail_url\":\"" + (*thumburl) + "\","
"\"w\":" + raii::itostr(image.width) + "\"w\":" + raii::itostr(image.width()) +
"}," "},"
"\"msgtype\":\"m.image\"," "\"msgtype\":\"m.image\","
"\"url\":\"" + url + "\"" "\"url\":\"" + url + "\""
"}"); "}");
} }
raii::string _video_body(const video_info& video){ raii::string _video_body(const uploaded_video& video){
return raii::string( return raii::string(
"{" "{"
"\"body\":\"" + raii::json_escape(video.filename) + "\"," "\"body\":\"" + raii::json_escape(video.name()) + "\","
"\"info\":{" "\"info\":{"
"\"h\":" + raii::itostr(video.height) + "," "\"h\":" + raii::itostr(video.height()) + ","
"\"mimetype\":\"" + video.mimetype + "\"," "\"mimetype\":\"" + video.mimetype() + "\","
"\"size\":" + raii::itostr(video.filesize) + "," "\"size\":" + raii::itostr(video.size()) + ","
"\"thumnail_info\":{" "\"thumnail_info\":{"
"\"h\":" + raii::itostr(video.thumb_height) + "," "\"h\":" + raii::itostr(video.thumb_height()) + ","
"\"mimetype\":\"image/jpeg\"," "\"mimetype\":\"image/jpeg\","
"\"size\":" + raii::itostr(video.thumbsize) + "," "\"size\":" + raii::itostr(video.thumb_size()) + ","
"\"w\":" + raii::itostr(video.thumb_width) + "\"w\":" + raii::itostr(video.thumb_width()) +
"}," "},"
"\"thumbnail_url\":\"" + video.thumburl + "\"," "\"thumbnail_url\":\"" + video.thumb_url() + "\","
"\"w\":" + raii::itostr(video.width) + "\"w\":" + raii::itostr(video.width()) +
"}," "},"
"\"msgtype\":\"m.video\"," "\"msgtype\":\"m.video\","
"\"url\":\"" + raii::json_escape(video.fileurl) + "\"" "\"url\":\"" + raii::json_escape(video.url()) + "\""
"}"); "}");
} }
raii::string _file_body(const file_info& file){ raii::string _file_body(const uploaded_file& file){
return raii::string( return raii::string(
"{" "{"
"\"body\":\"" + raii::json_escape(file.filename) + "\"," "\"body\":\"" + raii::json_escape(file.name()) + "\","
"\"info\":{" "\"info\":{"
"\"size\":" + raii::itostr(file.filesize) + "\"size\":" + raii::itostr(file.size()) +
"}," "},"
"\"msgtype\":\"m.file\"," "\"msgtype\":\"m.file\","
"\"body\":\"" + file.filename + "\"," "\"body\":\"" + file.name() + "\","
"\"url\":\"" + raii::json_escape(file.fileurl) + "\"" "\"url\":\"" + raii::json_escape(file.url()) + "\""
"}"); "}");
} }
raii::string _audio_body(const audio_info& audio){ raii::string _audio_body(const uploaded_audio& audio){
return raii::string( return raii::string(
"{" "{"
"\"body\":\"" + raii::json_escape(audio.filename) + "\"," "\"body\":\"" + raii::json_escape(audio.name()) + "\","
"\"info\":{" "\"info\":{"
"\"mimetype\":\"" + raii::json_escape(audio.mimetype) + "\"," "\"mimetype\":\"" + raii::json_escape(audio.mimetype()) + "\","
"\"size\":" + raii::itostr(audio.filesize) + "\"size\":" + raii::itostr(audio.size()) +
"}," "},"
"\"msgtype\":\"m.audio\"," "\"msgtype\":\"m.audio\","
"\"body\":\"" + audio.filename + "\"," "\"body\":\"" + audio.name() + "\","
"\"url\":\"" + raii::json_escape(audio.fileurl) + "\"" "\"url\":\"" + raii::json_escape(audio.url()) + "\""
"}"); "}");
} }

View File

@ -74,16 +74,16 @@ namespace matrix{
raii::rjp_string roomcxn::send_message(const raii::string_base& text)const{ raii::rjp_string roomcxn::send_message(const raii::string_base& text)const{
return _send_message(detail::_message_body(text)); return _send_message(detail::_message_body(text));
} }
raii::rjp_string roomcxn::send_file(const file_info& file)const{ raii::rjp_string roomcxn::send_file(const uploaded_file& file)const{
return _send_message(detail::_file_body(file)); return _send_message(detail::_file_body(file));
} }
raii::rjp_string roomcxn::send_image(const image_info& image)const{ raii::rjp_string roomcxn::send_image(const uploaded_image& image)const{
return _send_message(detail::_image_body(image)); return _send_message(detail::_image_body(image));
} }
raii::rjp_string roomcxn::send_video(const video_info& video)const{ raii::rjp_string roomcxn::send_video(const uploaded_video& video)const{
return _send_message(detail::_video_body(video)); return _send_message(detail::_video_body(video));
} }
raii::rjp_string roomcxn::send_audio(const audio_info& audio)const{ raii::rjp_string roomcxn::send_audio(const uploaded_audio& audio)const{
return _send_message(detail::_audio_body(audio)); return _send_message(detail::_audio_body(audio));
} }
bool roomcxn::send_typing(bool active, int timeout)const{ bool roomcxn::send_typing(bool active, int timeout)const{

View File

@ -1,3 +1,21 @@
/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "raii/binary.hpp" #include "raii/binary.hpp"
namespace raii{ namespace raii{