From 852a6bb1b358419b69f37c958d0ab94aaa6dc4f0 Mon Sep 17 00:00:00 2001 From: keita Date: Wed, 10 Mar 2021 23:59:01 +0900 Subject: [PATCH] =?UTF-8?q?media/upload=E3=82=92=E5=8F=A9=E3=81=8F?= =?UTF-8?q?=E3=82=84=E3=81=A4(#56)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cocoatweet/api/media/upload.cc | 78 ++++++++++++++++++++++++ src/cocoatweet/api/media/upload.h | 24 ++++---- src/cocoatweet/api/model/mediaStore.cc | 74 +++++++++++++++++++++++ src/cocoatweet/api/model/mediaStore.h | 82 ++++++++++++++++++++++++++ src/cocoatweet/api/model/tweet.h | 50 +++++++++++++++- 5 files changed, 294 insertions(+), 14 deletions(-) create mode 100644 src/cocoatweet/api/model/mediaStore.cc create mode 100644 src/cocoatweet/api/model/mediaStore.h diff --git a/src/cocoatweet/api/media/upload.cc b/src/cocoatweet/api/media/upload.cc index e69de29..64d77c3 100644 --- a/src/cocoatweet/api/media/upload.cc +++ b/src/cocoatweet/api/media/upload.cc @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +namespace CocoaTweet::API::Medias { +Upload::Upload() { + url_ = "https://upload.twitter.com/1.1/media/upload.json"; +} + +void Upload::media(const std::string& _media) { + media_ = _media; +} + +void Upload::mediaId(const std::string& _mediaId) {} + +void Upload::process(std::weak_ptr _oauth) { + std::ifstream ifs(media_, std::ios::binary); + ifs.seekg(0, std::ios::end); + unsigned long long size = ifs.tellg(); + bodyParam_.insert_or_assign("total_bytes", std::to_string(size)); + ifs.seekg(0); + std::string data((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + ifs.close(); + + // INIT + { + contentType_ = "application/x-www-form-urlencoded"; + // contentType_ = "multipart/form-data"; + bodyParam_.insert_or_assign("command", "INIT"); + bodyParam_.insert_or_assign("media_type", "image/jpeg"); + + CocoaTweet::API::Model::MediaStore media; + HttpPost::process(_oauth, + [&media](const unsigned int _responseCode, const std::string& _rsv) { + std::cout << _rsv << std::endl; + media = CocoaTweet::API::Model::MediaStore::parse(_responseCode, _rsv); + }); + + bodyParam_.insert_or_assign("media_id", media.id()); + } + + // APPEND + { + contentType_ = "multipart/form-data"; + bodyParam_.erase("media_type"); + bodyParam_.erase("total_bytes"); + + unsigned int segment = 0; + const unsigned long long chunk = 1024 * 512; + while (segment * chunk < data.size()) { + bodyParam_.insert_or_assign("command", "APPEND"); + bodyParam_.insert_or_assign("segment_index", std::to_string(segment)); + bodyParam_.insert_or_assign("media", data.substr(segment * chunk, chunk)); + CocoaTweet::API::Model::MediaStore media; + HttpPost::process(_oauth, [](const unsigned int _responseCode, const std::string& _rsv) { + // std::cout << _responseCode << std::endl << _rsv<< std::endl; + }); + segment++; + } + } + + // FINALIZE + { + contentType_ = "application/x-www-form-urlencoded"; + bodyParam_.insert_or_assign("command", "FINALIZE"); + bodyParam_.erase("segment_index"); + bodyParam_.erase("media"); + CocoaTweet::API::Model::MediaStore media; + HttpPost::process(_oauth, [](const unsigned int _responseCode, const std::string& _rsv) { + std::cout << _rsv << std::endl; + }); + } + + // STATUS if needed + {} +} +} // namespace CocoaTweet::API::Medias diff --git a/src/cocoatweet/api/media/upload.h b/src/cocoatweet/api/media/upload.h index d2c7554..4f19009 100644 --- a/src/cocoatweet/api/media/upload.h +++ b/src/cocoatweet/api/media/upload.h @@ -1,22 +1,20 @@ #ifndef COCOATWEET_API_MEDIA_UPLOAD_H_ #define COCOATWEET_API_MEDIA_UPLOAD_H_ -#include +#include +#include namespace CocoaTweet::API::Medias { -class Upload : public postInterface { -public: - enum Command : class st::string { - INIT = "INIT", - APPEND = "APPEND", - FINALIZE = "FINALIZE", - }; +class Upload : public CocoaTweet::API::Interface::HttpPost { +private: + std::string media_; - Upload::Upload(); +public: + Upload(); void media(const std::string& _media); - void mediaId(const std::string _mediaId); - void process(std::weak_ptr _oauth, Command _command); -} + void mediaId(const std::string& _mediaId); + void process(std::weak_ptr _oauth); +}; } // namespace CocoaTweet::API::Medias -#endif \ No newline at end of file +#endif diff --git a/src/cocoatweet/api/model/mediaStore.cc b/src/cocoatweet/api/model/mediaStore.cc new file mode 100644 index 0000000..03b3e31 --- /dev/null +++ b/src/cocoatweet/api/model/mediaStore.cc @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include +#include "nlohmann/json.hpp" + +namespace CocoaTweet::API::Model { +MediaStore MediaStore::parse(const unsigned int _responseCode, const std::string& _json) { + auto j = nlohmann::json::parse(_json); + MediaStore media; + + if (_responseCode / 100 == 2) { + if(j.count("media_id_string") != 0){ + media.id(j["media_id_string"]); + } + + if(j.count("size") != 0){ + media.size(j["size"]); + } + + if(j.count("expires_after_secs") != 0){ + media.expires(j["expires_after_secs"]); + } + + if(j.count("processing_info") == 0){ + media.state("succeeded"); + }else{ + media.state(j["processing_info"]["state"]); + media.remain(j["processing_info"]["check_after_secs"].get()); + } + } else { + } + + return media; +} + +void MediaStore::id(const std::string _id) { + id_ = _id; +} + +void MediaStore::size(const unsigned int _size){ +size_ = _size; +} + +void MediaStore::expires(const unsigned int _ex) { + expires_ = _ex; +} +void MediaStore::state(const std::string _state) { + state_ = _state; +} + +void MediaStore::remain(const unsigned int _remain){ + remain_ = _remain; +} + +const std::string MediaStore::id() const { + return id_; +} +const unsigned int MediaStore::size() const { + return size_; +} +const unsigned int MediaStore::expire() const { + return expires_; +} +const std::string MediaStore::state() const { + return state_; +} + +const unsigned int MediaStore::remain() const{ + return remain_; +} +} // namespace CocoaTweet::API::Model diff --git a/src/cocoatweet/api/model/mediaStore.h b/src/cocoatweet/api/model/mediaStore.h new file mode 100644 index 0000000..e060cc6 --- /dev/null +++ b/src/cocoatweet/api/model/mediaStore.h @@ -0,0 +1,82 @@ +#ifndef COCOATWEET_API_MODEL_MEDIASTORE_H_ +#define COCOATWEET_API_MODEL_MEDIASTORE_H_ + +#include + +namespace CocoaTweet::API::Model { + +/// @brief data class for tweet object +class MediaStore final { +public: + /// @brief constructor + MediaStore() = default; + + /// @brief copy constructor + MediaStore(const MediaStore&) = default; + + /// @brief constructor for create object from json response + /// @param[in] const unsigned int _responseCode : http status code which received when post request + /// @param[in] const std::string& _json : received content from twitter endpoint + MediaStore(const unsigned int _responseCode, const std::string& _json) + : MediaStore(MediaStore::parse(_responseCode, _json)) {} + + /// @brief response parser for tweet object + /// @param[in] const unsigned int _responseCode : http status code which received when post request + /// @param[in] const std::string& _json : received content from twitter endpoint + /// @param[out] CocoaTweet::API::Model::Tweet + static MediaStore parse(const unsigned int _responseCode, const std::string& _json); + + /// @brief set id of tweet + /// @param[in] const std::string _id : tweet id to set + /// @param[out] none + void id(const std::string _id); + + /// @brief set created time of tweet + /// @param[in] const std::string _at : tweet created time to set + /// @param[out] none + void size(const unsigned int _size); + + /// @brief set tweet text + /// @param[in] const std::string _text : text of tweet to set + /// @param[out] none + void expires(const unsigned int _ex); + + /// @brief set tweet source + /// @param[in] const std::string _source : source information to set + /// @param[out] none + void state(const std::string _state); + + void remain(const unsigned int _remain); + + /// @brief get tweet id + /// @param[in] none + /// @param[out] const std::string : tweet id + const std::string id() const; + + /// @brief get tweet create time + /// @param[in] none + /// @param[out] const std::string : time of tweet created time + const unsigned int size() const; + + /// @brief get tweet text + /// @param[in] none + /// @param[out] const std::string : tweet text + const unsigned int expire() const; + + /// @brief get tweet source information + /// @param[in] none + /// @param[out] const std::string : source information + const std::string state() const; + + const unsigned int remain() const; + +private: + std::string id_; + unsigned long long size_; + unsigned long long expires_; + std::string state_; + unsigned long long remain_; +}; +} // namespace CocoaTweet::API::Model + +#endif diff --git a/src/cocoatweet/api/model/tweet.h b/src/cocoatweet/api/model/tweet.h index ca80b95..5ad6b6c 100644 --- a/src/cocoatweet/api/model/tweet.h +++ b/src/cocoatweet/api/model/tweet.h @@ -4,20 +4,68 @@ #include namespace CocoaTweet::API::Model { + +/// @brief data class for tweet object class Tweet final { public: - Tweet() = default; + /// @brief constructor + Tweet() = default; + + /// @brief copy constructor Tweet(const Tweet&) = default; + + /// @brief constructor for create object from json response + /// @param[in] const unsigned int _responseCode : http status code which received when post + /// request + /// @param[in] const std::string& _json : received content from twitter endpoint Tweet(const unsigned int _responseCode, const std::string& _json) : Tweet(Tweet::parse(_responseCode, _json)) {} + + /// @brief response parser for tweet object + /// @param[in] const unsigned int _responseCode : http status code which received when post + /// request + /// @param[in] const std::string& _json : received content from twitter endpoint + /// @param[out] CocoaTweet::API::Model::Tweet static Tweet parse(const unsigned int _responseCode, const std::string& _json); + + /// @brief set id of tweet + /// @param[in] const std::string _id : tweet id to set + /// @param[out] none void id(const std::string _id); + + /// @brief set created time of tweet + /// @param[in] const std::string _at : tweet created time to set + /// @param[out] none void createdAt(const std::string _at); + + /// @brief set tweet text + /// @param[in] const std::string _text : text of tweet to set + /// @param[out] none void text(const std::string _text); + + /// @brief set tweet source + /// @param[in] const std::string _source : source information to set + /// @param[out] none void source(const std::string _source); + + /// @brief get tweet id + /// @param[in] none + /// @param[out] const std::string : tweet id const std::string id() const; + + /// @brief get tweet create time + /// @param[in] none + /// @param[out] const std::string : time of tweet created time const std::string createdAt() const; + + /// @brief get tweet text + /// @param[in] none + /// @param[out] const std::string : tweet text const std::string text() const; + + /// @brief get tweet source information + /// @param[in] none + /// @param[out] const std::string : source information const std::string source() const; private: