Reuse MappedFileImpl for reading custom css

This is nicer than slurping and removes a whole function. A further
improvement could be to retain the mapping permanently open.

Part of #49 refactor
pull/70/head
Oliver Giles 6 years ago
parent 758b5f2e46
commit d29715c0ec

@ -130,8 +130,8 @@ struct LaminarInterface {
virtual kj::Own<MappedFile> getArtefact(std::string path) = 0; virtual kj::Own<MappedFile> getArtefact(std::string path) = 0;
// Fetches the content of $LAMINAR_HOME/custom/style.css or an empty // Fetches the content of $LAMINAR_HOME/custom/style.css or an empty
// string. This shouldn't be used, because the sysadmin should have // string. Ideally, this would instead be served by a proper web server
// configured a real webserver to serve these things. // which handles this url.
virtual std::string getCustomCss() = 0; virtual std::string getCustomCss() = 0;
// Abort all running jobs // Abort all running jobs

@ -825,25 +825,6 @@ void Laminar::runFinished(Run * r) {
} }
} }
// Small helper function to return the full contents of a file given its path.
// It reads in the whole file into the given string reference.
// This is a terrible way to serve files (especially large ones). Hopefully
// no-one uses this function and configures their webservers appropriately.
static bool slurp(fs::path path, std::string& output) {
if(!fs::is_regular_file(path))
return false;
std::ifstream fstr(path.string());
fstr.seekg(0, std::ios::end);
ssize_t sz = fstr.tellg();
if(fstr.good()) {
output.resize(static_cast<size_t>(sz));
fstr.seekg(0);
fstr.read(&output[0], sz);
return true;
}
return false;
}
class MappedFileImpl : public MappedFile { class MappedFileImpl : public MappedFile {
public: public:
MappedFileImpl(const char* path) : MappedFileImpl(const char* path) :
@ -877,10 +858,10 @@ kj::Own<MappedFile> Laminar::getArtefact(std::string path) {
return kj::heap<MappedFileImpl>(fs::path(fs::path(homeDir)/"archive"/path).c_str()); return kj::heap<MappedFileImpl>(fs::path(fs::path(homeDir)/"archive"/path).c_str());
} }
std::string Laminar::getCustomCss() std::string Laminar::getCustomCss() {
{ MappedFileImpl cssFile(fs::path(fs::path(homeDir)/"custom"/"style.css").c_str());
fs::path file(fs::path(homeDir)/"custom"/"style.css"); if(cssFile.address()) {
std::string result; return std::string(static_cast<const char*>(cssFile.address()), cssFile.size());
slurp(file, result); }
return result; return std::string();
} }

Loading…
Cancel
Save