From d29715c0ecd4b6ccbd52aaa1be0ad1ab250c10fc Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Fri, 6 Jul 2018 13:45:13 +0300 Subject: [PATCH] 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 --- src/interface.h | 4 ++-- src/laminar.cpp | 31 ++++++------------------------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/src/interface.h b/src/interface.h index 6b67f63..d08fae0 100644 --- a/src/interface.h +++ b/src/interface.h @@ -130,8 +130,8 @@ struct LaminarInterface { virtual kj::Own getArtefact(std::string path) = 0; // Fetches the content of $LAMINAR_HOME/custom/style.css or an empty - // string. This shouldn't be used, because the sysadmin should have - // configured a real webserver to serve these things. + // string. Ideally, this would instead be served by a proper web server + // which handles this url. virtual std::string getCustomCss() = 0; // Abort all running jobs diff --git a/src/laminar.cpp b/src/laminar.cpp index 6abef4f..db1972a 100644 --- a/src/laminar.cpp +++ b/src/laminar.cpp @@ -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(sz)); - fstr.seekg(0); - fstr.read(&output[0], sz); - return true; - } - return false; -} - class MappedFileImpl : public MappedFile { public: MappedFileImpl(const char* path) : @@ -877,10 +858,10 @@ kj::Own Laminar::getArtefact(std::string path) { return kj::heap(fs::path(fs::path(homeDir)/"archive"/path).c_str()); } -std::string Laminar::getCustomCss() -{ - fs::path file(fs::path(homeDir)/"custom"/"style.css"); - std::string result; - slurp(file, result); - return result; +std::string Laminar::getCustomCss() { + MappedFileImpl cssFile(fs::path(fs::path(homeDir)/"custom"/"style.css").c_str()); + if(cssFile.address()) { + return std::string(static_cast(cssFile.address()), cssFile.size()); + } + return std::string(); }