StaticWebPageController.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "../processor/processor.h"
  5. #include "../webuiconf.h"
  6. #include "../processor/templates/framework7/Framework7Document.hpp"
  7. #include <sys/stat.h>
  8. using namespace std;
  9. using namespace xc::processor;
  10. using namespace xc::processor::templates;
  11. using namespace xc::processor::templates::framework7;
  12. using namespace configor;
  13. namespace xc::controller {
  14. ContentGeneratorDefine({
  15. if (!conf::enableStaticAssetsController) { return false; }
  16. if (request.getMethod() != "GET") { return false; }
  17. struct stat buffer;
  18. string filePath = "html";
  19. if (request.getURLPath()[0] == '/') {
  20. filePath += request.getURLPath();
  21. } else {
  22. filePath += "/" + request.getURLPath();
  23. }
  24. if (stat(filePath.c_str(), &buffer) == 0) {
  25. if (S_ISREG(buffer.st_mode)) {
  26. return true;
  27. } else {
  28. for (auto file: conf::defaultFiles) {
  29. string newFilePath = filePath;
  30. if (filePath[filePath.length() - 1] == '/') {
  31. newFilePath += file;
  32. } else {
  33. newFilePath += "/" + file;
  34. }
  35. if (stat(newFilePath.c_str(), &buffer) == 0) {
  36. return S_ISREG(buffer.st_mode);
  37. }
  38. }
  39. }
  40. }
  41. return false;
  42. }, {
  43. struct stat buffer;
  44. string filePath = "html";
  45. if (request.getURLPath()[0] == '/') {
  46. filePath += request.getURLPath();
  47. } else {
  48. filePath += "/" + request.getURLPath();
  49. }
  50. if (stat(filePath.c_str(), &buffer) == 0) {
  51. if (S_ISREG(buffer.st_mode)) {
  52. return (ResponseData *) new BinaryResponseData(200, filePath,
  53. mimeTypeOfFile(filePath));
  54. } else {
  55. for (auto file: conf::defaultFiles) {
  56. string newFilePath = filePath;
  57. if (filePath[filePath.length() - 1] == '/') {
  58. newFilePath += file;
  59. } else {
  60. newFilePath += "/" + file;
  61. }
  62. if (stat(newFilePath.c_str(), &buffer) == 0) {
  63. if (S_ISREG(buffer.st_mode)) {
  64. return (ResponseData *) new BinaryResponseData(200, newFilePath, mimeTypeOfFile(newFilePath));
  65. }
  66. }
  67. }
  68. }
  69. }
  70. return (ResponseData *) new TemplateResponseData(conf::errorPage404);
  71. })
  72. }