util.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #ifndef UTIL_HPP
  2. #define UTIL_HPP
  3. #pragma GCC system_header
  4. #include <string>
  5. #include <charconv>
  6. #include <utility>
  7. #include <vector>
  8. #include <QDebug>
  9. #include <QVector>
  10. #include <QString>
  11. #include "rapidjson/writer.h"
  12. #include "rapidjson/prettywriter.h"
  13. #include "rapidjson/pointer.h"
  14. #include "rapidjson/stringbuffer.h"
  15. #include "rapidjson/document.h"
  16. #include "json.hpp"
  17. namespace Kontainer {
  18. /** Reverse Iterator */
  19. template <class T>
  20. class ReverseIterator {
  21. T& _obj;
  22. public:
  23. ReverseIterator(T &obj) : _obj(obj) {}
  24. auto begin() {return _obj.rbegin();}
  25. auto end() {return _obj.rend();}
  26. };
  27. }
  28. enum FileType {
  29. VIDEO = 1,
  30. IMAGE = 2
  31. };
  32. struct KFileData {
  33. FileType type;
  34. QString name;
  35. QByteArray bytes;
  36. };
  37. namespace {
  38. using namespace rapidjson;
  39. using json = nlohmann::json;
  40. typedef std::string KOperation;
  41. typedef std::vector<std::pair<std::string, std::string>> TupVec;
  42. typedef std::vector<std::map<int, std::string>> MapVec;
  43. typedef std::vector<std::string> StdStringVec;
  44. typedef std::map<int, std::string> CommandMap;
  45. struct KSession {
  46. int id;
  47. int fd;
  48. int status;
  49. };
  50. static QString escapeText(QString s) {
  51. qDebug() << "Escaping text";
  52. if (s.contains("'")) {
  53. qDebug() << "Replacing single quote";
  54. s.replace("'", "\'");
  55. }
  56. if (s.contains("\t")) {
  57. s.replace("\t", "\\t");
  58. }
  59. if (s.contains('"')) {
  60. s.replace('"', "\\\"");
  61. }
  62. return s;
  63. }
  64. static QString escapeTextToRaw(QString s) {
  65. return escapeText(s).toUtf8().constData();
  66. }
  67. std::string getJsonString(std::string s) {
  68. Document d;
  69. d.Parse(s.c_str());
  70. StringBuffer buffer;
  71. PrettyWriter<StringBuffer> writer(buffer);
  72. d.Accept(writer);
  73. return buffer.GetString();
  74. }
  75. std::string createMessage(const char* data, std::string args = "") {
  76. StringBuffer s;
  77. Writer<StringBuffer> w(s);
  78. w.StartObject();
  79. w.Key("type");
  80. w.String("custom");
  81. w.Key("message");
  82. w.String(data);
  83. w.Key("args");
  84. w.String(args.c_str());
  85. w.EndObject();
  86. return s.GetString();
  87. }
  88. bool isOperation(const char* data) {
  89. Document d;
  90. d.Parse(data);
  91. return strcmp(d["type"].GetString(), "operation") == 0;
  92. }
  93. bool isUploadCompleteEvent(const char* event) {
  94. return strcmp(event, "File Transfer Complete") == 0;
  95. }
  96. bool isEvent(const char* data) {
  97. Document d;
  98. d.Parse(data);
  99. return strcmp(d["type"].GetString(), "event") == 0;
  100. }
  101. bool isPong(const char* data) {
  102. return strcmp(data, "PONG") == 0;
  103. }
  104. // TODO: This should be "message", no?
  105. bool isMessage(const char* data) {
  106. Document d;
  107. d.Parse(data);
  108. if (d.HasMember("message")) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. std::string createOperation(const char* op, std::vector<std::string> args) {
  115. StringBuffer s;
  116. Writer<StringBuffer, Document::EncodingType, ASCII<>> w(s);
  117. w.StartObject();
  118. w.Key("type");
  119. w.String("operation");
  120. w.Key("command");
  121. w.String(op);
  122. w.Key("args");
  123. w.StartArray();
  124. if (!args.empty()) {
  125. for (const auto& arg : args) {
  126. w.String(arg.c_str());
  127. }
  128. }
  129. w.EndArray();
  130. w.EndObject();
  131. return s.GetString();
  132. }
  133. std::string getOperation(const char* data) {
  134. Document d;
  135. d.Parse(data);
  136. if (d.HasMember("command")) {
  137. return d["command"].GetString();
  138. }
  139. return "";
  140. }
  141. QString getEvent(const char* data) {
  142. Document d;
  143. d.Parse(data);
  144. if (d.HasMember("event")) {
  145. return d["event"].GetString();
  146. }
  147. return "";
  148. }
  149. QString getMessage(const char* data) {
  150. Document d;
  151. d.Parse(data);
  152. if (d.HasMember("message")) {
  153. return d["message"].GetString();
  154. }
  155. return "";
  156. }
  157. QVector<QString> getShortArgs(const char* data) {
  158. Document d;
  159. d.Parse(data);
  160. QVector<QString> args{};
  161. if (d.HasMember("args")) {
  162. if (d["args"].IsArray()) {
  163. for (const auto& m : d["args"].GetArray()) {
  164. if (m.GetStringLength() < 100) {
  165. args.push_back(m.GetString());
  166. }
  167. }
  168. } else {
  169. for (const auto& m : d["args"].GetObject()) {
  170. QString arg = m.name.GetString();
  171. arg += ": ";
  172. arg += m.value.GetString();
  173. args.push_back(arg);
  174. }
  175. }
  176. }
  177. return args;
  178. }
  179. QVector<QString> getArgs(const char* data) {
  180. Document d;
  181. d.Parse(data);
  182. QVector<QString> args{};
  183. if (d.HasMember("args")) {
  184. for (const auto& m : d["args"].GetArray()) {
  185. args.push_back(m.GetString());
  186. }
  187. }
  188. return args;
  189. }
  190. CommandMap getArgMap(const char* data) {
  191. Document d;
  192. d.Parse(data);
  193. CommandMap cm{};
  194. if (d.HasMember("args")) {
  195. for (const auto& m : d["args"].GetObject()) {
  196. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  197. }
  198. }
  199. return cm;
  200. }
  201. std::string createMessage(const char* data,
  202. std::map<int, std::string> map = {}) {
  203. StringBuffer s;
  204. Writer<StringBuffer> w(s);
  205. w.StartObject();
  206. w.Key("type");
  207. w.String("custom");
  208. w.Key("message");
  209. w.String(data);
  210. w.Key("args");
  211. w.StartObject();
  212. if (!map.empty()) {
  213. for (const auto& [k, v] : map) {
  214. w.Key(std::to_string(k).c_str());
  215. w.String(v.c_str());
  216. }
  217. }
  218. w.EndObject();
  219. w.EndObject();
  220. return s.GetString();
  221. }
  222. std::string createMessage(const char* data, std::map<int, std::vector<std::string>> map = {}) {
  223. StringBuffer s;
  224. Writer<StringBuffer> w(s);
  225. w.StartObject();
  226. w.Key("type");
  227. w.String("custom");
  228. w.Key("message");
  229. w.String(data);
  230. w.Key("args");
  231. w.StartObject();
  232. if (!map.empty()) {
  233. for (const auto& [k, v] : map) {
  234. w.Key(std::to_string(k).c_str());
  235. if (!v.empty()) {
  236. w.StartArray();
  237. for (const auto& arg : v) {
  238. w.String(arg.c_str());
  239. }
  240. w.EndArray();
  241. }
  242. }
  243. }
  244. w.EndObject();
  245. w.EndObject();
  246. return s.GetString();
  247. }
  248. std::string rapidCreateMessage(const char* data,
  249. std::map<int, std::string> map = {}) {
  250. StringBuffer s;
  251. Writer<StringBuffer> w(s);
  252. w.StartObject();
  253. w.Key("type");
  254. w.String("custom");
  255. w.Key("message");
  256. w.String(data);
  257. w.Key("args");
  258. w.StartObject();
  259. if (!map.empty()) {
  260. for (const auto& [k, v] : map) {
  261. w.Key(std::to_string(k).c_str());
  262. w.String(v.c_str());
  263. }
  264. }
  265. w.EndObject();
  266. w.EndObject();
  267. return s.GetString();
  268. }
  269. bool isStartOperation(const char* data) {
  270. Document d;
  271. d.Parse(data);
  272. return strcmp(d["command"].GetString(), "start") == 0;
  273. }
  274. bool isStopOperation(const char* data) {
  275. Document d;
  276. d.Parse(data);
  277. return strcmp(d["command"].GetString(), "stop") == 0;
  278. }
  279. bool isNewSession(const char* data) {
  280. Document d;
  281. d.Parse(data);
  282. if (d.IsObject() && d.HasMember("message")) {
  283. return strcmp(d["message"].GetString(), "New Session") == 0;
  284. }
  285. return false;
  286. }
  287. bool serverWaitingForFile(const char* data) {
  288. Document d;
  289. d.Parse(data);
  290. if (d.IsObject() && d.HasMember("message")) {
  291. return strcmp(d["message"].GetString(), "File Ready") == 0;
  292. }
  293. return false;
  294. }
  295. std::string stringTupleVecToJson(
  296. std::vector<std::pair<std::string, std::string>> v) {
  297. json j{};
  298. for (const auto& row : v) {
  299. j[row.first] = row.second;
  300. }
  301. return j;
  302. }
  303. inline size_t findNullIndex(uint8_t* data) {
  304. size_t index = 0;
  305. while (data) {
  306. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  307. break;
  308. }
  309. index++;
  310. data++;
  311. }
  312. return index;
  313. }
  314. namespace FileUtils {
  315. QString generatePreview(QString video_path, QString video_name) {
  316. QString preview_name = video_name.left(video_name.size() - 4) + "-preview.jpg";
  317. // QString command{
  318. // "ffmpeg -ss 0 -i " + video_path + " -vf select=\"eq(pict_type\\,I)\" -vframes 1 ./assets/previews/" + preview_name};
  319. QString command {
  320. "ffmpeg -y -ss 0 -i " + video_path + " -vf \"scale=w=640:h=640:force_original_aspect_ratio=decrease,pad=w=640:h=640:x=(iw-ow)/2:y=(ih-oh/2):color=white\" -vframes 1 ./assets/previews/" + preview_name
  321. };
  322. std::system(command.toUtf8());
  323. return preview_name;
  324. }
  325. }; // namespace FileUtils
  326. }
  327. #endif // UTIL_HPP