From 85e0e991973594fb458410876a1cc09c5971b918 Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Thu, 26 Jan 2017 00:32:22 +0300 Subject: Helper method for loading files. Photo upload example. Fixes #24 --- src/tools/FileTools.cpp | 25 +++++++++++++++++++++++++ src/types/InputFile.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/tools/FileTools.cpp create mode 100644 src/types/InputFile.cpp (limited to 'src') diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp new file mode 100644 index 0000000..1522a9d --- /dev/null +++ b/src/tools/FileTools.cpp @@ -0,0 +1,25 @@ +// +// Created by Oleg on 25.01.17. +// + +#include "tgbot/tools/FileTools.h" + +#include +#include + +using namespace std; + +namespace FileTools { + +std::string read(const std::string& filePath) { + ifstream in(filePath, ios::in | ios::binary); + if (in) { + ostringstream contents; + contents << in.rdbuf(); + in.close(); + return contents.str(); + } + throw errno; +} + +}; diff --git a/src/types/InputFile.cpp b/src/types/InputFile.cpp new file mode 100644 index 0000000..0f6cb86 --- /dev/null +++ b/src/types/InputFile.cpp @@ -0,0 +1,24 @@ +// +// Created by Oleg Morozenkov on 25.01.17. +// + +#include "tgbot/types/InputFile.h" + +#include + +#include "tgbot/tools/StringTools.h" +#include "tgbot/tools/FileTools.h" + +using namespace std; + +namespace TgBot { + +InputFile::Ptr InputFile::fromFile(const string& filePath, const string& mimeType) { + InputFile::Ptr result(new InputFile); + result->data = FileTools::read(filePath); + result->mimeType = mimeType; + result->fileName = StringTools::split(filePath, '/').back(); + return result; +} + +}; -- cgit v1.2.3