| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- type file_download_data_t = {
- dst_path: string;
- done: (url: string)=>void;
- };
- type file_download_bytes_data_t = {
- url: string;
- save: string;
- done: (url: string, ab: buffer_t)=>void;
- };
- type file_cache_cloud_data_t = {
- dest: string;
- path: string;
- done: (dest: string)=>void;
- };
- let _file_download_map: map_t<string, file_download_data_t> = map_create();
- let _file_download_bytes_map: map_t<string, file_download_bytes_data_t> = map_create();
- let _file_cache_cloud_map: map_t<string, file_cache_cloud_data_t> = map_create();
- ///if arm_windows
- let file_cmd_mkdir: string = "mkdir";
- let file_cmd_copy: string = "copy";
- ///else
- let file_cmd_mkdir: string = "mkdir -p";
- let file_cmd_copy: string = "cp";
- ///end
- let file_cloud: map_t<string, string[]> = null;
- let file_cloud_sizes: map_t<string, i32> = null;
- let _file_init_cloud_bytes_done: ()=>void;
- ///if arm_android
- let file_internal: map_t<string, string[]> = null; // .apk contents
- ///end
- function file_read_directory(path: string): string[] {
- if (starts_with(path, "cloud")) {
- let files: string[] = file_cloud != null ? map_get(file_cloud, string_replace_all(path, "\\", "/")) : null;
- if (files != null) {
- return files;
- }
- else {
- let empty: string[] = [];
- return empty;
- }
- }
- ///if arm_android
- path = string_replace_all(path, "//", "/");
- if (file_internal == null) {
- let s: string = sys_buffer_to_string(data_get_blob("data_list.json"));
- file_internal = json_parse_to_map(s);
- }
- if (map_get(file_internal, path) != null) {
- return string_split(map_get(file_internal, path), ",");
- }
- ///end
- let files: string[] = string_split(iron_read_directory(path), "\n");
- array_sort(files, null);
- // Folders first
- let num: i32 = files.length;
- for (let i: i32 = 0; i < num; ++i) {
- let f: string = files[i];
- if (string_index_of(f, ".") > -1) {
- array_splice(files, i, 1);
- array_push(files, f);
- i--;
- num--;
- }
- }
- return files;
- }
- function file_create_directory(path: string) {
- iron_sys_command(file_cmd_mkdir + " \"" + path + "\"");
- }
- function file_copy(src_path: string, dst_path: string) {
- iron_sys_command(file_cmd_copy + " \"" + src_path + "\" \"" + dst_path + "\"");
- }
- function file_start(path: string) {
- ///if arm_windows
- iron_sys_command("start \"\" \"" + path + "\"");
- ///elseif arm_linux
- iron_sys_command("xdg-open \"" + path + "\"");
- ///else
- iron_sys_command("open \"" + path + "\"");
- ///end
- }
- function file_load_url(url: string) {
- iron_load_url(url);
- }
- function file_delete(path: string) {
- iron_delete_file(path);
- }
- function file_exists(path: string): bool {
- return iron_file_exists(path);
- }
- function file_download(url: string, dst_path: string, done: (url: string)=>void, size: i32 = 0) {
- ///if (arm_windows || arm_macos || arm_ios || arm_android)
- let fdd: file_download_data_t = { dst_path: dst_path, done: done };
- map_set(_file_download_map, url, fdd);
- _iron_http_request(url, size, function (url: string, ab: buffer_t) {
- let fdd: file_download_data_t = map_get(_file_download_map, url);
- if (ab != null) {
- iron_file_save_bytes(fdd.dst_path, ab, 0);
- }
- fdd.done(url);
- });
- ///elseif arm_linux
- iron_sys_command("wget -O \"" + dst_path + "\" \"" + url + "\"");
- done(url);
- ///else
- iron_sys_command("curl -L " + url + " -o \"" + dst_path + "\"");
- done(url);
- ///end
- }
- function file_download_bytes(url: string, done: (url: string, ab: buffer_t)=>void) {
- let save: string;
- if (path_is_protected()) {
- save = iron_internal_save_path();
- }
- else {
- save = path_data() + path_sep;
- }
- save += "download.bin";
- let fdbd: file_download_bytes_data_t = { url: url, save: save, done: done };
- map_set(_file_download_bytes_map, url, fdbd);
- file_download(url, save, function (url: string) {
- let fdbd: file_download_bytes_data_t = map_get(_file_download_bytes_map, url);
- let buffer: buffer_t = iron_load_blob(fdbd.save);
- fdbd.done(fdbd.url, buffer);
- });
- }
- function file_cache_cloud(path: string, done: (s: string)=>void) {
- ///if arm_ios
- let path2: string = string_replace_all(path, "/", "_"); // Cache everything into root folder
- ///else
- let path2: string = path;
- ///end
- let dest: string;
- if (path_is_protected()) {
- dest = iron_internal_save_path();
- }
- else {
- dest = iron_get_files_location() + path_sep;
- }
- dest += path2;
- if (file_exists(dest)) {
- ///if (arm_macos || arm_ios)
- done(dest);
- ///else
- let p: string;
- if (path_is_protected()) {
- p = iron_internal_save_path();
- }
- else {
- p = path_working_dir() + path_sep;
- }
- p += path;
- done(p);
- ///end
- return;
- }
- let file_dir: string = substring(dest, 0, string_last_index_of(dest, path_sep));
- if (file_read_directory(file_dir)[0] == "") {
- file_create_directory(file_dir);
- }
- ///if arm_windows
- path = string_replace_all(path, "\\", "/");
- ///end
- let url: string = config_raw.server + "/" + path;
- let fccd: file_cache_cloud_data_t = { dest: dest, path: path, done: done };
- map_set(_file_cache_cloud_map, url, fccd);
- file_download(url, dest, function (url: string) {
- let fccd: file_cache_cloud_data_t = map_get(_file_cache_cloud_map, url);
- if (!file_exists(fccd.dest)) {
- console_error(strings_check_internet_connection());
- fccd.done(null);
- return;
- }
- ///if (arm_macos || arm_ios)
- fccd.done(fccd.dest);
- ///else
- let p: string;
- if (path_is_protected()) {
- p = iron_internal_save_path();
- }
- else {
- p = path_working_dir() + path_sep;
- }
- p += fccd.path;
- fccd.done(p);
- ///end
- }, map_get(file_cloud_sizes, path));
- }
- function file_init_cloud_bytes(done: ()=>void, append: string = "") {
- _file_init_cloud_bytes_done = done;
- file_download_bytes(config_raw.server + "/?list-type=2" + append, function (url: string, buffer: buffer_t) {
- if (buffer == null) {
- let empty: string[] = [];
- map_set(file_cloud, "cloud", empty);
- console_error(strings_check_internet_connection());
- return;
- }
- let files: string[] = [];
- let sizes: i32[] = [];
- let str: string = sys_buffer_to_string(buffer);
- let pos_start: i32 = 0;
- let pos_end: i32 = 0;
- while (true) {
- pos_start = string_index_of_pos(str, "<Key>", pos_start);
- if (pos_start == -1) {
- break;
- }
- pos_start += 5; // <Key>
- pos_end = string_index_of_pos(str, "</Key>", pos_start);
- array_push(files, substring(str, pos_start, pos_end));
- pos_start = string_index_of_pos(str, "<Size>", pos_end);
- pos_start += 6; //<Size>
- pos_end = string_index_of_pos(str, "</Size>", pos_start);
- array_push(sizes, parse_int(substring(str, pos_start, pos_end)));
- }
- for (let i: i32 = 0; i < files.length; ++i) {
- let file: string = files[i];
- if (path_is_folder(file)) {
- let empty: string[] = [];
- map_set(file_cloud, substring(file, 0, file.length - 1), empty);
- }
- }
- for (let i: i32 = 0; i < files.length; ++i) {
- let file: string = files[i];
- let nested: bool = string_index_of(file, "/") != string_last_index_of(file, "/");
- if (nested) {
- let delim: i32 = path_is_folder(file) ? string_last_index_of(substring(file, 0, file.length - 1), "/") : string_last_index_of(file, "/");
- let parent: string = substring(file, 0, delim);
- let child: string = path_is_folder(file) ? substring(file, delim + 1, file.length - 1) : substring(file, delim + 1, file.length);
- array_push(map_get(file_cloud, parent), child);
- if (!path_is_folder(file)) {
- map_set(file_cloud_sizes, file, sizes[i]);
- }
- }
- }
- let is_truncated: bool = string_index_of(str, "<IsTruncated>true") > -1;
- if (is_truncated) {
- let pos_start: i32 = string_index_of(str, "<NextContinuationToken>");
- pos_start += 23;
- let pos_end: i32 = string_index_of_pos(str, "</NextContinuationToken>", pos_start);
- file_init_cloud_bytes(_file_init_cloud_bytes_done, "&start-after=" + substring(str, pos_start, pos_end));
- }
- else {
- _file_init_cloud_bytes_done();
- }
- });
- }
- function file_init_cloud(done: ()=>void) {
- file_cloud = map_create();
- file_cloud_sizes = map_create();
- file_init_cloud_bytes(done);
- }
|