Files
armorpaint/base/sources/ts/file.ts
T

289 lines
7.8 KiB
TypeScript
Raw Normal View History

2024-03-09 14:52:33 +01:00
2024-11-08 23:46:39 +01:00
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();
2024-09-06 21:11:46 +02:00
///if arm_windows
2024-03-09 14:52:33 +01:00
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
2024-03-11 15:10:18 +01:00
let file_cloud: map_t<string, string[]> = null;
let file_cloud_sizes: map_t<string, i32> = null;
2024-03-09 14:52:33 +01:00
2024-11-08 23:46:39 +01:00
let _file_init_cloud_bytes_done: ()=>void;
2024-11-14 21:59:12 +01:00
///if arm_android
let file_internal: map_t<string, string[]> = null; // .apk contents
///end
2024-03-09 14:52:33 +01:00
2024-09-29 23:48:42 +02:00
function file_read_directory(path: string): string[] {
2024-03-20 16:13:54 +01:00
if (starts_with(path, "cloud")) {
let files: string[] = file_cloud != null ? map_get(file_cloud, string_replace_all(path, "\\", "/")) : null;
2024-05-03 21:29:39 +02:00
if (files != null) {
return files;
}
else {
let empty: string[] = [];
return empty;
}
2024-03-09 14:52:33 +01:00
}
2024-11-22 19:41:12 +01:00
2024-11-14 21:59:12 +01:00
///if arm_android
path = string_replace_all(path, "//", "/");
if (file_internal == null) {
2024-11-14 22:06:55 +01:00
let s: string = sys_buffer_to_string(data_get_blob("data_list.json"));
2024-11-14 21:59:12 +01:00
file_internal = json_parse_to_map(s);
}
if (map_get(file_internal, path) != null) {
return string_split(map_get(file_internal, path), ",");
}
///end
2024-10-01 18:29:24 +02:00
let files: string[] = string_split(iron_read_directory(path), "\n");
2025-01-15 19:51:31 +01:00
array_sort(files, null);
2024-11-22 19:41:12 +01:00
// Folders first
2024-10-01 18:29:24 +02:00
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--;
}
}
2024-11-22 19:41:12 +01:00
2024-10-01 18:29:24 +02:00
return files;
2024-03-09 14:52:33 +01:00
}
function file_create_directory(path: string) {
2024-09-03 16:39:16 +02:00
iron_sys_command(file_cmd_mkdir + " \"" + path + "\"");
2024-03-09 14:52:33 +01:00
}
2025-01-02 16:32:43 +01:00
function file_copy(src_path: string, dst_path: string) {
iron_sys_command(file_cmd_copy + " \"" + src_path + "\" \"" + dst_path + "\"");
2024-03-09 14:52:33 +01:00
}
function file_start(path: string) {
2024-09-06 21:11:46 +02:00
///if arm_windows
2024-09-03 16:39:16 +02:00
iron_sys_command("start \"\" \"" + path + "\"");
2024-09-06 21:11:46 +02:00
///elseif arm_linux
2024-09-03 16:39:16 +02:00
iron_sys_command("xdg-open \"" + path + "\"");
2024-03-09 14:52:33 +01:00
///else
2024-09-03 16:39:16 +02:00
iron_sys_command("open \"" + path + "\"");
2024-03-09 14:52:33 +01:00
///end
}
function file_load_url(url: string) {
2025-03-09 17:02:12 +01:00
iron_load_url(url);
2024-03-09 14:52:33 +01:00
}
function file_delete(path: string) {
2024-09-03 16:39:16 +02:00
iron_delete_file(path);
2024-03-09 14:52:33 +01:00
}
function file_exists(path: string): bool {
2024-09-03 16:39:16 +02:00
return iron_file_exists(path);
2024-03-09 14:52:33 +01:00
}
2024-04-07 09:52:45 +02:00
function file_download(url: string, dst_path: string, done: (url: string)=>void, size: i32 = 0) {
2024-09-06 21:11:46 +02:00
///if (arm_windows || arm_macos || arm_ios || arm_android)
2024-04-07 09:52:45 +02:00
let fdd: file_download_data_t = { dst_path: dst_path, done: done };
map_set(_file_download_map, url, fdd);
2025-03-09 17:02:12 +01:00
_iron_http_request(url, size, function (url: string, ab: buffer_t) {
2024-04-07 09:52:45 +02:00
let fdd: file_download_data_t = map_get(_file_download_map, url);
2024-03-20 16:13:54 +01:00
if (ab != null) {
2024-09-03 16:39:16 +02:00
iron_file_save_bytes(fdd.dst_path, ab, 0);
2024-03-20 16:13:54 +01:00
}
2024-04-07 09:52:45 +02:00
fdd.done(url);
2024-03-09 14:52:33 +01:00
});
2024-09-06 21:11:46 +02:00
///elseif arm_linux
2024-09-03 16:39:16 +02:00
iron_sys_command("wget -O \"" + dst_path + "\" \"" + url + "\"");
2024-04-07 09:52:45 +02:00
done(url);
2024-03-09 14:52:33 +01:00
///else
2024-09-03 16:39:16 +02:00
iron_sys_command("curl -L " + url + " -o \"" + dst_path + "\"");
2024-04-07 09:52:45 +02:00
done(url);
2024-03-09 14:52:33 +01:00
///end
}
2024-04-07 09:52:45 +02:00
function file_download_bytes(url: string, done: (url: string, ab: buffer_t)=>void) {
2024-05-03 21:29:39 +02:00
let save: string;
if (path_is_protected()) {
2025-03-09 17:02:12 +01:00
save = iron_internal_save_path();
2024-05-03 21:29:39 +02:00
}
else {
save = path_data() + path_sep;
}
save += "download.bin";
2024-04-07 09:52:45 +02:00
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);
2024-09-03 16:39:16 +02:00
let buffer: buffer_t = iron_load_blob(fdbd.save);
2024-04-07 09:52:45 +02:00
fdbd.done(fdbd.url, buffer);
2024-03-09 14:52:33 +01:00
});
}
function file_cache_cloud(path: string, done: (s: string)=>void) {
2024-09-06 21:11:46 +02:00
///if arm_ios
2024-03-09 14:52:33 +01:00
let path2: string = string_replace_all(path, "/", "_"); // Cache everything into root folder
///else
let path2: string = path;
///end
2024-05-03 21:29:39 +02:00
let dest: string;
if (path_is_protected()) {
2025-03-09 17:02:12 +01:00
dest = iron_internal_save_path();
2024-05-03 21:29:39 +02:00
}
else {
2024-09-03 16:39:16 +02:00
dest = iron_get_files_location() + path_sep;
2024-05-03 21:29:39 +02:00
}
dest += path2;
2024-03-09 14:52:33 +01:00
if (file_exists(dest)) {
2024-09-06 21:11:46 +02:00
///if (arm_macos || arm_ios)
2024-03-09 14:52:33 +01:00
done(dest);
///else
2024-05-03 21:29:39 +02:00
let p: string;
if (path_is_protected()) {
2025-03-09 17:02:12 +01:00
p = iron_internal_save_path();
2024-05-03 21:29:39 +02:00
}
else {
p = path_working_dir() + path_sep;
}
p += path;
done(p);
2024-03-09 14:52:33 +01:00
///end
return;
}
2024-03-20 16:13:54 +01:00
let file_dir: string = substring(dest, 0, string_last_index_of(dest, path_sep));
2024-03-09 14:52:33 +01:00
if (file_read_directory(file_dir)[0] == "") {
file_create_directory(file_dir);
}
2024-09-06 21:11:46 +02:00
///if arm_windows
2024-03-09 14:52:33 +01:00
path = string_replace_all(path, "\\", "/");
///end
let url: string = config_raw.server + "/" + path;
2024-04-07 09:52:45 +02:00
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);
2024-05-03 21:29:39 +02:00
if (!file_exists(fccd.dest)) {
2025-01-19 20:35:39 +01:00
console_error(strings_check_internet_connection());
2024-04-07 09:52:45 +02:00
fccd.done(null);
2024-03-09 14:52:33 +01:00
return;
}
2024-09-06 21:11:46 +02:00
///if (arm_macos || arm_ios)
2024-04-07 09:52:45 +02:00
fccd.done(fccd.dest);
2024-03-09 14:52:33 +01:00
///else
2024-05-03 21:29:39 +02:00
let p: string;
if (path_is_protected()) {
2025-03-09 17:02:12 +01:00
p = iron_internal_save_path();
2024-05-03 21:29:39 +02:00
}
else {
p = path_working_dir() + path_sep;
}
p += fccd.path;
fccd.done(p);
2024-03-09 14:52:33 +01:00
///end
2024-03-20 16:13:54 +01:00
}, map_get(file_cloud_sizes, path));
2024-03-09 14:52:33 +01:00
}
function file_init_cloud_bytes(done: ()=>void, append: string = "") {
2024-04-07 09:52:45 +02:00
_file_init_cloud_bytes_done = done;
file_download_bytes(config_raw.server + "/?list-type=2" + append, function (url: string, buffer: buffer_t) {
2024-03-09 14:52:33 +01:00
if (buffer == null) {
2024-05-03 21:29:39 +02:00
let empty: string[] = [];
map_set(file_cloud, "cloud", empty);
2025-01-19 20:35:39 +01:00
console_error(strings_check_internet_connection());
2024-03-09 14:52:33 +01:00
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) {
2024-03-20 16:13:54 +01:00
pos_start = string_index_of_pos(str, "<Key>", pos_start);
if (pos_start == -1) {
break;
}
2024-03-09 14:52:33 +01:00
pos_start += 5; // <Key>
2024-03-20 16:13:54 +01:00
pos_end = string_index_of_pos(str, "</Key>", pos_start);
2024-03-09 14:52:33 +01:00
2024-03-20 16:13:54 +01:00
array_push(files, substring(str, pos_start, pos_end));
2024-03-09 14:52:33 +01:00
2024-03-20 16:13:54 +01:00
pos_start = string_index_of_pos(str, "<Size>", pos_end);
2024-03-09 14:52:33 +01:00
pos_start += 6; //<Size>
2024-03-20 16:13:54 +01:00
pos_end = string_index_of_pos(str, "</Size>", pos_start);
2024-03-09 14:52:33 +01:00
2024-05-03 21:29:39 +02:00
array_push(sizes, parse_int(substring(str, pos_start, pos_end)));
2024-03-09 14:52:33 +01:00
}
2024-03-21 21:06:41 +01:00
for (let i: i32 = 0; i < files.length; ++i) {
let file: string = files[i];
2024-03-09 14:52:33 +01:00
if (path_is_folder(file)) {
2024-05-03 21:29:39 +02:00
let empty: string[] = [];
map_set(file_cloud, substring(file, 0, file.length - 1), empty);
2024-03-09 14:52:33 +01:00
}
}
for (let i: i32 = 0; i < files.length; ++i) {
let file: string = files[i];
2024-03-20 16:13:54 +01:00
let nested: bool = string_index_of(file, "/") != string_last_index_of(file, "/");
2024-03-09 14:52:33 +01:00
if (nested) {
2024-03-20 16:13:54 +01:00
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);
2024-03-09 14:52:33 +01:00
if (!path_is_folder(file)) {
2024-03-20 16:13:54 +01:00
map_set(file_cloud_sizes, file, sizes[i]);
2024-03-09 14:52:33 +01:00
}
}
}
2024-03-20 16:13:54 +01:00
let is_truncated: bool = string_index_of(str, "<IsTruncated>true") > -1;
2024-03-09 14:52:33 +01:00
if (is_truncated) {
2024-03-20 16:13:54 +01:00
let pos_start: i32 = string_index_of(str, "<NextContinuationToken>");
2024-03-09 14:52:33 +01:00
pos_start += 23;
2024-03-20 16:13:54 +01:00
let pos_end: i32 = string_index_of_pos(str, "</NextContinuationToken>", pos_start);
2024-04-07 09:52:45 +02:00
file_init_cloud_bytes(_file_init_cloud_bytes_done, "&start-after=" + substring(str, pos_start, pos_end));
}
else {
_file_init_cloud_bytes_done();
2024-03-09 14:52:33 +01:00
}
});
}
function file_init_cloud(done: ()=>void) {
2024-03-11 15:10:18 +01:00
file_cloud = map_create();
file_cloud_sizes = map_create();
2024-03-09 14:52:33 +01:00
file_init_cloud_bytes(done);
}