ferencd@0: #include "file_sender.h" ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: ferencd@0: file_sender::file_sender(tnt::HttpRequest& request, tnt::HttpReply& reply, const std::string& file) ferencd@0: : web_component(request, reply, ""), mfile(file) ferencd@0: {} ferencd@0: ferencd@0: file_sender &file_sender::templatize() ferencd@0: { ferencd@0: mneeds_templates = true; ferencd@0: return *this; ferencd@0: } ferencd@0: ferencd@0: static std::string fn = ""; ferencd@0: ferencd@0: unsigned file_sender::send() ferencd@0: { ferencd@0: ferencd@0: char cCurrentPath[FILENAME_MAX] = {0}; ferencd@0: ferencd@0: if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) ferencd@0: { ferencd@0: return internalServerError(); ferencd@0: } ferencd@0: ferencd@0: std::string filename(cCurrentPath); ferencd@0: if(mfile[0] != '/' && filename[filename.length() - 1] != '/') ferencd@0: { ferencd@0: filename += '/'; ferencd@0: } ferencd@0: filename += mfile; ferencd@0: ferencd@0: std::ifstream fin(filename.c_str(), std::ios::in | std::ios::binary); ferencd@0: ferencd@0: if(fin) ferencd@0: { ferencd@0: if(mneeds_templates) ferencd@0: { ferencd@0: //TODO mutex! ferencd@0: fn = filename; ferencd@0: GENERIC_FILE_TEMPLATE(FileSender, fn); ferencd@0: std::string res = templater().templatize().get(); ferencd@0: mreply.out() << res; ferencd@0: } ferencd@0: else ferencd@0: { ferencd@0: mreply.setContentType(mimeDb.getMimetype(mfile)); ferencd@0: std::ostringstream oss; ferencd@0: oss << fin.rdbuf(); ferencd@0: std::string fileData(oss.str()); ferencd@0: mreply.out() << fileData; // send the file ferencd@0: } ferencd@0: ferencd@0: return HTTP_OK; ferencd@0: } ferencd@0: else ferencd@0: { ferencd@0: if(boost::starts_with(mfile, theme_path)) ferencd@0: { ferencd@0: return HTTP_NOT_FOUND; ferencd@0: } ferencd@0: ferencd@0: // see if the file is actually from the current theme ferencd@0: unsigned current_theme_file_send = file_sender(mrequest, mreply, theme_path + mfile).send(); ferencd@0: return current_theme_file_send; ferencd@0: } ferencd@0: }