FileResponseData.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "FileResponseData.h"
  5. #include "strop.h"
  6. using namespace std;
  7. namespace xc {
  8. namespace utils {
  9. Replacement::Replacement(string replace, string with) {
  10. this->replace = replace;
  11. this->with = with;
  12. }
  13. FileResponseData::FileResponseData(int statusCode, string filePath, string contentType):
  14. TextResponseData(statusCode, "", contentType),
  15. filePath() {
  16. this->setFilePath(filePath);
  17. }
  18. FileResponseData::FileResponseData(int statusCode, string filePath, string contentType, vector<Replacement> replacements):
  19. TextResponseData(statusCode, "", contentType) {
  20. this->setFilePath(filePath, replacements);
  21. }
  22. string FileResponseData::getFilePath() {
  23. return this->filePath;
  24. }
  25. void FileResponseData::setFilePath(string filePath) {
  26. this->filePath = filePath;
  27. this->setBody(contentsOfTextFile(filePath));
  28. }
  29. void FileResponseData::setFilePath(string filePath, vector<Replacement> replacements) {
  30. this->filePath = filePath;
  31. string str = contentsOfTextFile(filePath);
  32. for (auto replacement : replacements) {
  33. string fullReplaceText = "{{" + replacement.replace + "}}";
  34. replace_all(str, fullReplaceText, replacement.with);
  35. }
  36. this->setBody(str);
  37. }
  38. IncompleteFileResponseData::IncompleteFileResponseData(FileResponseData holdData): holdData(holdData) { }
  39. FileResponseData IncompleteFileResponseData::applyReplacements(vector<Replacement> replacements) const {
  40. return applyReplacements(this->holdData.getStatusCode(), replacements);
  41. }
  42. FileResponseData IncompleteFileResponseData::applyReplacements(int statusCode, vector<Replacement> replacements) const {
  43. FileResponseData responseData(this->holdData);
  44. responseData.setStatusCode(statusCode);
  45. string oriBody = responseData.getBody();
  46. for (auto replacement : replacements) {
  47. string fullReplaceText = "{{" + replacement.replace + "}}";
  48. replace_all(oriBody, fullReplaceText, replacement.with);
  49. }
  50. responseData.setBody(oriBody);
  51. return responseData;
  52. }
  53. } // xc
  54. } // utils