file.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. type file_download_data_t = {
  2. dst_path: string;
  3. done: (url: string)=>void;
  4. };
  5. type file_download_bytes_data_t = {
  6. url: string;
  7. save: string;
  8. done: (url: string, ab: buffer_t)=>void;
  9. };
  10. type file_cache_cloud_data_t = {
  11. dest: string;
  12. path: string;
  13. done: (dest: string)=>void;
  14. };
  15. let _file_download_map: map_t<string, file_download_data_t> = map_create();
  16. let _file_download_bytes_map: map_t<string, file_download_bytes_data_t> = map_create();
  17. let _file_cache_cloud_map: map_t<string, file_cache_cloud_data_t> = map_create();
  18. ///if arm_windows
  19. let file_cmd_mkdir: string = "mkdir";
  20. let file_cmd_copy: string = "copy";
  21. ///else
  22. let file_cmd_mkdir: string = "mkdir -p";
  23. let file_cmd_copy: string = "cp";
  24. ///end
  25. let file_cloud: map_t<string, string[]> = null;
  26. let file_cloud_sizes: map_t<string, i32> = null;
  27. let _file_init_cloud_bytes_done: ()=>void;
  28. ///if arm_android
  29. let file_internal: map_t<string, string[]> = null; // .apk contents
  30. ///end
  31. function file_read_directory(path: string): string[] {
  32. if (starts_with(path, "cloud")) {
  33. let files: string[] = file_cloud != null ? map_get(file_cloud, string_replace_all(path, "\\", "/")) : null;
  34. if (files != null) {
  35. return files;
  36. }
  37. else {
  38. let empty: string[] = [];
  39. return empty;
  40. }
  41. }
  42. ///if arm_android
  43. path = string_replace_all(path, "//", "/");
  44. if (file_internal == null) {
  45. let s: string = sys_buffer_to_string(data_get_blob("data_list.json"));
  46. file_internal = json_parse_to_map(s);
  47. }
  48. if (map_get(file_internal, path) != null) {
  49. return string_split(map_get(file_internal, path), ",");
  50. }
  51. ///end
  52. let files: string[] = string_split(iron_read_directory(path), "\n");
  53. array_sort(files, null);
  54. // Folders first
  55. let num: i32 = files.length;
  56. for (let i: i32 = 0; i < num; ++i) {
  57. let f: string = files[i];
  58. if (string_index_of(f, ".") > -1) {
  59. array_splice(files, i, 1);
  60. array_push(files, f);
  61. i--;
  62. num--;
  63. }
  64. }
  65. return files;
  66. }
  67. function file_create_directory(path: string) {
  68. iron_sys_command(file_cmd_mkdir + " \"" + path + "\"");
  69. }
  70. function file_copy(src_path: string, dst_path: string) {
  71. iron_sys_command(file_cmd_copy + " \"" + src_path + "\" \"" + dst_path + "\"");
  72. }
  73. function file_start(path: string) {
  74. ///if arm_windows
  75. iron_sys_command("start \"\" \"" + path + "\"");
  76. ///elseif arm_linux
  77. iron_sys_command("xdg-open \"" + path + "\"");
  78. ///else
  79. iron_sys_command("open \"" + path + "\"");
  80. ///end
  81. }
  82. function file_load_url(url: string) {
  83. iron_load_url(url);
  84. }
  85. function file_delete(path: string) {
  86. iron_delete_file(path);
  87. }
  88. function file_exists(path: string): bool {
  89. return iron_file_exists(path);
  90. }
  91. function file_download(url: string, dst_path: string, done: (url: string)=>void, size: i32 = 0) {
  92. ///if (arm_windows || arm_macos || arm_ios || arm_android)
  93. let fdd: file_download_data_t = { dst_path: dst_path, done: done };
  94. map_set(_file_download_map, url, fdd);
  95. _iron_http_request(url, size, function (url: string, ab: buffer_t) {
  96. let fdd: file_download_data_t = map_get(_file_download_map, url);
  97. if (ab != null) {
  98. iron_file_save_bytes(fdd.dst_path, ab, 0);
  99. }
  100. fdd.done(url);
  101. });
  102. ///elseif arm_linux
  103. iron_sys_command("wget -O \"" + dst_path + "\" \"" + url + "\"");
  104. done(url);
  105. ///else
  106. iron_sys_command("curl -L " + url + " -o \"" + dst_path + "\"");
  107. done(url);
  108. ///end
  109. }
  110. function file_download_bytes(url: string, done: (url: string, ab: buffer_t)=>void) {
  111. let save: string;
  112. if (path_is_protected()) {
  113. save = iron_internal_save_path();
  114. }
  115. else {
  116. save = path_data() + path_sep;
  117. }
  118. save += "download.bin";
  119. let fdbd: file_download_bytes_data_t = { url: url, save: save, done: done };
  120. map_set(_file_download_bytes_map, url, fdbd);
  121. file_download(url, save, function (url: string) {
  122. let fdbd: file_download_bytes_data_t = map_get(_file_download_bytes_map, url);
  123. let buffer: buffer_t = iron_load_blob(fdbd.save);
  124. fdbd.done(fdbd.url, buffer);
  125. });
  126. }
  127. function file_cache_cloud(path: string, done: (s: string)=>void) {
  128. ///if arm_ios
  129. let path2: string = string_replace_all(path, "/", "_"); // Cache everything into root folder
  130. ///else
  131. let path2: string = path;
  132. ///end
  133. let dest: string;
  134. if (path_is_protected()) {
  135. dest = iron_internal_save_path();
  136. }
  137. else {
  138. dest = iron_get_files_location() + path_sep;
  139. }
  140. dest += path2;
  141. if (file_exists(dest)) {
  142. ///if (arm_macos || arm_ios)
  143. done(dest);
  144. ///else
  145. let p: string;
  146. if (path_is_protected()) {
  147. p = iron_internal_save_path();
  148. }
  149. else {
  150. p = path_working_dir() + path_sep;
  151. }
  152. p += path;
  153. done(p);
  154. ///end
  155. return;
  156. }
  157. let file_dir: string = substring(dest, 0, string_last_index_of(dest, path_sep));
  158. if (file_read_directory(file_dir)[0] == "") {
  159. file_create_directory(file_dir);
  160. }
  161. ///if arm_windows
  162. path = string_replace_all(path, "\\", "/");
  163. ///end
  164. let url: string = config_raw.server + "/" + path;
  165. let fccd: file_cache_cloud_data_t = { dest: dest, path: path, done: done };
  166. map_set(_file_cache_cloud_map, url, fccd);
  167. file_download(url, dest, function (url: string) {
  168. let fccd: file_cache_cloud_data_t = map_get(_file_cache_cloud_map, url);
  169. if (!file_exists(fccd.dest)) {
  170. console_error(strings_check_internet_connection());
  171. fccd.done(null);
  172. return;
  173. }
  174. ///if (arm_macos || arm_ios)
  175. fccd.done(fccd.dest);
  176. ///else
  177. let p: string;
  178. if (path_is_protected()) {
  179. p = iron_internal_save_path();
  180. }
  181. else {
  182. p = path_working_dir() + path_sep;
  183. }
  184. p += fccd.path;
  185. fccd.done(p);
  186. ///end
  187. }, map_get(file_cloud_sizes, path));
  188. }
  189. function file_init_cloud_bytes(done: ()=>void, append: string = "") {
  190. _file_init_cloud_bytes_done = done;
  191. file_download_bytes(config_raw.server + "/?list-type=2" + append, function (url: string, buffer: buffer_t) {
  192. if (buffer == null) {
  193. let empty: string[] = [];
  194. map_set(file_cloud, "cloud", empty);
  195. console_error(strings_check_internet_connection());
  196. return;
  197. }
  198. let files: string[] = [];
  199. let sizes: i32[] = [];
  200. let str: string = sys_buffer_to_string(buffer);
  201. let pos_start: i32 = 0;
  202. let pos_end: i32 = 0;
  203. while (true) {
  204. pos_start = string_index_of_pos(str, "<Key>", pos_start);
  205. if (pos_start == -1) {
  206. break;
  207. }
  208. pos_start += 5; // <Key>
  209. pos_end = string_index_of_pos(str, "</Key>", pos_start);
  210. array_push(files, substring(str, pos_start, pos_end));
  211. pos_start = string_index_of_pos(str, "<Size>", pos_end);
  212. pos_start += 6; //<Size>
  213. pos_end = string_index_of_pos(str, "</Size>", pos_start);
  214. array_push(sizes, parse_int(substring(str, pos_start, pos_end)));
  215. }
  216. for (let i: i32 = 0; i < files.length; ++i) {
  217. let file: string = files[i];
  218. if (path_is_folder(file)) {
  219. let empty: string[] = [];
  220. map_set(file_cloud, substring(file, 0, file.length - 1), empty);
  221. }
  222. }
  223. for (let i: i32 = 0; i < files.length; ++i) {
  224. let file: string = files[i];
  225. let nested: bool = string_index_of(file, "/") != string_last_index_of(file, "/");
  226. if (nested) {
  227. let delim: i32 = path_is_folder(file) ? string_last_index_of(substring(file, 0, file.length - 1), "/") : string_last_index_of(file, "/");
  228. let parent: string = substring(file, 0, delim);
  229. let child: string = path_is_folder(file) ? substring(file, delim + 1, file.length - 1) : substring(file, delim + 1, file.length);
  230. array_push(map_get(file_cloud, parent), child);
  231. if (!path_is_folder(file)) {
  232. map_set(file_cloud_sizes, file, sizes[i]);
  233. }
  234. }
  235. }
  236. let is_truncated: bool = string_index_of(str, "<IsTruncated>true") > -1;
  237. if (is_truncated) {
  238. let pos_start: i32 = string_index_of(str, "<NextContinuationToken>");
  239. pos_start += 23;
  240. let pos_end: i32 = string_index_of_pos(str, "</NextContinuationToken>", pos_start);
  241. file_init_cloud_bytes(_file_init_cloud_bytes_done, "&start-after=" + substring(str, pos_start, pos_end));
  242. }
  243. else {
  244. _file_init_cloud_bytes_done();
  245. }
  246. });
  247. }
  248. function file_init_cloud(done: ()=>void) {
  249. file_cloud = map_create();
  250. file_cloud_sizes = map_create();
  251. file_init_cloud_bytes(done);
  252. }