JsonCommander.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. #include "../include/JsonCommander.h"
  2. #include "../include/Config.h"
  3. #include "../include/Notifications.h"
  4. #include "../include/Queue.h"
  5. #include "../include/UserManager.h"
  6. #include "../include/base64.h"
  7. JsonCommander::JsonCommander(FileManager &fileManager) : fileManager(fileManager) {
  8. commandsMap["status"] = &JsonCommander::executeStatus;
  9. commandsMap["close"] = &JsonCommander::executeClose;
  10. commandsMap["list"] = &JsonCommander::executeList;
  11. commandsMap["listdata"] = &JsonCommander::executeListData;
  12. commandsMap["put"] = &JsonCommander::executePut;
  13. commandsMap["putdata"] = &JsonCommander::executePutdata;
  14. commandsMap["get"] = &JsonCommander::executeGet;
  15. commandsMap["getdata"] = &JsonCommander::executeGetdata;
  16. commandsMap["head"] = &JsonCommander::executeHead;
  17. commandsMap["deleteme"] = &JsonCommander::executeDeleteMe;
  18. commandsMap["deletefile"] = &JsonCommander::executeDeleteFile;
  19. commandsMap["extendedstatus"] = &JsonCommander::executeExtendedStatus;
  20. commandsMap["notifications"] = &JsonCommander::executeNotifications;
  21. commandsMap["extendedlist"] = &JsonCommander::executeExtendedList;
  22. commandsMap["extendedlistdata"] = &JsonCommander::executeExtendedListData;
  23. commandsMap["queue"] = &JsonCommander::executeQueue;
  24. commandsMap["dequeue"] = &JsonCommander::executeDequeue;
  25. }
  26. JsonCommander::~JsonCommander() {}
  27. JsonCommander::Response JsonCommander::execute(const Json::Value &message) {
  28. JsonCommander::Response response;
  29. Response (JsonCommander::*commandExecutor)(const Json::Value &) = commandsMap[message["command"].asString()];
  30. if (commandExecutor != nullptr) {
  31. response = (this->*commandExecutor)(message);
  32. } else {
  33. // command does not exist
  34. response.action = close;
  35. }
  36. return response;
  37. }
  38. JsonCommander::Response JsonCommander::executeStatus(const Json::Value &message) {
  39. JsonCommander::Response response;
  40. response.action = send;
  41. response.json["command"] = "status";
  42. // answer a real status message
  43. std::string status;
  44. if (this->fileManager.isUploading() && this->fileManager.isDownloading()) {
  45. status = "download and upload running";
  46. } else if (this->fileManager.isUploading()) {
  47. status = "upload running";
  48. } else if (this->fileManager.isDownloading()) {
  49. status = "download running";
  50. } else {
  51. status = "ok";
  52. }
  53. response.json["response"] = status;
  54. return response;
  55. }
  56. JsonCommander::Response JsonCommander::executeClose(const Json::Value &message) {
  57. JsonCommander::Response response;
  58. response.action = closeAndSend;
  59. response.json["command"] = "close";
  60. response.json["response"] = "bye";
  61. return response;
  62. }
  63. JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
  64. JsonCommander::Response response;
  65. response.action = send;
  66. response.json["command"] = "list";
  67. int chunks;
  68. if (fileManager.getRemainingListChunks() > 0) {
  69. response.json["accept"] = false;
  70. response.json["chunks"] = -1;
  71. response.json["items"] = -1;
  72. response.json["error"] = "there is already an open list command";
  73. } else if ((chunks = fileManager.openList()) == -1) { // this should not happen on a current system
  74. response.json["accept"] = false;
  75. response.json["chunks"] = -1;
  76. response.json["items"] = -1;
  77. response.json["error"] = "there is a filename which is too long";
  78. } else {
  79. response.json["accept"] = true;
  80. response.json["chunks"] = chunks;
  81. response.json["items"] = fileManager.getListSize();
  82. response.json["error"] = "";
  83. }
  84. return response;
  85. }
  86. JsonCommander::Response JsonCommander::executeListData(const Json::Value &message) {
  87. JsonCommander::Response response;
  88. response.action = send;
  89. response.json["command"] = "listdata";
  90. Json::Value array;
  91. const int remainingListchunks = fileManager.getRemainingListChunks();
  92. if (!message["chunk"].isInt() || !message["cancel"].isBool()) {
  93. response.action = closeAndSend;
  94. response.json["cancel"] = true;
  95. response.json["remaining"] = -1;
  96. response.json["names"] = Json::arrayValue;
  97. response.json["error"] = "incorrect listdata command request";
  98. } else if (remainingListchunks == 0) {
  99. response.json["cancel"] = true;
  100. response.json["remaining"] = -1;
  101. response.json["names"] = Json::arrayValue;
  102. response.json["error"] = "there are no chunks to send";
  103. } else if (message["cancel"].asBool()) {
  104. response.json["cancel"] = true;
  105. response.json["remaining"] = message["chunk"].asInt(); // so it can be associated to the request
  106. response.json["names"] = Json::arrayValue;
  107. response.json["error"] = "";
  108. fileManager.cancelList();
  109. } else if (remainingListchunks - 1 != message["chunk"].asInt()) {
  110. response.action = closeAndSend;
  111. response.json["cancel"] = true;
  112. response.json["remaining"] = -1;
  113. response.json["names"] = Json::arrayValue;
  114. response.json["error"] = "wrong chunk number";
  115. } else {
  116. std::vector<std::string> v = fileManager.getNextChunkFromList();
  117. for (int i = 0; i < v.size(); i++)
  118. array.append(v.at(i));
  119. response.json["remaining"] = message["chunk"].asInt();
  120. response.json["cancel"] = false;
  121. response.json["names"] = array;
  122. response.json["error"] = "";
  123. }
  124. return response;
  125. }
  126. JsonCommander::Response JsonCommander::executePut(const Json::Value &message) {
  127. JsonCommander::Response response;
  128. response.json["command"] = "put";
  129. response.json["file"] = message["file"].asString();
  130. if (!message["file"].isString() || !message["size"].isInt() || !message["chunks"].isInt()) {
  131. // if request is incomplete close connection
  132. response.action = closeAndSend;
  133. response.json["accept"] = false;
  134. response.json["error"] = "incorrect put command request";
  135. } else if (fileManager.isUploading()) {
  136. // if an upload is alread running deny request
  137. response.action = send;
  138. response.json["accept"] = false;
  139. response.json["error"] = "upload already running";
  140. } else if (message["chunks"].asInt() <= 0) {
  141. response.action = send;
  142. response.json["accept"] = false;
  143. response.json["error"] = "there must be at least one chunk";
  144. } else if (fileManager.checkFilename(message["file"].asString())) {
  145. // accept request
  146. response.action = send;
  147. bool opened = fileManager.openPutFile(message["file"].asString());
  148. if (opened) {
  149. response.json["accept"] = true;
  150. response.json["error"] = "";
  151. this->putFileReceived = message["chunks"].asInt();
  152. this->putSize = message["chunks"].asInt();
  153. } else {
  154. response.json["accept"] = false;
  155. response.json["error"] = "file already exists";
  156. }
  157. } else {
  158. // deny request if file name is not valid
  159. response.action = send;
  160. response.json["accept"] = false;
  161. response.json["error"] = "invalid file name";
  162. }
  163. return response;
  164. }
  165. JsonCommander::Response JsonCommander::executePutdata(const Json::Value &message) {
  166. JsonCommander::Response response;
  167. response.json["command"] = "putdata";
  168. response.json["file"] = message["file"].asString();
  169. response.json["received"] = message["remaining"].asInt();
  170. if (!message["file"].isString() || !message["data"].isString() || !message["remaining"].isInt() || !message["cancel"].isBool()) {
  171. // if request is incomplete close connection
  172. response.action = closeAndSend;
  173. response.json["cancel"] = true;
  174. response.json["error"] = "incorrect putdata command request";
  175. this->fileManager.cancelPut();
  176. } else if (!fileManager.isUploading()) {
  177. // no upload running -> command
  178. response.action = send;
  179. response.json["cancel"] = true;
  180. response.json["error"] = "no upload running";
  181. } else if (message["cancel"].asBool()) {
  182. response.action = send;
  183. response.json["cancel"] = true;
  184. response.json["error"] = "";
  185. this->fileManager.cancelPut();
  186. } else if (message["file"].asString().compare(fileManager.getPutBaseFileName()) == 0) {
  187. if (--this->putFileReceived == message["remaining"].asInt()) {
  188. // accept request
  189. response.action = send;
  190. response.json["cancel"] = false;
  191. response.json["error"] = "";
  192. const std::vector<char> data = base64::decode<std::vector<char>>(message["data"].asString());
  193. fileManager.writePut(data);
  194. this->putFileReceived = message["remaining"].asInt();
  195. if (this->putFileReceived == 0) {
  196. // close file after last chunk was received
  197. this->fileManager.closePutFile();
  198. }
  199. } else {
  200. // wrong remaining number
  201. response.action = send;
  202. response.json["cancel"] = true;
  203. response.json["error"] = "wrong remaining number";
  204. this->fileManager.cancelPut();
  205. }
  206. } else {
  207. // wrong file name
  208. response.action = send;
  209. response.json["cancel"] = true;
  210. response.json["error"] = "another file was already being uploaded";
  211. this->fileManager.cancelPut();
  212. }
  213. return response;
  214. }
  215. JsonCommander::Response JsonCommander::executeGet(const Json::Value &message) {
  216. JsonCommander::Response response;
  217. response.json["command"] = "get";
  218. response.json["file"] = message["file"].asString();
  219. if (!message["file"].isString()) {
  220. // if request is incomplete close connection
  221. response.action = closeAndSend;
  222. response.json["accept"] = false;
  223. response.json["chunks"] = -1;
  224. response.json["error"] = "incorrect get command request";
  225. } else if (fileManager.isDownloading()) {
  226. // if an upload is alread running deny request
  227. response.action = send;
  228. response.json["accept"] = false;
  229. response.json["chunks"] = -1;
  230. response.json["error"] = "download already running";
  231. } else if (fileManager.checkFilename(message["file"].asString())) {
  232. // accept request
  233. response.action = send;
  234. std::pair<bool, int> opened = fileManager.openGetFile(message["file"].asString());
  235. if (opened.first) {
  236. this->getFileRemaining = opened.second;
  237. this->getSize = opened.second;
  238. response.json["accept"] = true;
  239. response.json["chunks"] = this->getFileRemaining;
  240. response.json["error"] = "";
  241. } else {
  242. response.json["accept"] = false;
  243. response.json["chunks"] = -1;
  244. response.json["error"] = "file does not exist";
  245. }
  246. } else {
  247. // deny request if file name is not valid
  248. response.action = send;
  249. response.json["accept"] = false;
  250. response.json["chunks"] = -1;
  251. response.json["error"] = "invalid file name";
  252. }
  253. return response;
  254. }
  255. JsonCommander::Response JsonCommander::executeGetdata(const Json::Value &message) {
  256. JsonCommander::Response response;
  257. response.json["command"] = "getdata";
  258. response.json["file"] = message["file"].asString();
  259. response.json["remaining"] = message["chunk"].asInt();
  260. if (!message["file"].isString() || !message["chunk"].isInt() || !message["cancel"].isBool()) {
  261. // if request is incomplete close connection
  262. response.action = closeAndSend;
  263. response.json["cancel"] = true;
  264. response.json["data"] = "";
  265. response.json["error"] = "incorrect putdata command request";
  266. this->fileManager.closeGetFile();
  267. } else if (!fileManager.isDownloading()) {
  268. // no upload running -> command
  269. response.action = send;
  270. response.json["cancel"] = true;
  271. response.json["data"] = "";
  272. response.json["error"] = "no download running";
  273. } else if (message["cancel"].asBool()) {
  274. response.action = send;
  275. response.json["cancel"] = true;
  276. response.json["data"] = "";
  277. response.json["error"] = "";
  278. this->fileManager.closeGetFile();
  279. } else if (message["file"].asString().compare(fileManager.getGetBaseFileName()) == 0) {
  280. if (--this->getFileRemaining == message["chunk"].asInt()) {
  281. // accept request
  282. response.action = send;
  283. response.json["cancel"] = false;
  284. response.json["error"] = "";
  285. const std::vector<char> data = fileManager.readGet();
  286. response.json["data"] = base64::encode<std::vector<char>>(data);
  287. fileManager.writePut(data);
  288. if (this->getFileRemaining == 0) {
  289. // close file after last chunk was sent
  290. this->fileManager.closeGetFile();
  291. }
  292. } else {
  293. // wrong chunk number
  294. response.action = send;
  295. response.json["cancel"] = true;
  296. response.json["data"] = "";
  297. response.json["error"] = "wrong chunk number";
  298. this->fileManager.closeGetFile();
  299. }
  300. } else {
  301. // wrong file name
  302. response.action = send;
  303. response.json["cancel"] = true;
  304. response.json["data"] = "";
  305. response.json["error"] = "another file was already being downloaded";
  306. this->fileManager.closeGetFile();
  307. }
  308. return response;
  309. }
  310. JsonCommander::Response JsonCommander::executeHead(const Json::Value &message) {
  311. JsonCommander::Response response;
  312. response.action = send;
  313. response.json["command"] = "head";
  314. if (!message["file"].isString()) {
  315. response.json["accept"] = false;
  316. response.json["file"] = "";
  317. response.json["data"] = "";
  318. response.json["error"] = "incorrect head command request";
  319. } else {
  320. std::pair<std::vector<char>, FileManager::Error> res = fileManager.getBytesFromFile(message["file"].asString(), 32);
  321. if (res.second == FileManager::Error::file_too_small)
  322. res = fileManager.getBytesFromFile(message["file"].asString(), 4);
  323. switch (res.second) {
  324. case FileManager::Error::no_error:
  325. response.json["accept"] = true;
  326. response.json["file"] = message["file"].asString();
  327. response.json["data"] = base64::encode<std::vector<char>>(res.first);
  328. response.json["error"] = "";
  329. break;
  330. case FileManager::Error::no_such_file:
  331. response.json["accept"] = false;
  332. response.json["file"] = message["file"].asString();
  333. response.json["data"] = "";
  334. response.json["error"] = "no such file";
  335. break;
  336. case FileManager::Error::file_too_small:
  337. response.json["accept"] = false;
  338. response.json["file"] = message["file"].asString();
  339. response.json["data"] = "";
  340. response.json["error"] = "file is smaller than specified size";
  341. break;
  342. default:
  343. response.action = closeAndSend;
  344. response.json["accept"] = false;
  345. response.json["file"] = message["file"];
  346. response.json["data"] = "";
  347. response.json["error"] = "internal error. Please fix this!!!";
  348. }
  349. }
  350. return response;
  351. }
  352. JsonCommander::Response JsonCommander::executeDeleteMe(const Json::Value &message) {
  353. JsonCommander::Response response;
  354. response.json["command"] = "deleteme";
  355. if (!message["pass"].isString()) {
  356. response.action = closeAndSend;
  357. response.json["accept"] = false;
  358. response.json["error"] = "incorrect deleteme command request";
  359. } else if (UserManager::deleteUser(currentUser, message["pass"].asString())) {
  360. // success
  361. response.action = closeAndSend;
  362. response.json["accept"] = true;
  363. response.json["error"] = "";
  364. } else {
  365. response.action = send;
  366. response.json["accept"] = false;
  367. response.json["error"] = "wrong password";
  368. }
  369. return response;
  370. }
  371. JsonCommander::Response JsonCommander::testVersion(const Json::Value &message) {
  372. JsonCommander::Response response;
  373. response.json["major"] = this->protocolMajorVersion;
  374. response.json["minor"] = this->protocolMinorVersion;
  375. if (!message["major"].isInt() || !message["minor"].isInt()) {
  376. response.action = closeAndSend;
  377. response.json["accept"] = false;
  378. } else if (message["major"].asInt() == this->protocolMajorVersion && message["minor"].asInt() <= this->protocolMinorVersion) {
  379. response.action = send;
  380. response.json["accept"] = true;
  381. } else {
  382. response.action = closeAndSend;
  383. response.json["accept"] = false;
  384. }
  385. return response;
  386. }
  387. JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
  388. JsonCommander::Response response;
  389. if (!message["login"].isBool() || !message["user"].isString() || !message["pass"].isString() || !message["cancel"].isBool()) {
  390. // invalid login request
  391. response.action = closeAndSend;
  392. response.json["accept"] = false;
  393. response.json["error"] = "invalid login request";
  394. } else if (message["cancel"].asBool()) {
  395. response.action = close;
  396. } else if (message["login"].asBool() && UserManager::isAllowed(message["user"].asString(), message["pass"].asString())) {
  397. // credential check
  398. response.action = send;
  399. response.json["accept"] = true;
  400. response.json["error"] = "";
  401. currentUser = message["user"].asString();
  402. } else if (!message["login"].asBool()) {
  403. // add user. Check if already exists before
  404. if (!UserManager::addUser(message["user"].asString(), message["pass"].asString())) {
  405. response.action = closeAndSend;
  406. response.json["accept"] = false;
  407. response.json["error"] = "user does already exist";
  408. } else {
  409. response.action = send;
  410. response.json["accept"] = true;
  411. response.json["error"] = "";
  412. currentUser = message["user"].asString();
  413. }
  414. } else {
  415. // reject user
  416. response.action = closeAndSend;
  417. response.json["accept"] = false;
  418. response.json["error"] = "wrong username or password";
  419. }
  420. return response;
  421. }
  422. JsonCommander::Response JsonCommander::executeDeleteFile(const Json::Value &message) {
  423. JsonCommander::Response response;
  424. response.json["command"] = "deletefile";
  425. if (!message["file"].isString()) {
  426. response.action = closeAndSend;
  427. response.json["accept"] = false;
  428. response.json["error"] = "incorrect deletefile command request";
  429. response.json["file"] = "";
  430. } else {
  431. response.action = send;
  432. FileManager::Error err = fileManager.deleteFile(message["file"].asString());
  433. switch (err) {
  434. case FileManager::Error::no_error:
  435. response.json["accept"] = true;
  436. response.json["error"] = "";
  437. response.json["file"] = message["file"];
  438. break;
  439. case FileManager::Error::not_allowed:
  440. response.json["accept"] = false;
  441. response.json["error"] = "deleting files is disabled";
  442. response.json["file"] = message["file"];
  443. break;
  444. case FileManager::Error::no_such_file:
  445. response.json["accept"] = false;
  446. response.json["error"] = "no such file";
  447. response.json["file"] = message["file"];
  448. break;
  449. default:
  450. response.action = closeAndSend;
  451. response.json["accept"] = false;
  452. response.json["error"] = "internal error. Please fix this!!!";
  453. response.json["file"] = message["file"];
  454. }
  455. }
  456. return response;
  457. }
  458. JsonCommander::Response JsonCommander::executeExtendedStatus(const Json::Value &message) {
  459. JsonCommander::Response response;
  460. response.json["command"] = "extendedstatus";
  461. // get status from client server transfers
  462. int index = 0;
  463. if (fileManager.isUploading()) {
  464. response.json["transfersclientserver"][index]["upload"] = true;
  465. response.json["transfersclientserver"][index]["file"] = fileManager.getPutBaseFileName();
  466. int progress = 0;
  467. if (this->putSize != 0) {
  468. double d = (double)(this->putSize - this->putFileReceived) / (double)this->putSize;
  469. progress = (int)(d * 100);
  470. }
  471. response.json["transfersclientserver"][index]["progress"] = progress;
  472. index++;
  473. }
  474. if (fileManager.isDownloading()) {
  475. response.json["transfersclientserver"][index]["upload"] = false;
  476. response.json["transfersclientserver"][index]["file"] = fileManager.getGetBaseFileName();
  477. int progress = 0;
  478. if (this->getSize != 0) {
  479. double d = 1 - ((double)this->getFileRemaining / (double)this->getSize);
  480. progress = (int)(d * 100);
  481. }
  482. response.json["transfersclientserver"][index]["progress"] = progress;
  483. }
  484. // get status from covert channel
  485. index = 0;
  486. if (Queue::channel != nullptr && Queue::channel->isTransferRunning()) {
  487. response.json["transfersserverserver"][index]["type"] = Config::getValue("passiveMode").compare("true") == 0 ? "download" : "upload";
  488. response.json["transfersserverserver"][index]["file"] = Queue::curTransfer();
  489. auto rawprogress = Queue::channel->getProgress();
  490. double d;
  491. if (rawprogress.second == 0)
  492. d = 0.0;
  493. else
  494. d = ((double)rawprogress.first / (double)rawprogress.second);
  495. int progress = (int)(d * 100);
  496. response.json["transfersserverserver"][index]["progress"] = progress;
  497. std::time_t startTime = Queue::channel->getTransferStart();
  498. std::time_t curTime = std::time(nullptr);
  499. std::time_t diffTime = curTime - startTime;
  500. double speed = (double)rawprogress.first / (double)diffTime;
  501. response.json["transfersserverserver"][index]["speed"] = speed;
  502. response.json["transfersserverserver"][index]["method"] = Config::getValue("covertChannelMode");
  503. index++;
  504. }
  505. for (int i = 0; i < Queue::queue.size(); i++) {
  506. response.json["transfersserverserver"][index]["type"] = "queued";
  507. response.json["transfersserverserver"][index]["file"] = Queue::queue[i];
  508. response.json["transfersserverserver"][index]["progress"] = 0;
  509. response.json["transfersserverserver"][index]["speed"] = 0;
  510. response.json["transfersserverserver"][index]["method"] = Config::getValue("covertChannelMode");
  511. index++;
  512. }
  513. response.json["accept"] = true;
  514. response.json["error"] = "";
  515. response.action = send;
  516. return response;
  517. }
  518. JsonCommander::Response JsonCommander::executeNotifications(const Json::Value &message) {
  519. JsonCommander::Response response;
  520. response.json["command"] = "notifications";
  521. Json::Value array;
  522. std::vector<std::string> v = Notifications::getMessages(currentUser);
  523. for (int i = 0; i < v.size(); i++)
  524. array.append(v.at(i));
  525. response.action = send;
  526. response.json["messages"] = array;
  527. response.json["accept"] = true;
  528. response.json["error"] = "";
  529. return response;
  530. }
  531. JsonCommander::Response JsonCommander::executeExtendedList(const Json::Value &message) {
  532. JsonCommander::Response response;
  533. response.action = send;
  534. response.json["command"] = "extendedlist";
  535. int chunks;
  536. if (fileManager.getRemainingExtendedListChunks() > 0) {
  537. response.json["accept"] = false;
  538. response.json["chunks"] = -1;
  539. response.json["items"] = -1;
  540. response.json["error"] = "there is already an open extendedlist command";
  541. } else if ((chunks = fileManager.openExtendedList()) == -1) {
  542. response.json["accept"] = false;
  543. response.json["chunks"] = -1;
  544. response.json["items"] = -1;
  545. response.json["error"] = "there is a filename which is too long";
  546. } else {
  547. response.json["accept"] = true;
  548. response.json["chunks"] = chunks;
  549. response.json["items"] = fileManager.getExtendedListSize();
  550. response.json["error"] = "";
  551. }
  552. return response;
  553. }
  554. JsonCommander::Response JsonCommander::executeQueue(const Json::Value &message) {
  555. JsonCommander::Response response;
  556. response.json["command"] = "queue";
  557. if (!message["file"].isString()) {
  558. response.action = closeAndSend;
  559. response.json["file"] = "";
  560. response.json["accept"] = false;
  561. response.json["error"] = "invalid request";
  562. } else {
  563. bool res = Queue::push(message["file"].asString());
  564. if (res) {
  565. response.action = send;
  566. response.json["file"] = message["file"].asString();
  567. response.json["accept"] = true;
  568. response.json["error"] = "";
  569. } else {
  570. response.action = send;
  571. response.json["file"] = message["file"].asString();
  572. response.json["accept"] = false;
  573. response.json["error"] = "file could not be queued";
  574. }
  575. }
  576. return response;
  577. }
  578. JsonCommander::Response JsonCommander::executeExtendedListData(const Json::Value &message) {
  579. JsonCommander::Response response;
  580. response.action = send;
  581. response.json["command"] = "extendedlistdata";
  582. Json::Value array;
  583. const int remainingListchunks = fileManager.getRemainingExtendedListChunks();
  584. if (!message["chunk"].isInt() || !message["cancel"].isBool()) {
  585. response.action = closeAndSend;
  586. response.json["cancel"] = true;
  587. response.json["remaining"] = -1;
  588. response.json["files"] = Json::arrayValue;
  589. response.json["error"] = "incorrect listdata command request";
  590. } else if (remainingListchunks == 0) {
  591. response.json["cancel"] = true;
  592. response.json["remaining"] = -1;
  593. response.json["files"] = Json::arrayValue;
  594. response.json["error"] = "there are no chunks to send";
  595. } else if (message["cancel"].asBool()) {
  596. response.json["cancel"] = true;
  597. response.json["remaining"] = message["chunk"].asInt();
  598. response.json["files"] = Json::arrayValue;
  599. response.json["error"] = "";
  600. fileManager.cancelExtendedList();
  601. } else if (remainingListchunks - 1 != message["chunk"].asInt()) {
  602. response.action = closeAndSend;
  603. response.json["cancel"] = true;
  604. response.json["remaining"] = -1;
  605. response.json["files"] = Json::arrayValue;
  606. response.json["error"] = "wrong chunk number";
  607. } else {
  608. auto v = fileManager.getNextChunkFromExtendedList();
  609. for (int i = 0; i < v.size(); i++) {
  610. Json::Value obj;
  611. obj["name"] = std::get<0>(v.at(i));
  612. obj["head"] = std::get<1>(v.at(i)).compare("") == 0 ? "" : base64::encode(std::get<1>(v.at(i)));
  613. obj["size"] = std::get<2>(v.at(i));
  614. array.append(obj);
  615. }
  616. response.json["remaining"] = message["chunk"].asInt();
  617. response.json["cancel"] = false;
  618. response.json["files"] = array;
  619. response.json["error"] = "";
  620. }
  621. return response;
  622. }
  623. JsonCommander::Response JsonCommander::executeDequeue(const Json::Value &message) {
  624. JsonCommander::Response response;
  625. response.json["command"] = "dequeue";
  626. if (!message["file"].isString()) {
  627. response.action = closeAndSend;
  628. response.json["file"] = "";
  629. response.json["accept"] = false;
  630. response.json["error"] = "invalid request";
  631. } else {
  632. bool res = Queue::remove(message["file"].asString());
  633. if (res) {
  634. response.action = send;
  635. response.json["file"] = message["file"].asString();
  636. response.json["accept"] = true;
  637. response.json["error"] = "";
  638. } else {
  639. response.action = send;
  640. response.json["file"] = message["file"].asString();
  641. response.json["accept"] = false;
  642. response.json["error"] = "cannot remove this file from queue";
  643. }
  644. }
  645. return response;
  646. }