Fix the ambiguity of the contructors of FormPart.

master
Adam Gu 6 years ago
parent 381b148806
commit 2a20030f7e

@ -4,7 +4,7 @@
TEST(FormBodyTest, Payload) {
std::vector<webcc::FormPartPtr> parts{
std::make_shared<webcc::FormPart>("json", "{}", "application/json")
webcc::FormPart::New("json", "{}", "application/json")
};
webcc::FormBody form_body{ parts, "123456" };

@ -168,24 +168,37 @@ bool ContentDisposition::Init(const std::string& str) {
// -----------------------------------------------------------------------------
FormPart::FormPart(const std::string& name, const Path& path,
const std::string& media_type)
: name_(name), path_(path), media_type_(media_type) {
FormPartPtr FormPart::New(const std::string& name, std::string&& data,
const std::string& media_type) {
auto form_part = std::make_shared<FormPart>();
form_part->name_ = name;
form_part->data_ = std::move(data);
form_part->media_type_ = media_type;
return form_part;
}
FormPartPtr FormPart::NewFile(const std::string& name, const Path& path,
const std::string& media_type) {
auto form_part = std::make_shared<FormPart>();
form_part->name_ = name;
form_part->path_ = path;
form_part->media_type_ = media_type;
// Determine file name from file path.
// TODO: encoding
file_name_ = path.filename().string(std::codecvt_utf8<wchar_t>());
form_part->file_name_ = path.filename().string(std::codecvt_utf8<wchar_t>());
// Determine media type from file extension.
if (media_type_.empty()) {
std::string extension = path.extension().string();
// TODO: Default to "application/text"?
media_type_ = media_types::FromExtension(extension);
// TODO: Default to "application/text"?
if (form_part->media_type_.empty()) {
auto ext = path.extension().string();
form_part->media_type_ = media_types::FromExtension(ext);
}
}
FormPart::FormPart(const std::string& name, std::string&& data,
const std::string& media_type)
: name_(name), data_(std::move(data)), media_type_(media_type) {
return form_part;
}
void FormPart::Prepare(Payload* payload) {

@ -135,26 +135,29 @@ private:
// -----------------------------------------------------------------------------
class FormPart;
using FormPartPtr = std::shared_ptr<FormPart>;
// A part of the multipart form data.
class FormPart {
public:
FormPart() = default;
// Construct a file part.
// The file name will be extracted from path.
// The media type, if not provided, will be inferred from file extension.
FormPart(const std::string& name, const Path& path,
const std::string& media_type = "");
FormPart(const FormPart&) = delete;
FormPart& operator=(const FormPart&) = delete;
// Construct a non-file part.
// The data will be moved, no file name is needed.
// The media type is optional. If the data is a JSON string, you can specify
// media type as "application/json".
FormPart(const std::string& name, std::string&& data,
const std::string& media_type = "");
static FormPartPtr New(const std::string& name, std::string&& data,
const std::string& media_type = "");
FormPart(const FormPart&) = delete;
FormPart& operator=(const FormPart&) = delete;
// Construct a file part.
// The file name will be extracted from path.
// The media type, if not provided, will be inferred from file extension.
static FormPartPtr NewFile(const std::string& name, const Path& path,
const std::string& media_type = "");
// API: SERVER
const std::string& name() const {
@ -243,8 +246,6 @@ private:
std::string data_;
};
using FormPartPtr = std::shared_ptr<FormPart>;
} // namespace webcc
#endif // WEBCC_COMMON_H_

@ -55,8 +55,7 @@ RequestBuilder& RequestBuilder::File(const std::string& name,
const Path& path,
const std::string& media_type) {
assert(!name.empty());
auto part = std::make_shared<FormPart>(name, path, media_type);
form_parts_.push_back(part);
form_parts_.push_back(FormPart::NewFile(name, path, media_type));
return *this;
}
@ -64,8 +63,7 @@ RequestBuilder& RequestBuilder::Form(const std::string& name,
std::string&& data,
const std::string& media_type) {
assert(!name.empty());
auto part = std::make_shared<FormPart>(name, std::move(data), media_type);
form_parts_.push_back(part);
form_parts_.push_back(FormPart::New(name, std::move(data), media_type));
return *this;
}

Loading…
Cancel
Save