file.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. type file_download_data_t = {
  2. done : (url: string) => void;
  3. };
  4. type file_cache_cloud_data_t = {
  5. dest: string; path : string; done : (dest: string) => void;
  6. };
  7. let _file_download_map: map_t<string, file_download_data_t> = map_create();
  8. let _file_cache_cloud_map: map_t<string, file_cache_cloud_data_t> = map_create();
  9. /// if arm_windows
  10. let file_cmd_copy: string = "copy";
  11. /// else
  12. let file_cmd_copy: string = "cp";
  13. /// end
  14. let file_cloud: map_t<string, string[]> = null;
  15. let file_cloud_sizes: map_t<string, i32> = null;
  16. let _file_init_cloud_bytes_done: () => void;
  17. /// if arm_android
  18. let file_internal: map_t<string, string[]> = null; // .apk contents
  19. /// end
  20. function file_read_directory(path: string): string[] {
  21. if (starts_with(path, "cloud")) {
  22. let files: string[] = file_cloud != null ? map_get(file_cloud, string_replace_all(path, "\\", "/")) : null;
  23. if (files != null) {
  24. return files;
  25. }
  26. else {
  27. let empty: string[] = [];
  28. return empty;
  29. }
  30. }
  31. /// if arm_android
  32. path = string_replace_all(path, "//", "/");
  33. if (file_internal == null) {
  34. let s: string = sys_buffer_to_string(data_get_blob("data_list.json"));
  35. file_internal = json_parse_to_map(s);
  36. }
  37. if (map_get(file_internal, path) != null) {
  38. return string_split(map_get(file_internal, path), ",");
  39. }
  40. /// end
  41. let files: string[] = string_split(iron_read_directory(path), "\n");
  42. array_sort(files, null);
  43. // Folders first
  44. let num: i32 = files.length;
  45. for (let i: i32 = 0; i < num; ++i) {
  46. let f: string = files[i];
  47. if (string_index_of(f, ".") > -1) {
  48. array_splice(files, i, 1);
  49. array_push(files, f);
  50. i--;
  51. num--;
  52. }
  53. }
  54. return files;
  55. }
  56. function file_create_directory(path: string) {
  57. iron_create_directory(path);
  58. }
  59. function file_copy(src_path: string, dst_path: string) {
  60. iron_sys_command(file_cmd_copy + " \"" + src_path + "\" \"" + dst_path + "\"");
  61. }
  62. function file_start(path: string) {
  63. /// if arm_windows
  64. iron_sys_command("start \"\" \"" + path + "\"");
  65. /// elseif arm_linux
  66. iron_sys_command("xdg-open \"" + path + "\"");
  67. /// else
  68. iron_sys_command("open \"" + path + "\"");
  69. /// end
  70. }
  71. function file_download_to(url: string, dst_path: string, done: (url: string) => void, size: i32 = 0) {
  72. let fdd: file_download_data_t = {done : done};
  73. map_set(_file_download_map, url, fdd);
  74. iron_file_download(url, function(url: string, ab: buffer_t) {
  75. let fdd: file_download_data_t = map_get(_file_download_map, url);
  76. fdd.done(url);
  77. }, size, dst_path);
  78. }
  79. function file_cache_cloud(path: string, done: (s: string) => void) {
  80. let dest: string;
  81. if (path_is_protected()) {
  82. dest = iron_internal_save_path();
  83. }
  84. else {
  85. dest = iron_internal_files_location() + path_sep;
  86. }
  87. dest += path;
  88. if (iron_file_exists(dest)) {
  89. done(dest);
  90. return;
  91. }
  92. let file_dir: string = substring(dest, 0, string_last_index_of(dest, path_sep));
  93. if (file_read_directory(file_dir)[0] == "") {
  94. file_create_directory(file_dir);
  95. }
  96. /// if arm_windows
  97. path = string_replace_all(path, "\\", "/");
  98. /// end
  99. let url: string = config_raw.server + "/" + path;
  100. let fccd: file_cache_cloud_data_t = {dest : dest, path : path, done : done};
  101. map_set(_file_cache_cloud_map, url, fccd);
  102. file_download_to(url, dest, function(url: string) {
  103. let fccd: file_cache_cloud_data_t = map_get(_file_cache_cloud_map, url);
  104. if (!iron_file_exists(fccd.dest)) {
  105. console_error(strings_check_internet_connection());
  106. fccd.done(null);
  107. return;
  108. }
  109. fccd.done(fccd.dest);
  110. }, map_get(file_cloud_sizes, path));
  111. }
  112. function file_init_cloud_bytes(done: () => void, append: string = "") {
  113. _file_init_cloud_bytes_done = done;
  114. iron_file_download(config_raw.server + "/?list-type=2" + append, function(url: string, buffer: buffer_t) {
  115. if (buffer == null) {
  116. let empty: string[] = [];
  117. map_set(file_cloud, "cloud", empty);
  118. console_error(strings_check_internet_connection());
  119. return;
  120. }
  121. let files: string[] = [];
  122. let sizes: i32[] = [];
  123. let str: string = sys_buffer_to_string(buffer);
  124. let pos_start: i32 = 0;
  125. let pos_end: i32 = 0;
  126. while (true) {
  127. pos_start = string_index_of_pos(str, "<Key>", pos_start);
  128. if (pos_start == -1) {
  129. break;
  130. }
  131. pos_start += 5; // <Key>
  132. pos_end = string_index_of_pos(str, "</Key>", pos_start);
  133. array_push(files, substring(str, pos_start, pos_end));
  134. pos_start = string_index_of_pos(str, "<Size>", pos_end);
  135. pos_start += 6; //<Size>
  136. pos_end = string_index_of_pos(str, "</Size>", pos_start);
  137. array_push(sizes, parse_int(substring(str, pos_start, pos_end)));
  138. }
  139. for (let i: i32 = 0; i < files.length; ++i) {
  140. let file: string = files[i];
  141. if (path_is_folder(file)) {
  142. let empty: string[] = [];
  143. map_set(file_cloud, substring(file, 0, file.length - 1), empty);
  144. }
  145. }
  146. for (let i: i32 = 0; i < files.length; ++i) {
  147. let file: string = files[i];
  148. let nested: bool = string_index_of(file, "/") != string_last_index_of(file, "/");
  149. if (nested) {
  150. let delim: i32 = path_is_folder(file) ? string_last_index_of(substring(file, 0, file.length - 1), "/") : string_last_index_of(file, "/");
  151. let parent: string = substring(file, 0, delim);
  152. let child: string = path_is_folder(file) ? substring(file, delim + 1, file.length - 1) : substring(file, delim + 1, file.length);
  153. array_push(map_get(file_cloud, parent), child);
  154. if (!path_is_folder(file)) {
  155. map_set(file_cloud_sizes, file, sizes[i]);
  156. }
  157. }
  158. }
  159. let is_truncated: bool = string_index_of(str, "<IsTruncated>true") > -1;
  160. if (is_truncated) {
  161. let pos_start: i32 = string_index_of(str, "<NextContinuationToken>");
  162. pos_start += 23;
  163. let pos_end: i32 = string_index_of_pos(str, "</NextContinuationToken>", pos_start);
  164. file_init_cloud_bytes(_file_init_cloud_bytes_done, "&start-after=" + substring(str, pos_start, pos_end));
  165. _file_init_cloud_bytes_done();
  166. }
  167. else {
  168. _file_init_cloud_bytes_done();
  169. }
  170. });
  171. }
  172. function file_init_cloud(done: () => void) {
  173. file_cloud = map_create();
  174. file_cloud_sizes = map_create();
  175. file_init_cloud_bytes(done);
  176. }