Refine the support for uploading files.

This commit is contained in:
Chunting Gu
2019-04-04 17:36:06 +08:00
parent d1ddfa4b3d
commit 6f7acfc442
15 changed files with 222 additions and 88 deletions
+84
View File
@@ -1,9 +1,33 @@
#include "webcc/globals.h"
#include <fstream>
#include <map>
#include "boost/filesystem/path.hpp"
#include "webcc/version.h"
namespace webcc {
// -----------------------------------------------------------------------------
// Read entire file into string.
static bool ReadFile(const std::string& path, std::string* output) {
std::ifstream ifs{path, std::ios::binary | std::ios::ate};
if (!ifs) {
return false;
}
auto size = ifs.tellg();
output->resize(size, '\0');
ifs.seekg(0);
ifs.read(&(*output)[0], size); // TODO: Error handling
return true;
}
// -----------------------------------------------------------------------------
namespace http {
const std::string& UserAgent() {
@@ -11,6 +35,66 @@ const std::string& UserAgent() {
return s_user_agent;
}
File::File(const std::string& file_path) {
if (!ReadFile(file_path, &data)) {
throw Exception(kFileIOError, "Cannot read the file.");
}
namespace bfs = boost::filesystem;
// Determine file name from file path.
//if (file_name.empty()) {
file_name = bfs::path(file_path).filename().string();
//} else {
// file_name = file_name;
//}
// Determine content type from file extension.
//if (mime_type.empty()) {
std::string extension = bfs::path(file_path).extension().string();
mime_type = http::media_types::FromExtension(extension, false);
//} else {
//mime_type = mime_type;
//}
}
namespace media_types {
// TODO: Add more.
static void InitMap(std::map<std::string, std::string>& map) {
map["gif"] = "image/gif";
map["htm"] = "text/html";
map["html"] = "text/html";
map["jpg"] = "image/jpeg";
map["jpeg"] = "image/jpeg";
map["png"] = "image/png";
map["txt"] = "text/plain";
map[""] = "";
}
// TODO: Ignore case on Windows.
std::string FromExtension(const std::string& extension,
bool default_to_plain_text) {
static std::map<std::string, std::string> s_map;
if (s_map.empty()) {
InitMap(s_map);
}
auto it = s_map.find(extension);
if (it != s_map.end()) {
return it->second;
}
if (default_to_plain_text) {
return "text/plain";
} else {
return "";
}
}
} // namespace media_types
} // namespace http
const char* DescribeError(Error error) {