diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index b82b0b40..0f12b1d7 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -423,10 +423,11 @@ namespace Components return; } + std::string file; std::string fsGame = Dvar::Var("fs_game").get(); std::string path = Dvar::Var("fs_basepath").get() + "\\" + fsGame + "\\" + url; - if (fsGame.empty() || !Utils::IO::FileExists(path)) + if (fsGame.empty() || !Utils::IO::ReadFile(path, &file)) { mg_printf(nc, "HTTP/1.1 404 Not Found\r\n" @@ -437,8 +438,6 @@ namespace Components } else { - std::string file = Utils::IO::ReadFile(path); - mg_printf(nc, "HTTP/1.1 200 OK\r\n" "Content-Type: application/octet-stream\r\n" diff --git a/src/Utils/IO.cpp b/src/Utils/IO.cpp index ed3a0227..7b9ecdbf 100644 --- a/src/Utils/IO.cpp +++ b/src/Utils/IO.cpp @@ -9,7 +9,7 @@ namespace Utils return std::ifstream(file).good(); } - void WriteFile(std::string file, std::string data, bool append) + bool WriteFile(std::string file, std::string data, bool append) { auto pos = file.find_last_of("/\\"); if (pos != std::string::npos) @@ -23,17 +23,28 @@ namespace Utils { stream.write(data.data(), data.size()); stream.close(); + return true; } + + return false; } std::string ReadFile(std::string file) { - std::string buffer; + std::string data; + ReadFile(file, &data); + return data; + } + + bool ReadFile(std::string file, std::string* data) + { + if (!data) return false; + data->clear(); if (FileExists(file)) { std::ifstream stream(file, std::ios::binary); - if (!stream.is_open()) return buffer; + if (!stream.is_open()) return false; stream.seekg(0, std::ios::end); std::streamsize size = stream.tellg(); @@ -41,16 +52,16 @@ namespace Utils if (size > -1) { - buffer.clear(); - buffer.resize(static_cast(size)); - - stream.read(const_cast(buffer.data()), size); + data->resize(static_cast(size)); + stream.read(const_cast(data->data()), size); + stream.close(); + return true; } stream.close(); } - return buffer; + return false; } bool CreateDirectory(std::string dir) diff --git a/src/Utils/IO.hpp b/src/Utils/IO.hpp index 355fac8e..5ad50183 100644 --- a/src/Utils/IO.hpp +++ b/src/Utils/IO.hpp @@ -5,7 +5,8 @@ namespace Utils namespace IO { bool FileExists(std::string file); - void WriteFile(std::string file, std::string data, bool append = false); + bool WriteFile(std::string file, std::string data, bool append = false); + bool ReadFile(std::string file, std::string* data); std::string ReadFile(std::string file); bool CreateDirectory(std::string dir); std::vector ListFiles(std::string dir);