From f35598c8ae05b09ffe282887956606f513f3e379 Mon Sep 17 00:00:00 2001 From: keita Date: Sun, 14 Mar 2021 00:58:29 +0900 Subject: [PATCH] add unsupportedMediaTypeException --- src/cocoatweet/api/media/upload.cc | 7 +++++++ src/cocoatweet/api/model/mediaStore.cc | 7 ++----- src/cocoatweet/exception/exception.h | 1 + .../exception/unsupportedMediaTypeException.h | 12 ++++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/cocoatweet/exception/unsupportedMediaTypeException.h diff --git a/src/cocoatweet/api/media/upload.cc b/src/cocoatweet/api/media/upload.cc index 01dff7d..bd10fc0 100644 --- a/src/cocoatweet/api/media/upload.cc +++ b/src/cocoatweet/api/media/upload.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -21,6 +22,12 @@ void Upload::mediaId(const std::string& _mediaId) {} CocoaTweet::API::Model::MediaStore Upload::process( std::weak_ptr _oauth) { + auto extension = std::filesystem::path(media_).extension().string(); + if (mimeType.count(extension) == 0) { + throw new CocoaTweet::Exception::UnsupportedMediaTypeException( + std::string("media type \"" + extension + "\" is not supported media type")); + } + auto backup = bodyParam_; CocoaTweet::API::Model::MediaStore media; std::ifstream ifs(media_, std::ios::binary); diff --git a/src/cocoatweet/api/model/mediaStore.cc b/src/cocoatweet/api/model/mediaStore.cc index 5a7531f..10f2cd0 100644 --- a/src/cocoatweet/api/model/mediaStore.cc +++ b/src/cocoatweet/api/model/mediaStore.cc @@ -1,9 +1,5 @@ #include -#include -#include -#include -#include -#include +#include #include "nlohmann/json.hpp" namespace CocoaTweet::API::Model { @@ -31,6 +27,7 @@ MediaStore MediaStore::parse(const unsigned int _responseCode, const std::string media.remain(j["processing_info"]["check_after_secs"].get()); } } else { + throw new CocoaTweet::Exception::Exception(j["error"]); } return media; diff --git a/src/cocoatweet/exception/exception.h b/src/cocoatweet/exception/exception.h index 90683e3..35311ea 100644 --- a/src/cocoatweet/exception/exception.h +++ b/src/cocoatweet/exception/exception.h @@ -8,6 +8,7 @@ namespace CocoaTweet::Exception { class Exception : public std::exception { public: Exception(const char* _msg) : msg_(std::string(_msg)) {} + Exception(const std::string& _msg) : msg_(std::string(_msg)) {} const std::string& what() { return msg_; } diff --git a/src/cocoatweet/exception/unsupportedMediaTypeException.h b/src/cocoatweet/exception/unsupportedMediaTypeException.h new file mode 100644 index 0000000..97bf57d --- /dev/null +++ b/src/cocoatweet/exception/unsupportedMediaTypeException.h @@ -0,0 +1,12 @@ +#ifndef COCOATWEET_EXCEPTION_UNSUPPORTEDMEDIATYPEEXCEPTION_H_ +#define COCOATWEET_EXCEPTION_UNSUPPORTEDMEDIATYPEEXCEPTION_H_ + +#include + +namespace CocoaTweet::Exception { +class UnsupportedMediaTypeException final : Exception { + using Exception::Exception; +}; +} // namespace CocoaTweet::Exception + +#endif