web_crawler_storage.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 \
  7. STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag \
  8. "/" // add the file name to the end (e.g. "received_data.txt")
  9. // will need to make a duplicate of the file
  10. // one to save data, and the other for users to manipulate
  11. #define MAX_RECEIVED_DATA_SIZE 1024
  12. #define SHOW_MAX_FILE_SIZE 2048
  13. // Define the truncation notice
  14. #define TRUNCATION_NOTICE "\n\n[Data truncated due to size limits]"
  15. // Function to save settings: path, SSID, and password
  16. static void save_settings(
  17. const char* path,
  18. const char* ssid,
  19. const char* password,
  20. const char* file_rename,
  21. const char* file_type,
  22. const char* http_method,
  23. const char* headers,
  24. const char* payload) {
  25. // Create the directory for saving settings
  26. char directory_path[256];
  27. snprintf(
  28. directory_path,
  29. sizeof(directory_path),
  30. STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler_app");
  31. // Create the directory
  32. Storage* storage = furi_record_open(RECORD_STORAGE);
  33. storage_common_mkdir(storage, directory_path);
  34. // Open the settings file
  35. File* file = storage_file_alloc(storage);
  36. if(!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  37. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  38. storage_file_free(file);
  39. furi_record_close(RECORD_STORAGE);
  40. return;
  41. }
  42. if(file_type == NULL || strlen(file_type) == 0) {
  43. file_type = ".txt";
  44. }
  45. // Save the SSID length and data
  46. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  47. if(storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  48. storage_file_write(file, ssid, ssid_length) != ssid_length) {
  49. FURI_LOG_E(TAG, "Failed to write SSID");
  50. }
  51. // Save the password length and data
  52. size_t password_length = strlen(password) + 1; // Include null terminator
  53. if(storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  54. storage_file_write(file, password, password_length) != password_length) {
  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. FURI_LOG_E(TAG, "Failed to write path");
  62. }
  63. // Save the file rename length and data
  64. size_t file_rename_length = strlen(file_rename) + 1; // Include null terminator
  65. if(storage_file_write(file, &file_rename_length, sizeof(size_t)) != sizeof(size_t) ||
  66. storage_file_write(file, file_rename, file_rename_length) != file_rename_length) {
  67. FURI_LOG_E(TAG, "Failed to write file rename");
  68. }
  69. // Save the file type length and data
  70. size_t file_type_length = strlen(file_type) + 1; // Include null terminator
  71. if(storage_file_write(file, &file_type_length, sizeof(size_t)) != sizeof(size_t) ||
  72. storage_file_write(file, file_type, file_type_length) != file_type_length) {
  73. FURI_LOG_E(TAG, "Failed to write file type");
  74. }
  75. // Save the http method length and data
  76. size_t http_method_length = strlen(http_method) + 1; // Include null terminator
  77. if(storage_file_write(file, &http_method_length, sizeof(size_t)) != sizeof(size_t) ||
  78. storage_file_write(file, http_method, http_method_length) != http_method_length) {
  79. FURI_LOG_E(TAG, "Failed to write http method");
  80. }
  81. // Save the headers length and data
  82. size_t headers_length = strlen(headers) + 1; // Include null terminator
  83. if(storage_file_write(file, &headers_length, sizeof(size_t)) != sizeof(size_t) ||
  84. storage_file_write(file, headers, headers_length) != headers_length) {
  85. FURI_LOG_E(TAG, "Failed to write headers");
  86. }
  87. // Save the payload length and data
  88. size_t payload_length = strlen(payload) + 1; // Include null terminator
  89. if(storage_file_write(file, &payload_length, sizeof(size_t)) != sizeof(size_t) ||
  90. storage_file_write(file, payload, payload_length) != payload_length) {
  91. FURI_LOG_E(TAG, "Failed to write payload");
  92. }
  93. storage_file_close(file);
  94. storage_file_free(file);
  95. furi_record_close(RECORD_STORAGE);
  96. }
  97. // Function to load settings (the variables must be opened in the order they were saved)
  98. static bool load_settings(
  99. char* path,
  100. size_t path_size,
  101. char* ssid,
  102. size_t ssid_size,
  103. char* password,
  104. size_t password_size,
  105. char* file_rename,
  106. size_t file_rename_size,
  107. char* file_type,
  108. size_t file_type_size,
  109. char* http_method,
  110. size_t http_method_size,
  111. char* headers,
  112. size_t headers_size,
  113. char* payload,
  114. size_t payload_size,
  115. WebCrawlerApp* app) {
  116. if(!app) {
  117. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  118. return false;
  119. }
  120. Storage* storage = furi_record_open(RECORD_STORAGE);
  121. File* file = storage_file_alloc(storage);
  122. if(!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  123. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  124. storage_file_free(file);
  125. furi_record_close(RECORD_STORAGE);
  126. return false; // Return false if the file does not exist
  127. }
  128. // Load the SSID
  129. size_t ssid_length;
  130. if(storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  131. ssid_length > ssid_size || storage_file_read(file, ssid, ssid_length) != ssid_length) {
  132. FURI_LOG_E(TAG, "Failed to read SSID");
  133. storage_file_close(file);
  134. storage_file_free(file);
  135. furi_record_close(RECORD_STORAGE);
  136. return false;
  137. }
  138. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  139. // Load the password
  140. size_t password_length;
  141. if(storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  142. password_length > password_size ||
  143. storage_file_read(file, password, password_length) != password_length) {
  144. FURI_LOG_E(TAG, "Failed to read password");
  145. storage_file_close(file);
  146. storage_file_free(file);
  147. furi_record_close(RECORD_STORAGE);
  148. return false;
  149. }
  150. password[password_length - 1] = '\0'; // Ensure null-termination
  151. // Load the path
  152. size_t path_length;
  153. if(storage_file_read(file, &path_length, sizeof(size_t)) != sizeof(size_t) ||
  154. path_length > path_size || storage_file_read(file, path, path_length) != path_length) {
  155. FURI_LOG_E(TAG, "Failed to read path");
  156. storage_file_close(file);
  157. storage_file_free(file);
  158. furi_record_close(RECORD_STORAGE);
  159. return false;
  160. }
  161. path[path_length - 1] = '\0'; // Ensure null-termination
  162. // Load the file rename
  163. size_t file_rename_length;
  164. if(storage_file_read(file, &file_rename_length, sizeof(size_t)) != sizeof(size_t) ||
  165. file_rename_length > file_rename_size ||
  166. storage_file_read(file, file_rename, file_rename_length) != file_rename_length) {
  167. FURI_LOG_E(TAG, "Failed to read file rename");
  168. storage_file_close(file);
  169. storage_file_free(file);
  170. furi_record_close(RECORD_STORAGE);
  171. return false;
  172. }
  173. file_rename[file_rename_length - 1] = '\0'; // Ensure null-termination
  174. // Load the file type
  175. size_t file_type_length;
  176. if(storage_file_read(file, &file_type_length, sizeof(size_t)) != sizeof(size_t) ||
  177. file_type_length > file_type_size ||
  178. storage_file_read(file, file_type, file_type_length) != file_type_length) {
  179. FURI_LOG_E(TAG, "Failed to read file type");
  180. storage_file_close(file);
  181. storage_file_free(file);
  182. furi_record_close(RECORD_STORAGE);
  183. return false;
  184. }
  185. file_type[file_type_length - 1] = '\0'; // Ensure null-termination
  186. // Load the http method
  187. size_t http_method_length;
  188. if(storage_file_read(file, &http_method_length, sizeof(size_t)) != sizeof(size_t) ||
  189. http_method_length > http_method_size ||
  190. storage_file_read(file, http_method, http_method_length) != http_method_length) {
  191. FURI_LOG_E(TAG, "Failed to read http method");
  192. storage_file_close(file);
  193. storage_file_free(file);
  194. furi_record_close(RECORD_STORAGE);
  195. return false;
  196. }
  197. // Load the headers
  198. size_t headers_length;
  199. if(storage_file_read(file, &headers_length, sizeof(size_t)) != sizeof(size_t) ||
  200. headers_length > headers_size ||
  201. storage_file_read(file, headers, headers_length) != headers_length) {
  202. FURI_LOG_E(TAG, "Failed to read headers");
  203. storage_file_close(file);
  204. storage_file_free(file);
  205. furi_record_close(RECORD_STORAGE);
  206. return false;
  207. }
  208. // Load the payload
  209. size_t payload_length;
  210. if(storage_file_read(file, &payload_length, sizeof(size_t)) != sizeof(size_t) ||
  211. payload_length > payload_size ||
  212. storage_file_read(file, payload, payload_length) != payload_length) {
  213. FURI_LOG_E(TAG, "Failed to read payload");
  214. storage_file_close(file);
  215. storage_file_free(file);
  216. furi_record_close(RECORD_STORAGE);
  217. return false;
  218. }
  219. // set the path, ssid, and password
  220. strncpy(app->path, path, path_size);
  221. strncpy(app->ssid, ssid, ssid_size);
  222. strncpy(app->password, password, password_size);
  223. strncpy(app->file_rename, file_rename, file_rename_size);
  224. strncpy(app->file_type, file_type, file_type_size);
  225. strncpy(app->http_method, http_method, http_method_size);
  226. strncpy(app->headers, headers, headers_size);
  227. strncpy(app->payload, payload, payload_size);
  228. storage_file_close(file);
  229. storage_file_free(file);
  230. furi_record_close(RECORD_STORAGE);
  231. return true;
  232. }
  233. static bool delete_received_data(WebCrawlerApp* app) {
  234. if(app == NULL) {
  235. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  236. return false;
  237. }
  238. // Open the storage record
  239. Storage* storage = furi_record_open(RECORD_STORAGE);
  240. if(!storage) {
  241. FURI_LOG_E(TAG, "Failed to open storage record");
  242. return false;
  243. }
  244. if(!storage_simply_remove_recursive(storage, RECEIVED_DATA_PATH "received_data.txt")) {
  245. FURI_LOG_E(TAG, "Failed to delete main file");
  246. furi_record_close(RECORD_STORAGE);
  247. return false;
  248. }
  249. // Allocate memory for new_path
  250. char* new_path = malloc(256);
  251. if(new_path == NULL) {
  252. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  253. free(new_path);
  254. return false;
  255. }
  256. if(app->file_type == NULL || strlen(app->file_type) == 0) {
  257. app->file_type = ".txt";
  258. }
  259. // Format the new_path
  260. int ret_new =
  261. snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  262. if(ret_new < 0 || (size_t)ret_new >= 256) {
  263. FURI_LOG_E(TAG, "Failed to create new_path");
  264. free(new_path);
  265. return false;
  266. }
  267. if(!storage_simply_remove_recursive(storage, new_path)) {
  268. FURI_LOG_E(TAG, "Failed to delete duplicate file");
  269. furi_record_close(RECORD_STORAGE);
  270. return false;
  271. }
  272. furi_record_close(RECORD_STORAGE);
  273. return true;
  274. }
  275. static bool rename_received_data(
  276. const char* old_name,
  277. const char* new_name,
  278. const char* file_type,
  279. const char* old_file_type) {
  280. // Open the storage record
  281. Storage* storage = furi_record_open(RECORD_STORAGE);
  282. if(!storage) {
  283. FURI_LOG_E(TAG, "Failed to open storage record");
  284. return false;
  285. }
  286. // Allocate memory for old_path and new_path
  287. char* new_path = malloc(256);
  288. char* old_path = malloc(256);
  289. if(new_path == NULL || old_path == NULL) {
  290. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  291. free(old_path);
  292. free(new_path);
  293. return false;
  294. }
  295. if(file_type == NULL || strlen(file_type) == 0) {
  296. file_type = ".txt";
  297. }
  298. if(old_file_type == NULL || strlen(old_file_type) == 0) {
  299. old_file_type = ".txt";
  300. }
  301. // Format the old_path
  302. int ret_old = snprintf(old_path, 256, "%s%s%s", RECEIVED_DATA_PATH, old_name, old_file_type);
  303. if(ret_old < 0 || (size_t)ret_old >= 256) {
  304. FURI_LOG_E(TAG, "Failed to create old_path");
  305. free(old_path);
  306. free(new_path);
  307. return false;
  308. }
  309. // Format the new_path
  310. int ret_new = snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, new_name, file_type);
  311. if(ret_new < 0 || (size_t)ret_new >= 256) {
  312. FURI_LOG_E(TAG, "Failed to create new_path");
  313. free(old_path);
  314. free(new_path);
  315. return false;
  316. }
  317. // Check if the file exists
  318. if(!storage_file_exists(storage, old_path)) {
  319. if(!storage_file_exists(storage, RECEIVED_DATA_PATH "received_data.txt")) {
  320. FURI_LOG_E(TAG, "No saved file exists");
  321. free(old_path);
  322. free(new_path);
  323. furi_record_close(RECORD_STORAGE);
  324. return false;
  325. } else {
  326. bool renamed =
  327. storage_common_copy(storage, RECEIVED_DATA_PATH "received_data.txt", new_path) ==
  328. FSE_OK;
  329. furi_record_close(RECORD_STORAGE);
  330. return renamed;
  331. }
  332. } else {
  333. bool renamed = storage_common_rename(storage, old_path, new_path) == FSE_OK;
  334. storage_simply_remove_recursive(storage, old_path);
  335. furi_record_close(RECORD_STORAGE);
  336. return renamed;
  337. }
  338. }
  339. #endif // WEB_CRAWLER_STORAGE_H