wifi_marauder_ep.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "wifi_marauder_ep.h"
  2. // returns success (if true, then caller needs to free(the_html))
  3. bool wifi_marauder_ep_read_html_file(WifiMarauderApp* app, uint8_t** the_html, size_t* html_size) {
  4. // browse for files
  5. FuriString* predefined_filepath = furi_string_alloc_set_str(MARAUDER_APP_FOLDER_HTML);
  6. FuriString* selected_filepath = furi_string_alloc();
  7. DialogsFileBrowserOptions browser_options;
  8. dialog_file_browser_set_basic_options(&browser_options, ".html", &I_Text_10x10);
  9. if(!dialog_file_browser_show(
  10. app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
  11. return false;
  12. }
  13. File* index_html = storage_file_alloc(app->storage);
  14. if(!storage_file_open(
  15. index_html, furi_string_get_cstr(selected_filepath), FSAM_READ, FSOM_OPEN_EXISTING)) {
  16. dialog_message_show_storage_error(app->dialogs, "Cannot open file");
  17. return false;
  18. }
  19. uint64_t size = storage_file_size(index_html);
  20. *the_html = malloc(size); // to be freed by caller
  21. uint8_t* buf_ptr = *the_html;
  22. size_t read = 0;
  23. while(read < size) {
  24. size_t to_read = size - read;
  25. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  26. uint16_t now_read = storage_file_read(index_html, buf_ptr, (uint16_t)to_read);
  27. read += now_read;
  28. buf_ptr += now_read;
  29. }
  30. *html_size = read;
  31. storage_file_close(index_html);
  32. storage_file_free(index_html);
  33. furi_string_free(selected_filepath);
  34. furi_string_free(predefined_filepath);
  35. return true;
  36. }