web_crawler_storage.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #ifndef WEB_CRAWLER_STORAGE_H
  2. #define WEB_CRAWLER_STORAGE_H
  3. #include <furi.h>
  4. #include <storage/storage.h>
  5. #define SETTINGS_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/settings.bin"
  6. #define RECEIVED_DATA_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/" // add the file name to the end (e.g. "received_data.txt")
  7. // will need to make a duplicate of the file
  8. // one to save data, and the other for users to manipulate
  9. #define MAX_RECEIVED_DATA_SIZE 1024
  10. #define SHOW_MAX_FILE_SIZE 2048
  11. // Define the truncation notice
  12. #define TRUNCATION_NOTICE "\n\n[Data truncated due to size limits]"
  13. // Function to save settings: path, SSID, and password
  14. static void save_settings(
  15. const char *path,
  16. const char *ssid,
  17. const char *password,
  18. const char *file_rename,
  19. const char *file_type,
  20. const char *http_method,
  21. const char *headers,
  22. const char *payload)
  23. {
  24. // Create the directory for saving settings
  25. char directory_path[256];
  26. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler_app");
  27. // Create the directory
  28. Storage *storage = furi_record_open(RECORD_STORAGE);
  29. storage_common_mkdir(storage, directory_path);
  30. // Open the settings file
  31. File *file = storage_file_alloc(storage);
  32. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  33. {
  34. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  35. storage_file_free(file);
  36. furi_record_close(RECORD_STORAGE);
  37. return;
  38. }
  39. if (file_type == NULL || strlen(file_type) == 0)
  40. {
  41. file_type = ".txt";
  42. }
  43. // Save the SSID length and data
  44. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  45. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  46. storage_file_write(file, ssid, ssid_length) != ssid_length)
  47. {
  48. FURI_LOG_E(TAG, "Failed to write SSID");
  49. }
  50. // Save the password length and data
  51. size_t password_length = strlen(password) + 1; // Include null terminator
  52. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  53. storage_file_write(file, password, password_length) != password_length)
  54. {
  55. FURI_LOG_E(TAG, "Failed to write password");
  56. }
  57. // Save the path length and data
  58. size_t path_length = strlen(path) + 1; // Include null terminator
  59. if (storage_file_write(file, &path_length, sizeof(size_t)) != sizeof(size_t) ||
  60. storage_file_write(file, path, path_length) != path_length)
  61. {
  62. FURI_LOG_E(TAG, "Failed to write path");
  63. }
  64. // Save the file rename length and data
  65. size_t file_rename_length = strlen(file_rename) + 1; // Include null terminator
  66. if (storage_file_write(file, &file_rename_length, sizeof(size_t)) != sizeof(size_t) ||
  67. storage_file_write(file, file_rename, file_rename_length) != file_rename_length)
  68. {
  69. FURI_LOG_E(TAG, "Failed to write file rename");
  70. }
  71. // Save the file type length and data
  72. size_t file_type_length = strlen(file_type) + 1; // Include null terminator
  73. if (storage_file_write(file, &file_type_length, sizeof(size_t)) != sizeof(size_t) ||
  74. storage_file_write(file, file_type, file_type_length) != file_type_length)
  75. {
  76. FURI_LOG_E(TAG, "Failed to write file type");
  77. }
  78. // Save the http method length and data
  79. size_t http_method_length = strlen(http_method) + 1; // Include null terminator
  80. if (storage_file_write(file, &http_method_length, sizeof(size_t)) != sizeof(size_t) ||
  81. storage_file_write(file, http_method, http_method_length) != http_method_length)
  82. {
  83. FURI_LOG_E(TAG, "Failed to write http method");
  84. }
  85. // Save the headers length and data
  86. size_t headers_length = strlen(headers) + 1; // Include null terminator
  87. if (storage_file_write(file, &headers_length, sizeof(size_t)) != sizeof(size_t) ||
  88. storage_file_write(file, headers, headers_length) != headers_length)
  89. {
  90. FURI_LOG_E(TAG, "Failed to write headers");
  91. }
  92. // Save the payload length and data
  93. size_t payload_length = strlen(payload) + 1; // Include null terminator
  94. if (storage_file_write(file, &payload_length, sizeof(size_t)) != sizeof(size_t) ||
  95. storage_file_write(file, payload, payload_length) != payload_length)
  96. {
  97. FURI_LOG_E(TAG, "Failed to write payload");
  98. }
  99. storage_file_close(file);
  100. storage_file_free(file);
  101. furi_record_close(RECORD_STORAGE);
  102. }
  103. // Function to load settings (the variables must be opened in the order they were saved)
  104. static bool load_settings(
  105. char *path,
  106. size_t path_size,
  107. char *ssid,
  108. size_t ssid_size,
  109. char *password,
  110. size_t password_size,
  111. char *file_rename,
  112. size_t file_rename_size,
  113. char *file_type,
  114. size_t file_type_size,
  115. char *http_method,
  116. size_t http_method_size,
  117. char *headers,
  118. size_t headers_size,
  119. char *payload,
  120. size_t payload_size,
  121. WebCrawlerApp *app)
  122. {
  123. if (!app)
  124. {
  125. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  126. return false;
  127. }
  128. Storage *storage = furi_record_open(RECORD_STORAGE);
  129. File *file = storage_file_alloc(storage);
  130. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  131. {
  132. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  133. storage_file_free(file);
  134. furi_record_close(RECORD_STORAGE);
  135. return false; // Return false if the file does not exist
  136. }
  137. // Load the SSID
  138. size_t ssid_length;
  139. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  140. storage_file_read(file, ssid, ssid_length) != ssid_length)
  141. {
  142. FURI_LOG_E(TAG, "Failed to read SSID");
  143. storage_file_close(file);
  144. storage_file_free(file);
  145. furi_record_close(RECORD_STORAGE);
  146. return false;
  147. }
  148. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  149. // Load the password
  150. size_t password_length;
  151. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  152. storage_file_read(file, password, password_length) != password_length)
  153. {
  154. FURI_LOG_E(TAG, "Failed to read password");
  155. storage_file_close(file);
  156. storage_file_free(file);
  157. furi_record_close(RECORD_STORAGE);
  158. return false;
  159. }
  160. password[password_length - 1] = '\0'; // Ensure null-termination
  161. // Load the path
  162. size_t path_length;
  163. if (storage_file_read(file, &path_length, sizeof(size_t)) != sizeof(size_t) || path_length > path_size ||
  164. storage_file_read(file, path, path_length) != path_length)
  165. {
  166. FURI_LOG_E(TAG, "Failed to read path");
  167. storage_file_close(file);
  168. storage_file_free(file);
  169. furi_record_close(RECORD_STORAGE);
  170. return false;
  171. }
  172. path[path_length - 1] = '\0'; // Ensure null-termination
  173. // Load the file rename
  174. size_t file_rename_length;
  175. if (storage_file_read(file, &file_rename_length, sizeof(size_t)) != sizeof(size_t) || file_rename_length > file_rename_size ||
  176. storage_file_read(file, file_rename, file_rename_length) != file_rename_length)
  177. {
  178. FURI_LOG_E(TAG, "Failed to read file rename");
  179. storage_file_close(file);
  180. storage_file_free(file);
  181. furi_record_close(RECORD_STORAGE);
  182. return false;
  183. }
  184. file_rename[file_rename_length - 1] = '\0'; // Ensure null-termination
  185. // Load the file type
  186. size_t file_type_length;
  187. if (storage_file_read(file, &file_type_length, sizeof(size_t)) != sizeof(size_t) || file_type_length > file_type_size ||
  188. storage_file_read(file, file_type, file_type_length) != file_type_length)
  189. {
  190. FURI_LOG_E(TAG, "Failed to read file type");
  191. storage_file_close(file);
  192. storage_file_free(file);
  193. furi_record_close(RECORD_STORAGE);
  194. return false;
  195. }
  196. file_type[file_type_length - 1] = '\0'; // Ensure null-termination
  197. // Load the http method
  198. size_t http_method_length;
  199. if (storage_file_read(file, &http_method_length, sizeof(size_t)) != sizeof(size_t) || http_method_length > http_method_size ||
  200. storage_file_read(file, http_method, http_method_length) != http_method_length)
  201. {
  202. FURI_LOG_E(TAG, "Failed to read http method");
  203. storage_file_close(file);
  204. storage_file_free(file);
  205. furi_record_close(RECORD_STORAGE);
  206. return false;
  207. }
  208. // Load the headers
  209. size_t headers_length;
  210. if (storage_file_read(file, &headers_length, sizeof(size_t)) != sizeof(size_t) || headers_length > headers_size ||
  211. storage_file_read(file, headers, headers_length) != headers_length)
  212. {
  213. FURI_LOG_E(TAG, "Failed to read headers");
  214. storage_file_close(file);
  215. storage_file_free(file);
  216. furi_record_close(RECORD_STORAGE);
  217. return false;
  218. }
  219. // Load the payload
  220. size_t payload_length;
  221. if (storage_file_read(file, &payload_length, sizeof(size_t)) != sizeof(size_t) || payload_length > payload_size ||
  222. storage_file_read(file, payload, payload_length) != payload_length)
  223. {
  224. FURI_LOG_E(TAG, "Failed to read payload");
  225. storage_file_close(file);
  226. storage_file_free(file);
  227. furi_record_close(RECORD_STORAGE);
  228. return false;
  229. }
  230. // set the path, ssid, and password
  231. strncpy(app->path, path, path_size);
  232. strncpy(app->ssid, ssid, ssid_size);
  233. strncpy(app->password, password, password_size);
  234. strncpy(app->file_rename, file_rename, file_rename_size);
  235. strncpy(app->file_type, file_type, file_type_size);
  236. strncpy(app->http_method, http_method, http_method_size);
  237. strncpy(app->headers, headers, headers_size);
  238. strncpy(app->payload, payload, payload_size);
  239. storage_file_close(file);
  240. storage_file_free(file);
  241. furi_record_close(RECORD_STORAGE);
  242. return true;
  243. }
  244. static bool delete_received_data(WebCrawlerApp *app)
  245. {
  246. if (app == NULL)
  247. {
  248. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  249. return false;
  250. }
  251. // Open the storage record
  252. Storage *storage = furi_record_open(RECORD_STORAGE);
  253. if (!storage)
  254. {
  255. FURI_LOG_E(TAG, "Failed to open storage record");
  256. return false;
  257. }
  258. if (!storage_simply_remove_recursive(storage, RECEIVED_DATA_PATH "received_data.txt"))
  259. {
  260. FURI_LOG_E(TAG, "Failed to delete main file");
  261. furi_record_close(RECORD_STORAGE);
  262. return false;
  263. }
  264. // Allocate memory for new_path
  265. char *new_path = malloc(256);
  266. if (new_path == NULL)
  267. {
  268. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  269. free(new_path);
  270. return false;
  271. }
  272. if (app->file_type == NULL || strlen(app->file_type) == 0)
  273. {
  274. app->file_type = ".txt";
  275. }
  276. // Format the new_path
  277. int ret_new = snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  278. if (ret_new < 0 || (size_t)ret_new >= 256)
  279. {
  280. FURI_LOG_E(TAG, "Failed to create new_path");
  281. free(new_path);
  282. return false;
  283. }
  284. if (!storage_simply_remove_recursive(storage, new_path))
  285. {
  286. FURI_LOG_E(TAG, "Failed to delete duplicate file");
  287. furi_record_close(RECORD_STORAGE);
  288. return false;
  289. }
  290. furi_record_close(RECORD_STORAGE);
  291. return true;
  292. }
  293. static bool rename_received_data(const char *old_name, const char *new_name, const char *file_type, const char *old_file_type)
  294. {
  295. // Open the storage record
  296. Storage *storage = furi_record_open(RECORD_STORAGE);
  297. if (!storage)
  298. {
  299. FURI_LOG_E(TAG, "Failed to open storage record");
  300. return false;
  301. }
  302. // Allocate memory for old_path and new_path
  303. char *new_path = malloc(256);
  304. char *old_path = malloc(256);
  305. if (new_path == NULL || old_path == NULL)
  306. {
  307. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  308. free(old_path);
  309. free(new_path);
  310. return false;
  311. }
  312. if (file_type == NULL || strlen(file_type) == 0)
  313. {
  314. file_type = ".txt";
  315. }
  316. if (old_file_type == NULL || strlen(old_file_type) == 0)
  317. {
  318. old_file_type = ".txt";
  319. }
  320. // Format the old_path
  321. int ret_old = snprintf(old_path, 256, "%s%s%s", RECEIVED_DATA_PATH, old_name, old_file_type);
  322. if (ret_old < 0 || (size_t)ret_old >= 256)
  323. {
  324. FURI_LOG_E(TAG, "Failed to create old_path");
  325. free(old_path);
  326. free(new_path);
  327. return false;
  328. }
  329. // Format the new_path
  330. int ret_new = snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, new_name, file_type);
  331. if (ret_new < 0 || (size_t)ret_new >= 256)
  332. {
  333. FURI_LOG_E(TAG, "Failed to create new_path");
  334. free(old_path);
  335. free(new_path);
  336. return false;
  337. }
  338. // Check if the file exists
  339. if (!storage_file_exists(storage, old_path))
  340. {
  341. if (!storage_file_exists(storage, RECEIVED_DATA_PATH "received_data.txt"))
  342. {
  343. FURI_LOG_E(TAG, "No saved file exists");
  344. free(old_path);
  345. free(new_path);
  346. furi_record_close(RECORD_STORAGE);
  347. return false;
  348. }
  349. else
  350. {
  351. bool renamed = storage_common_copy(storage, RECEIVED_DATA_PATH "received_data.txt", new_path) == FSE_OK;
  352. furi_record_close(RECORD_STORAGE);
  353. return renamed;
  354. }
  355. }
  356. else
  357. {
  358. bool renamed = storage_common_rename(storage, old_path, new_path) == FSE_OK;
  359. storage_simply_remove_recursive(storage, old_path);
  360. furi_record_close(RECORD_STORAGE);
  361. return renamed;
  362. }
  363. }
  364. static bool text_show_read_lines(File *file, FuriString *str_result)
  365. {
  366. // Reset the FuriString to ensure it's empty before reading
  367. furi_string_reset(str_result);
  368. // Define a buffer to hold the read data
  369. uint8_t buffer[SHOW_MAX_FILE_SIZE];
  370. // Read data into the buffer
  371. size_t read_count = storage_file_read(file, buffer, SHOW_MAX_FILE_SIZE);
  372. if (storage_file_get_error(file) != FSE_OK)
  373. {
  374. FURI_LOG_E(TAG, "Error reading from file.");
  375. return false;
  376. }
  377. // Append each byte to the FuriString
  378. for (size_t i = 0; i < read_count; i++)
  379. {
  380. furi_string_push_back(str_result, buffer[i]);
  381. }
  382. return true;
  383. }
  384. static bool load_received_data(WebCrawlerApp *app)
  385. {
  386. if (app == NULL)
  387. {
  388. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  389. return false;
  390. }
  391. if (!app->widget_file_read)
  392. {
  393. FURI_LOG_E(TAG, "Textbox is NULL");
  394. return false;
  395. }
  396. // Open the storage record
  397. Storage *storage = furi_record_open(RECORD_STORAGE);
  398. if (!storage)
  399. {
  400. FURI_LOG_E(TAG, "Failed to open storage record");
  401. return false;
  402. }
  403. // Allocate a file handle
  404. File *file = storage_file_alloc(storage);
  405. if (!file)
  406. {
  407. FURI_LOG_E(TAG, "Failed to allocate storage file");
  408. furi_record_close(RECORD_STORAGE);
  409. return false;
  410. }
  411. // Open the file for reading
  412. if (!storage_file_open(file, RECEIVED_DATA_PATH "received_data.txt", FSAM_READ, FSOM_OPEN_EXISTING))
  413. {
  414. storage_file_free(file);
  415. furi_record_close(RECORD_STORAGE);
  416. return false; // Return false if the file does not exist
  417. }
  418. // Allocate a FuriString to hold the received data
  419. FuriString *str_result = furi_string_alloc();
  420. if (!str_result)
  421. {
  422. FURI_LOG_E(TAG, "Failed to allocate FuriString");
  423. storage_file_close(file);
  424. storage_file_free(file);
  425. furi_record_close(RECORD_STORAGE);
  426. return false;
  427. }
  428. // Read data into the FuriString
  429. bool read_success = text_show_read_lines(file, str_result);
  430. if (!read_success)
  431. {
  432. FURI_LOG_E(TAG, "Failed to read data from file");
  433. furi_string_free(str_result);
  434. storage_file_close(file);
  435. storage_file_free(file);
  436. furi_record_close(RECORD_STORAGE);
  437. return false;
  438. }
  439. // Check if there is more data beyond the maximum size
  440. char extra_byte;
  441. storage_file_read(file, &extra_byte, 1);
  442. // Retrieve the C-string from FuriString
  443. const char *data_cstr = furi_string_get_cstr(str_result);
  444. // Set the text box with the received data
  445. widget_reset(app->widget_file_read);
  446. if (str_result != NULL)
  447. {
  448. widget_add_text_scroll_element(
  449. app->widget_file_read,
  450. 0,
  451. 0,
  452. 128,
  453. 64, data_cstr);
  454. }
  455. else
  456. {
  457. widget_add_text_scroll_element(
  458. app->widget_file_read,
  459. 0,
  460. 0,
  461. 128,
  462. 64, "File is empty.");
  463. }
  464. // Clean up
  465. furi_string_free(str_result);
  466. storage_file_close(file);
  467. storage_file_free(file);
  468. furi_record_close(RECORD_STORAGE);
  469. return true;
  470. }
  471. #endif // WEB_CRAWLER_STORAGE_H