flip_store_github.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include <github/flip_store_github.h>
  2. #include <flip_storage/flip_store_storage.h>
  3. // Helper to download a file from Github and save it to the storage
  4. bool flip_store_download_github_file(
  5. FlipperHTTP *fhttp,
  6. const char *filename,
  7. const char *author,
  8. const char *repo,
  9. const char *link)
  10. {
  11. if (!fhttp)
  12. {
  13. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  14. return false;
  15. }
  16. // Create the file directory
  17. char dir[256];
  18. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s/%s/%s", author, repo, filename);
  19. snprintf(fhttp->file_path, sizeof(fhttp->file_path), "%s", dir);
  20. Storage *storage = furi_record_open(RECORD_STORAGE);
  21. storage_common_mkdir(storage, dir);
  22. furi_record_close(RECORD_STORAGE);
  23. fhttp->state = IDLE;
  24. fhttp->save_received_data = false;
  25. fhttp->is_bytes_request = true;
  26. return flipper_http_get_request_bytes(fhttp, link, "{\"Content-Type\":\"application/octet-stream\"}");
  27. }
  28. bool flip_store_get_github_contents(FlipperHTTP *fhttp, const char *author, const char *repo)
  29. {
  30. // Create Initial directory
  31. Storage *storage = furi_record_open(RECORD_STORAGE);
  32. char dir[256];
  33. // create a data directory: /ext/apps_data/flip_store/data
  34. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data");
  35. storage_common_mkdir(storage, dir);
  36. // create a data directory for the author: /ext/apps_data/flip_store/data/author
  37. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s", author);
  38. storage_common_mkdir(storage, dir);
  39. // example path: /ext/apps_data/flip_store/data/author/info.json
  40. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/info.json", author);
  41. // create a data directory for the repo: /ext/apps_data/flip_store/data/author/repo
  42. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/%s", author, repo);
  43. storage_common_mkdir(storage, dir);
  44. // example path: /ext/apps_data/flip_store/author
  45. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s", author);
  46. storage_common_mkdir(storage, dir);
  47. // example path: /ext/apps_data/flip_store/author/repo
  48. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s/%s", author, repo);
  49. storage_common_mkdir(storage, dir);
  50. furi_record_close(RECORD_STORAGE);
  51. // get the contents of the repo
  52. char link[256];
  53. snprintf(link, sizeof(link), "https://api.github.com/repos/%s/%s/contents", author, repo);
  54. fhttp->save_received_data = true;
  55. return flipper_http_get_request_with_headers(fhttp, link, "{\"Content-Type\":\"application/json\"}");
  56. }
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <stdbool.h>
  60. // Assuming necessary headers and definitions for FuriString, FURI_LOG, etc., are included.
  61. bool flip_store_parse_github_contents(char *file_path, const char *author, const char *repo)
  62. {
  63. FURI_LOG_I(TAG, "Parsing Github contents from %s - %s.", author, repo);
  64. if (!file_path || !author || !repo)
  65. {
  66. FURI_LOG_E(TAG, "Invalid arguments.");
  67. return false;
  68. }
  69. // Load JSON data from file
  70. FuriString *git_data = flipper_http_load_from_file(file_path);
  71. if (git_data == NULL)
  72. {
  73. FURI_LOG_E(TAG, "Failed to load received data from file.");
  74. return false;
  75. }
  76. // Allocate a new FuriString to hold the entire JSON structure
  77. FuriString *git_data_str = furi_string_alloc();
  78. if (!git_data_str)
  79. {
  80. FURI_LOG_E(TAG, "Failed to allocate git_data_str.");
  81. furi_string_free(git_data);
  82. return false;
  83. }
  84. // Construct the full JSON string
  85. furi_string_cat_str(git_data_str, "{\"json_data\":");
  86. furi_string_cat(git_data_str, git_data);
  87. furi_string_cat_str(git_data_str, "}");
  88. furi_string_free(git_data); // Free the original git_data as it's now part of git_data_str
  89. // Check available memory
  90. const size_t additional_bytes = strlen("{\"json_data\":") + 1; // +1 for the closing "}"
  91. if (memmgr_get_free_heap() < furi_string_size(git_data_str) + additional_bytes)
  92. {
  93. FURI_LOG_E(TAG, "Not enough memory to allocate git_data_str.");
  94. furi_string_free(git_data_str);
  95. return false;
  96. }
  97. int file_count = 0;
  98. char dir[512]; // Increased size to accommodate longer paths if necessary
  99. FURI_LOG_I(TAG, "Looping through Github files.");
  100. FURI_LOG_I(TAG, "Available memory: %d bytes", memmgr_get_free_heap());
  101. // Get the C-string and its length for processing
  102. char *data = (char *)furi_string_get_cstr(git_data_str);
  103. size_t data_len = furi_string_size(git_data_str);
  104. size_t pos = 0; // Current position in the data string
  105. // Locate the start of the JSON array
  106. char *array_start = strchr(data, '[');
  107. if (!array_start)
  108. {
  109. FURI_LOG_E(TAG, "Invalid JSON format: '[' not found.");
  110. furi_string_free(git_data_str);
  111. return false;
  112. }
  113. pos = array_start - data; // Update position to the start of the array
  114. size_t brace_count = 0;
  115. size_t obj_start = 0;
  116. bool in_string = false; // To handle braces inside strings
  117. while (pos < data_len && file_count < MAX_GITHUB_FILES)
  118. {
  119. char current = data[pos];
  120. // Toggle in_string flag if a quote is found (handling escaped quotes)
  121. if (current == '"' && (pos == 0 || data[pos - 1] != '\\'))
  122. {
  123. in_string = !in_string;
  124. }
  125. if (!in_string)
  126. {
  127. if (current == '{')
  128. {
  129. if (brace_count == 0)
  130. {
  131. obj_start = pos; // Potential start of a JSON object
  132. }
  133. brace_count++;
  134. }
  135. else if (current == '}')
  136. {
  137. brace_count--;
  138. if (brace_count == 0)
  139. {
  140. size_t obj_end = pos;
  141. size_t obj_length = obj_end - obj_start + 1;
  142. // Extract the JSON object substring
  143. char *obj_str = malloc(obj_length + 1);
  144. if (!obj_str)
  145. {
  146. FURI_LOG_E(TAG, "Memory allocation failed for obj_str.");
  147. break;
  148. }
  149. strncpy(obj_str, data + obj_start, obj_length);
  150. obj_str[obj_length] = '\0'; // Null-terminate
  151. FuriString *json_data_array = furi_string_alloc();
  152. furi_string_set(json_data_array, obj_str); // Set the string to the allocated memory
  153. free(obj_str); // Free the temporary C-string
  154. if (!json_data_array)
  155. {
  156. FURI_LOG_E(TAG, "Failed to initialize json_data_array.");
  157. break;
  158. }
  159. FURI_LOG_I(TAG, "Loaded json data array value %d. Available memory: %d bytes", file_count, memmgr_get_free_heap());
  160. // Extract "type" field
  161. FuriString *type = get_json_value_furi("type", json_data_array);
  162. if (!type)
  163. {
  164. FURI_LOG_E(TAG, "Failed to get type.");
  165. furi_string_free(json_data_array);
  166. break;
  167. }
  168. // Skip non-file types (e.g., directories)
  169. if (strcmp(furi_string_get_cstr(type), "file") != 0)
  170. {
  171. furi_string_free(type);
  172. furi_string_free(json_data_array);
  173. pos = obj_end + 1; // Move past this object
  174. continue;
  175. }
  176. furi_string_free(type);
  177. // Extract "download_url" and "name"
  178. FuriString *download_url = get_json_value_furi("download_url", json_data_array);
  179. if (!download_url)
  180. {
  181. FURI_LOG_E(TAG, "Failed to get download_url.");
  182. furi_string_free(json_data_array);
  183. break;
  184. }
  185. FuriString *name = get_json_value_furi("name", json_data_array);
  186. if (!name)
  187. {
  188. FURI_LOG_E(TAG, "Failed to get name.");
  189. furi_string_free(json_data_array);
  190. furi_string_free(download_url);
  191. break;
  192. }
  193. furi_string_free(json_data_array);
  194. FURI_LOG_I(TAG, "Received name and download_url. Available memory: %d bytes", memmgr_get_free_heap());
  195. // Create JSON to save
  196. FuriString *json = furi_string_alloc();
  197. if (!json)
  198. {
  199. FURI_LOG_E(TAG, "Failed to allocate json.");
  200. furi_string_free(download_url);
  201. furi_string_free(name);
  202. break;
  203. }
  204. furi_string_cat_str(json, "{\"name\":\"");
  205. furi_string_cat(json, name);
  206. furi_string_cat_str(json, "\",\"link\":\"");
  207. furi_string_cat(json, download_url);
  208. furi_string_cat_str(json, "\"}");
  209. FURI_LOG_I(TAG, "Created json. Available memory: %d bytes", memmgr_get_free_heap());
  210. // Save the JSON to the data folder: /ext/apps_data/flip_store/data/author/repo/fileX.json
  211. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/%s/file%d.json", author, repo, file_count);
  212. if (!save_char_with_path(dir, furi_string_get_cstr(json)))
  213. {
  214. FURI_LOG_E(TAG, "Failed to save json.");
  215. }
  216. FURI_LOG_I(TAG, "Saved file %s.", furi_string_get_cstr(name));
  217. // Free allocated resources
  218. furi_string_free(name);
  219. furi_string_free(download_url);
  220. furi_string_free(json);
  221. file_count++;
  222. // This can be expensive for large strings; consider memory constraints
  223. size_t remaining_length = data_len - (obj_end + 1);
  224. memmove(data + obj_start, data + obj_end + 1, remaining_length + 1); // +1 to include null terminator
  225. data_len -= (obj_end + 1 - obj_start);
  226. pos = obj_start; // Reset position to the start of the modified string
  227. continue;
  228. }
  229. }
  230. }
  231. pos++;
  232. }
  233. furi_string_free(git_data_str);
  234. // Save file count
  235. char file_count_str[16];
  236. snprintf(file_count_str, sizeof(file_count_str), "%d", file_count);
  237. char file_count_dir[512]; // Increased size for longer paths
  238. snprintf(file_count_dir, sizeof(file_count_dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/file_count.txt", author);
  239. FURI_LOG_I(TAG, "Successfully parsed %d files.", file_count);
  240. return save_char_with_path(file_count_dir, file_count_str);
  241. }
  242. bool flip_store_install_all_github_files(FlipperHTTP *fhttp, const char *author, const char *repo)
  243. {
  244. FURI_LOG_I(TAG, "Installing all Github files.");
  245. if (!fhttp || !author || !repo)
  246. {
  247. FURI_LOG_E(TAG, "Invalid arguments.");
  248. return false;
  249. }
  250. // get the file count
  251. char file_count_dir[256]; // /ext/apps_data/flip_store/data/author/file_count.txt
  252. snprintf(file_count_dir, sizeof(file_count_dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/file_count.txt", author);
  253. FuriString *file_count = flipper_http_load_from_file(file_count_dir);
  254. if (file_count == NULL)
  255. {
  256. FURI_LOG_E(TAG, "Failed to load file count.");
  257. return false;
  258. }
  259. int count = atoi(furi_string_get_cstr(file_count));
  260. furi_string_free(file_count);
  261. bool parse()
  262. {
  263. return true;
  264. }
  265. // install all files
  266. char file_dir[256]; // /ext/apps_data/flip_store/data/author/repo/file.json
  267. FURI_LOG_I(TAG, "Installing %d files.", count);
  268. for (int i = 0; i < count; i++)
  269. {
  270. snprintf(file_dir, sizeof(file_dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/%s/file%d.json", author, repo, i);
  271. FURI_LOG_I(TAG, "Loading file %s.", file_dir);
  272. FuriString *file = flipper_http_load_from_file(file_dir);
  273. if (file == NULL)
  274. {
  275. FURI_LOG_E(TAG, "Failed to load file.");
  276. return false;
  277. }
  278. FURI_LOG_I(TAG, "Loaded file %s.", file_dir);
  279. FuriString *name = get_json_value_furi("name", file);
  280. if (!name)
  281. {
  282. FURI_LOG_E(TAG, "Failed to get name.");
  283. furi_string_free(file);
  284. return false;
  285. }
  286. FuriString *link = get_json_value_furi("link", file);
  287. if (!link)
  288. {
  289. FURI_LOG_E(TAG, "Failed to get link.");
  290. furi_string_free(file);
  291. furi_string_free(name);
  292. return false;
  293. }
  294. furi_string_free(file);
  295. bool fetch_file()
  296. {
  297. return flip_store_download_github_file(fhttp, furi_string_get_cstr(name), author, repo, furi_string_get_cstr(link));
  298. }
  299. // download the file and wait until it is downloaded
  300. FURI_LOG_I(TAG, "Downloading file %s.", furi_string_get_cstr(name));
  301. if (!flipper_http_process_response_async(fhttp, fetch_file, parse))
  302. {
  303. FURI_LOG_E(TAG, "Failed to download file.");
  304. furi_string_free(name);
  305. furi_string_free(link);
  306. return false;
  307. }
  308. FURI_LOG_I(TAG, "Downloaded file %s.", furi_string_get_cstr(name));
  309. furi_string_free(name);
  310. furi_string_free(link);
  311. }
  312. return true;
  313. }