diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2022-06-17 02:46:55 -0400 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2022-06-17 02:46:55 -0400 |
commit | 4ee7025ab09b606a2556bf9f42c1218d7fd72843 (patch) | |
tree | 4b7747da4be59e672d649e254474d09cab0f01c0 /src/http.cpp | |
parent | d0fa308f6e339b94044d39bf0e76b8221da48c3a (diff) | |
download | abaddon-portaudio-4ee7025ab09b606a2556bf9f42c1218d7fd72843.tar.gz abaddon-portaudio-4ee7025ab09b606a2556bf9f42c1218d7fd72843.zip |
add file upload via dnd + rework http
Diffstat (limited to 'src/http.cpp')
-rw-r--r-- | src/http.cpp | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/http.cpp b/src/http.cpp index beb1944..14fe3f5 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -35,7 +35,8 @@ request::request(request &&other) noexcept , m_method(std::exchange(other.m_method, nullptr)) , m_header_list(std::exchange(other.m_header_list, nullptr)) , m_error_buf(other.m_error_buf) - , m_form(std::exchange(other.m_form, nullptr)) { + , m_form(std::exchange(other.m_form, nullptr)) + , m_read_streams(std::move(other.m_read_streams)) { // i think this is correct??? } @@ -82,12 +83,29 @@ void request::make_form() { m_form = curl_mime_init(m_curl); } +static size_t http_readfunc(char *buffer, size_t size, size_t nitems, void *arg) { + auto stream = Glib::wrap(G_FILE_INPUT_STREAM(arg), true); + int r = stream->read(buffer, size * nitems); + if (r == -1) { + // https://github.com/curl/curl/blob/ad9bc5976d6661cd5b03ebc379313bf657701c14/lib/mime.c#L724 + return size_t(-1); + } + return r; +} + // file must exist until request completes -void request::add_file(std::string_view name, std::string_view file_path, std::string_view filename) { +void request::add_file(std::string_view name, const Glib::RefPtr<Gio::File> &file, std::string_view filename) { + if (!file->query_exists()) return; + auto *field = curl_mime_addpart(m_form); curl_mime_name(field, name.data()); - curl_mime_filedata(field, file_path.data()); + auto info = file->query_info(); + auto stream = file->read(); + curl_mime_data_cb(field, info->get_size(), http_readfunc, nullptr, nullptr, stream->gobj()); curl_mime_filename(field, filename.data()); + + // hold ref + m_read_streams.insert(stream); } // copied |