Mercurial > thymian
comparison server/file_sender.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 #include "file_sender.h" | |
| 2 #include <templater.h> | |
| 3 #include <log.h> | |
| 4 #include <unistd.h> | |
| 5 #include <fstream> | |
| 6 #include <boost/algorithm/string.hpp> | |
| 7 | |
| 8 file_sender::file_sender(tnt::HttpRequest& request, tnt::HttpReply& reply, const std::string& file) | |
| 9 : web_component(request, reply, ""), mfile(file) | |
| 10 {} | |
| 11 | |
| 12 file_sender &file_sender::templatize() | |
| 13 { | |
| 14 mneeds_templates = true; | |
| 15 return *this; | |
| 16 } | |
| 17 | |
| 18 static std::string fn = ""; | |
| 19 | |
| 20 unsigned file_sender::send() | |
| 21 { | |
| 22 | |
| 23 char cCurrentPath[FILENAME_MAX] = {0}; | |
| 24 | |
| 25 if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) | |
| 26 { | |
| 27 return internalServerError(); | |
| 28 } | |
| 29 | |
| 30 std::string filename(cCurrentPath); | |
| 31 if(mfile[0] != '/' && filename[filename.length() - 1] != '/') | |
| 32 { | |
| 33 filename += '/'; | |
| 34 } | |
| 35 filename += mfile; | |
| 36 | |
| 37 std::ifstream fin(filename.c_str(), std::ios::in | std::ios::binary); | |
| 38 | |
| 39 if(fin) | |
| 40 { | |
| 41 if(mneeds_templates) | |
| 42 { | |
| 43 //TODO mutex! | |
| 44 fn = filename; | |
| 45 GENERIC_FILE_TEMPLATE(FileSender, fn); | |
| 46 std::string res = templater<FileSender>().templatize().get(); | |
| 47 mreply.out() << res; | |
| 48 } | |
| 49 else | |
| 50 { | |
| 51 mreply.setContentType(mimeDb.getMimetype(mfile)); | |
| 52 std::ostringstream oss; | |
| 53 oss << fin.rdbuf(); | |
| 54 std::string fileData(oss.str()); | |
| 55 mreply.out() << fileData; // send the file | |
| 56 } | |
| 57 | |
| 58 return HTTP_OK; | |
| 59 } | |
| 60 else | |
| 61 { | |
| 62 if(boost::starts_with(mfile, theme_path)) | |
| 63 { | |
| 64 return HTTP_NOT_FOUND; | |
| 65 } | |
| 66 | |
| 67 // see if the file is actually from the current theme | |
| 68 unsigned current_theme_file_send = file_sender(mrequest, mreply, theme_path + mfile).send(); | |
| 69 return current_theme_file_send; | |
| 70 } | |
| 71 } |
