archive_files.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "archive_files.h"
  2. #include "archive_favorites.h"
  3. #include "../archive_i.h"
  4. bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) {
  5. furi_assert(file_info);
  6. furi_assert(tab_ext);
  7. furi_assert(name);
  8. bool result = false;
  9. if(strcmp(tab_ext, "*") == 0) {
  10. result = true;
  11. } else if(strstr(name, tab_ext) != NULL) {
  12. result = true;
  13. } else if(file_info->flags & FSF_DIRECTORY) {
  14. result = true;
  15. }
  16. return result;
  17. }
  18. void archive_trim_file_ext(char* name) {
  19. size_t str_len = strlen(name);
  20. char* end = name + str_len;
  21. while(end > name && *end != '.' && *end != '\\' && *end != '/') {
  22. --end;
  23. }
  24. if((end > name && *end == '.') && (*(end - 1) != '\\' && *(end - 1) != '/')) {
  25. *end = '\0';
  26. }
  27. }
  28. void set_file_type(ArchiveFile_t* file, FileInfo* file_info) {
  29. furi_assert(file);
  30. furi_assert(file_info);
  31. for(size_t i = 0; i < SIZEOF_ARRAY(known_ext); i++) {
  32. if(string_search_str(file->name, known_ext[i], 0) != STRING_FAILURE) {
  33. file->type = i;
  34. return;
  35. }
  36. }
  37. if(file_info->flags & FSF_DIRECTORY) {
  38. file->type = ArchiveFileTypeFolder;
  39. } else {
  40. file->type = ArchiveFileTypeUnknown;
  41. }
  42. }
  43. bool archive_get_filenames(void* context, uint8_t tab_id, const char* path) {
  44. furi_assert(context);
  45. ArchiveMainView* main_view = context;
  46. archive_file_array_clean(main_view);
  47. if(tab_id != ArchiveTabFavorites) {
  48. archive_read_dir(main_view, path);
  49. } else {
  50. archive_favorites_read(main_view);
  51. }
  52. return true;
  53. }
  54. bool archive_read_dir(void* context, const char* path) {
  55. furi_assert(context);
  56. ArchiveMainView* main_view = context;
  57. FileInfo file_info;
  58. Storage* fs_api = furi_record_open("storage");
  59. File* directory = storage_file_alloc(fs_api);
  60. char name[MAX_NAME_LEN];
  61. if(!storage_dir_open(directory, path)) {
  62. storage_dir_close(directory);
  63. storage_file_free(directory);
  64. return false;
  65. }
  66. while(1) {
  67. if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
  68. break;
  69. }
  70. uint16_t files_cnt = archive_file_array_size(main_view);
  71. if(files_cnt > MAX_FILES) {
  72. break;
  73. } else if(storage_file_get_error(directory) == FSE_OK) {
  74. archive_view_add_item(main_view, &file_info, name);
  75. } else {
  76. storage_dir_close(directory);
  77. storage_file_free(directory);
  78. return false;
  79. }
  80. }
  81. storage_dir_close(directory);
  82. storage_file_free(directory);
  83. furi_record_close("storage");
  84. return true;
  85. }
  86. void archive_file_append(const char* path, string_t string) {
  87. furi_assert(path);
  88. furi_assert(string);
  89. FileWorker* file_worker = file_worker_alloc(false);
  90. if(!file_worker_open(file_worker, path, FSAM_WRITE, FSOM_OPEN_APPEND)) {
  91. FURI_LOG_E("Archive", "Append open error");
  92. }
  93. if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
  94. FURI_LOG_E("Archive", "Append write error");
  95. }
  96. file_worker_close(file_worker);
  97. file_worker_free(file_worker);
  98. }
  99. void archive_delete_file(void* context, string_t path, string_t name) {
  100. furi_assert(context);
  101. furi_assert(path);
  102. furi_assert(name);
  103. ArchiveMainView* main_view = context;
  104. FileWorker* file_worker = file_worker_alloc(false);
  105. string_t full_path;
  106. string_init(full_path);
  107. string_printf(full_path, "%s/%s", string_get_cstr(path), string_get_cstr(name));
  108. file_worker_remove(file_worker, string_get_cstr(full_path));
  109. file_worker_free(file_worker);
  110. string_clear(full_path);
  111. if(archive_is_favorite(string_get_cstr(path), string_get_cstr(name))) {
  112. archive_favorites_delete(string_get_cstr(path), string_get_cstr(name));
  113. }
  114. archive_file_array_remove_selected(main_view);
  115. }