flip_store_github.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. furi_record_close(RECORD_STORAGE);
  45. // get the contents of the repo
  46. char link[256];
  47. snprintf(link, sizeof(link), "https://api.github.com/repos/%s/%s/contents", author, repo);
  48. fhttp->save_received_data = true;
  49. return flipper_http_get_request_with_headers(fhttp, link, "{\"Content-Type\":\"application/json\"}");
  50. }
  51. bool flip_store_parse_github_contents(char *file_path, const char *author, const char *repo)
  52. {
  53. // Parse list of files and prepare to download
  54. FuriString *git_data = flipper_http_load_from_file(file_path);
  55. if (git_data == NULL)
  56. {
  57. FURI_LOG_E(TAG, "Failed to load received data from file.");
  58. return false;
  59. }
  60. FuriString *git_data_str = furi_string_alloc();
  61. if (!git_data_str)
  62. {
  63. FURI_LOG_E("Game", "Failed to allocate json_data string");
  64. return false;
  65. }
  66. furi_string_cat_str(git_data_str, "{\"json_data\":");
  67. if (memmgr_get_free_heap() < furi_string_size(git_data) + furi_string_size(git_data_str) + 2)
  68. {
  69. FURI_LOG_E(TAG, "Not enough memory to allocate git_data_str.");
  70. furi_string_free(git_data);
  71. furi_string_free(git_data_str);
  72. return false;
  73. }
  74. furi_string_cat(git_data_str, git_data);
  75. furi_string_free(git_data);
  76. furi_string_cat_str(git_data_str, "}");
  77. //
  78. int file_count = 0;
  79. for (int i = 0; i < MAX_GITHUB_FILES; i++)
  80. {
  81. FuriString *json_data_array = get_json_array_value_furi("json_data", i, git_data_str);
  82. if (!json_data_array)
  83. {
  84. break;
  85. }
  86. FuriString *type = get_json_value_furi("type", json_data_array);
  87. if (!type)
  88. {
  89. FURI_LOG_E(TAG, "Failed to get type.");
  90. furi_string_free(json_data_array);
  91. break;
  92. }
  93. // skip directories for now
  94. if (strcmp(furi_string_get_cstr(type), "file") != 0)
  95. {
  96. furi_string_free(type);
  97. furi_string_free(json_data_array);
  98. continue;
  99. }
  100. furi_string_free(type);
  101. FuriString *download_url = get_json_value_furi("download_url", json_data_array);
  102. if (!download_url)
  103. {
  104. FURI_LOG_E(TAG, "Failed to get download_url.");
  105. furi_string_free(json_data_array);
  106. break;
  107. }
  108. FuriString *name = get_json_value_furi("name", json_data_array);
  109. if (!name)
  110. {
  111. FURI_LOG_E(TAG, "Failed to get name.");
  112. furi_string_free(json_data_array);
  113. furi_string_free(download_url);
  114. break;
  115. }
  116. furi_string_free(json_data_array);
  117. // create json to save
  118. FuriString *json = furi_string_alloc();
  119. if (!json)
  120. {
  121. FURI_LOG_E(TAG, "Failed to allocate json.");
  122. furi_string_free(json_data_array);
  123. furi_string_free(download_url);
  124. furi_string_free(name);
  125. break;
  126. }
  127. furi_string_cat_str(json, "{\"name\":\"");
  128. furi_string_cat(json, name);
  129. furi_string_cat_str(json, "\",\"link\":\"");
  130. furi_string_cat(json, download_url);
  131. furi_string_free(download_url);
  132. furi_string_cat_str(json, "\"}");
  133. // save the json to the data folder: /ext/apps_data/flip_store/data/author/repo
  134. char dir[256];
  135. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/%s/%s.json", author, repo, furi_string_get_cstr(name));
  136. if (!save_char_with_path(dir, furi_string_get_cstr(json)))
  137. {
  138. FURI_LOG_E(TAG, "Failed to save json.");
  139. }
  140. // free the json
  141. furi_string_free(name);
  142. furi_string_free(json);
  143. file_count++;
  144. }
  145. furi_string_free(git_data_str);
  146. // save file count
  147. char file_count_str[16];
  148. snprintf(file_count_str, sizeof(file_count_str), "%d", file_count);
  149. char file_count_dir[256]; // /ext/apps_data/flip_store/data/author/file_count.txt
  150. snprintf(file_count_dir, sizeof(file_count_dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s/file_count.txt", author);
  151. return save_char_with_path(file_count_dir, file_count_str);
  152. }