StaticWebPageController.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if (request.getURL().length() == 0) { return false; }
  18. struct stat buffer;
  19. string filePath = "html";
  20. if (request.getURL()[0] == '/') {
  21. filePath += request.getURL();
  22. } else {
  23. filePath += "/" + request.getURL();
  24. }
  25. if (stat(filePath.c_str(), &buffer) == 0) {
  26. if (S_ISREG(buffer.st_mode)) {
  27. return true;
  28. } else {
  29. for (auto file: conf::defaultFiles) {
  30. string newFilePath = filePath;
  31. if (filePath[filePath.length() - 1] == '/') {
  32. newFilePath += file;
  33. } else {
  34. newFilePath += "/" + file;
  35. }
  36. if (stat(newFilePath.c_str(), &buffer) == 0) {
  37. return S_ISREG(buffer.st_mode);
  38. }
  39. }
  40. }
  41. }
  42. return false;
  43. }, {
  44. struct stat buffer;
  45. string filePath = "html";
  46. if (request.getURL()[0] == '/') {
  47. filePath += request.getURL();
  48. } else {
  49. filePath += "/" + request.getURL();
  50. }
  51. if (stat(filePath.c_str(), &buffer) == 0) {
  52. if (S_ISREG(buffer.st_mode)) {
  53. return (ResponseData *) new BinaryResponseData(200, filePath,
  54. mimeTypeOfFile(filePath));
  55. } else {
  56. for (auto file: conf::defaultFiles) {
  57. string newFilePath = filePath;
  58. if (filePath[filePath.length() - 1] == '/') {
  59. newFilePath += file;
  60. } else {
  61. newFilePath += "/" + file;
  62. }
  63. if (stat(newFilePath.c_str(), &buffer) == 0) {
  64. if (S_ISREG(buffer.st_mode)) {
  65. return (ResponseData *) new BinaryResponseData(200, newFilePath, mimeTypeOfFile(newFilePath));
  66. }
  67. }
  68. }
  69. }
  70. }
  71. return (ResponseData *) new FileResponseData(conf::errorPage404);
  72. })
  73. }