Remove HttpFile to FormPart; refine payload prepare.
parent
80d0c73617
commit
a96109c3b7
@ -1,64 +0,0 @@
|
||||
#include "webcc/http_file.h"
|
||||
|
||||
#include "boost/filesystem/fstream.hpp"
|
||||
|
||||
namespace bfs = boost::filesystem;
|
||||
|
||||
namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Read entire file into string.
|
||||
static bool ReadFile(const Path& path, std::string* output) {
|
||||
bfs::ifstream ifs{path, std::ios::binary | std::ios::ate};
|
||||
if (!ifs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto size = ifs.tellg();
|
||||
output->resize(static_cast<std::size_t>(size), '\0');
|
||||
ifs.seekg(0);
|
||||
ifs.read(&(*output)[0], size); // TODO: Error handling
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
HttpFile::HttpFile(const Path& path, const std::string& mime_type) {
|
||||
if (!ReadFile(path, &data_)) {
|
||||
throw Exception(kFileIOError, "Cannot read the file.");
|
||||
}
|
||||
|
||||
// Determine file name from file path.
|
||||
// TODO: Encoding
|
||||
file_name_ = path.filename().string();
|
||||
|
||||
// Determine content type from file extension.
|
||||
if (mime_type.empty()) {
|
||||
std::string extension = path.extension().string();
|
||||
mime_type_ = http::media_types::FromExtension(extension, false);
|
||||
} else {
|
||||
mime_type_ = mime_type;
|
||||
}
|
||||
}
|
||||
|
||||
HttpFile::HttpFile(std::string&& data, const std::string& file_name,
|
||||
const std::string& mime_type) {
|
||||
data_ = std::move(data);
|
||||
|
||||
file_name_ = file_name;
|
||||
|
||||
mime_type_ = mime_type;
|
||||
|
||||
// Determine content type from file extension.
|
||||
if (mime_type_.empty()) {
|
||||
std::size_t pos = file_name_.find_last_of('.');
|
||||
if (pos != std::string::npos) {
|
||||
std::string extension = file_name_.substr(pos + 1);
|
||||
mime_type_ = http::media_types::FromExtension(extension, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace webcc
|
@ -1,87 +0,0 @@
|
||||
#ifndef WEBCC_HTTP_FILE_H_
|
||||
#define WEBCC_HTTP_FILE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "boost/filesystem/path.hpp"
|
||||
|
||||
#include "webcc/globals.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
using Path = boost::filesystem::path;
|
||||
|
||||
// File for HTTP transfer (upload/download).
|
||||
class HttpFile {
|
||||
public:
|
||||
HttpFile() = default;
|
||||
|
||||
explicit HttpFile(const Path& path, const std::string& mime_type = "");
|
||||
|
||||
HttpFile(std::string&& data, const std::string& file_name,
|
||||
const std::string& mime_type = "");
|
||||
|
||||
#if WEBCC_DEFAULT_MOVE_COPY_ASSIGN
|
||||
|
||||
HttpFile(HttpFile&&) = default;
|
||||
HttpFile& operator=(HttpFile&&) = default;
|
||||
|
||||
#else
|
||||
|
||||
HttpFile(HttpFile&& rhs)
|
||||
: data_(std::move(rhs.data_)),
|
||||
file_name_(std::move(rhs.file_name_)),
|
||||
mime_type_(std::move(rhs.mime_type_)) {
|
||||
}
|
||||
|
||||
HttpFile& operator=(HttpFile&& rhs) {
|
||||
if (&rhs != this) {
|
||||
data_ = std::move(rhs.data_);
|
||||
file_name_ = std::move(rhs.file_name_);
|
||||
mime_type_ = std::move(rhs.mime_type_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif // WEBCC_DEFAULT_MOVE_COPY_ASSIGN
|
||||
|
||||
const std::string& data() const {
|
||||
return data_;
|
||||
}
|
||||
|
||||
void AppendData(const std::string& data) {
|
||||
data_.append(data);
|
||||
}
|
||||
|
||||
void AppendData(const char* data, std::size_t size) {
|
||||
data_.append(data, size);
|
||||
}
|
||||
|
||||
const std::string& file_name() const {
|
||||
return file_name_;
|
||||
}
|
||||
|
||||
void set_file_name(const std::string& file_name) {
|
||||
file_name_ = file_name;
|
||||
}
|
||||
|
||||
const std::string& mime_type() const {
|
||||
return mime_type_;
|
||||
}
|
||||
|
||||
private:
|
||||
// Binary file data.
|
||||
// TODO: don't use std::string?
|
||||
std::string data_;
|
||||
|
||||
// E.g., example.jpg
|
||||
// TODO: Unicode
|
||||
std::string file_name_;
|
||||
|
||||
// E.g., image/jpeg
|
||||
std::string mime_type_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_HTTP_FILE_H_
|
Loading…
Reference in New Issue