FileReader.cpp 973 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "utils-private.h"
  5. #include "../webuiconf.h"
  6. using namespace std;
  7. namespace xc::utils {
  8. string contentsOfTextFile(string filePath) {
  9. ifstream fin(filePath);
  10. if (fin.fail()) {
  11. cerr << "[FileIOError]: " << ::strerror(errno) << endl;
  12. return "404";
  13. }
  14. stringstream buffer;
  15. buffer << fin.rdbuf();
  16. string str(buffer.str());
  17. fin.close();
  18. return str;
  19. }
  20. string mimeTypeOfFile(string filePath) {
  21. std::filesystem::path p(filePath);
  22. string ext = p.extension();
  23. auto cit = conf::fileExtensionToMimeTypes.find(ext);
  24. if (cit != conf::fileExtensionToMimeTypes.end()) {
  25. return cit->second;
  26. }
  27. cit = conf::fileExtensionToMimeTypes.find("default");
  28. if (cit != conf::fileExtensionToMimeTypes.end()) {
  29. return cit->second;
  30. }
  31. return "data";
  32. }
  33. }