30 lines
760 B
C++
30 lines
760 B
C++
#include "FileUtil.h"
|
|
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
std::optional<std::string> readTextFile(const std::filesystem::path& path)
|
|
{
|
|
std::ifstream file(path, std::ios::binary);
|
|
if (!file)
|
|
return std::nullopt;
|
|
|
|
std::ostringstream buffer;
|
|
buffer << file.rdbuf();
|
|
return buffer.str();
|
|
}
|
|
|
|
std::string contentTypeForPath(const std::filesystem::path& path)
|
|
{
|
|
const auto ext = path.extension().string();
|
|
if (ext == ".html")
|
|
return "text/html; charset=utf-8";
|
|
if (ext == ".css")
|
|
return "text/css; charset=utf-8";
|
|
if (ext == ".js")
|
|
return "application/javascript; charset=utf-8";
|
|
if (ext == ".json")
|
|
return "application/json; charset=utf-8";
|
|
return "application/octet-stream";
|
|
}
|