Files
armorpaint/Libraries/syslib/Sources/arm/sys/File.hx
T

161 lines
4.4 KiB
Haxe
Raw Normal View History

2019-11-13 13:51:17 +01:00
package arm.sys;
import haxe.io.Bytes;
class File {
#if krom_windows
static inline var cmd_dir = "dir /b";
2020-01-10 23:58:53 +01:00
static inline var cmd_dir_nofile = "dir /b /ad";
2019-11-13 13:51:17 +01:00
static inline var cmd_copy = "copy";
2019-11-13 14:36:36 +01:00
static inline var cmd_del = "del /f";
2019-11-13 13:51:17 +01:00
#else
static inline var cmd_dir = "ls";
2020-01-10 23:58:53 +01:00
static inline var cmd_dir_nofile = "ls";
2019-11-13 13:51:17 +01:00
static inline var cmd_copy = "cp";
2019-11-13 14:36:36 +01:00
static inline var cmd_del = "rm";
2019-11-13 13:51:17 +01:00
#end
2021-01-11 11:12:19 +01:00
static var cloud: Map<String, Array<String>> = null;
2021-01-19 16:43:18 +01:00
static var cloudSizes: Map<String, Int> = null;
2021-01-11 11:12:19 +01:00
2020-01-10 23:58:53 +01:00
public static function readDirectory(path: String, foldersOnly = false): Array<String> {
2021-01-11 11:12:19 +01:00
if (path.startsWith("cloud")) {
if (cloud == null) initCloud();
var files = cloud.get(path.replace("\\", "/"));
return files != null ? files : [];
}
2020-01-19 01:04:26 +01:00
return Krom.readDirectory(path, foldersOnly).split("\n");
2019-11-13 13:51:17 +01:00
}
2019-12-09 00:28:10 +01:00
public static function createDirectory(path: String) {
2021-02-03 19:22:37 +01:00
#if krom_windows
Krom.sysCommand("mkdir " + path); // -p by default
#else
Krom.sysCommand("mkdir -p " + path);
#end
2019-11-30 14:24:26 +01:00
}
2019-12-09 00:28:10 +01:00
public static function copy(srcPath: String, dstPath: String) {
2019-11-13 14:36:36 +01:00
Krom.sysCommand(cmd_copy + ' "' + srcPath + '" "' + dstPath + '"');
}
2019-12-09 00:28:10 +01:00
public static function start(path: String) {
2019-11-13 14:36:36 +01:00
#if krom_windows
Krom.sysCommand('start "" "' + path + '"');
#elseif krom_linux
Krom.sysCommand('xdg-open "' + path + '"');
#else
Krom.sysCommand('open "' + path + '"');
#end
}
2019-12-09 00:28:10 +01:00
public static function explorer(url: String) {
2019-11-13 14:36:36 +01:00
#if krom_windows
Krom.sysCommand('explorer "' + url + '"');
#elseif krom_linux
Krom.sysCommand('xdg-open "' + url + '"');
2020-02-22 19:23:42 +01:00
#elseif (krom_android || krom_ios)
2020-02-11 01:54:50 +01:00
Krom.loadUrl(url);
2019-11-13 14:36:36 +01:00
#else
Krom.sysCommand('open "' + url + '"');
#end
}
2019-12-09 00:28:10 +01:00
public static function delete(path: String) {
2019-11-13 14:36:36 +01:00
Krom.sysCommand(cmd_del + ' "' + path + '"');
}
2019-12-09 00:28:10 +01:00
public static function exists(path: String): Bool {
2020-09-29 13:38:46 +02:00
return Krom.fileExists(path);
2019-11-13 14:36:36 +01:00
}
2021-01-19 16:43:18 +01:00
public static function download(url: String, dstPath: String, size: Null<Int> = null) {
2019-11-13 14:36:36 +01:00
#if krom_windows
2021-01-19 16:43:18 +01:00
if (size == null) {
Krom.sysCommand('powershell -c "Invoke-WebRequest -Uri ' + url + " -OutFile '" + dstPath + "'");
}
else {
Krom.httpRequest(url, size, function(ab: js.lib.ArrayBuffer) {
if (ab != null) Krom.fileSaveBytes(dstPath, ab);
});
}
2019-11-13 14:36:36 +01:00
#else
Krom.sysCommand("curl -L " + url + " -o " + dstPath);
2019-11-13 14:36:36 +01:00
#end
}
2019-12-09 00:28:10 +01:00
public static function downloadBytes(url: String): Bytes {
2019-11-13 14:36:36 +01:00
var save = Path.data() + Path.sep + "download.bin";
download(url, save);
2020-01-22 13:06:40 +01:00
try {
return Bytes.ofData(Krom.loadBlob(save));
}
catch (e: Dynamic) {
return null;
}
2019-11-13 13:51:17 +01:00
}
2021-01-11 11:12:19 +01:00
2021-01-16 16:02:27 +01:00
public static function cacheCloud(path: String): String {
2021-02-03 19:22:37 +01:00
var dest = Krom.getFilesLocation() + Path.sep + path;
2021-01-16 16:02:27 +01:00
if (!File.exists(dest)) {
2021-02-03 19:22:37 +01:00
var fileDir = dest.substr(0, dest.lastIndexOf(Path.sep));
2021-01-16 16:02:27 +01:00
File.createDirectory(fileDir);
2021-01-19 16:43:18 +01:00
#if krom_windows
path = path.replace("\\", "/");
#end
2021-01-16 16:02:27 +01:00
var url = Config.raw.server + "/" + path;
2021-02-03 19:22:37 +01:00
File.download(url, dest, cloudSizes.get(path));
2021-01-16 16:02:27 +01:00
if (!File.exists(dest)) {
Log.error(Strings.error5());
return null;
}
}
2021-02-03 19:22:37 +01:00
#if krom_darwin
2021-01-16 16:02:27 +01:00
return dest;
2021-02-03 19:22:37 +01:00
#else
return Path.workingDir() + Path.sep + path;
#end
2021-01-16 16:02:27 +01:00
}
2021-01-11 11:12:19 +01:00
static function initCloud() {
cloud = [];
2021-01-19 16:43:18 +01:00
cloudSizes = [];
2021-01-11 11:12:19 +01:00
var files: Array<String> = [];
2021-01-19 16:43:18 +01:00
var sizes: Array<Int> = [];
2021-01-12 21:01:18 +01:00
var bytes = File.downloadBytes(Config.raw.server);
2021-01-16 16:02:27 +01:00
var dataPath = Path.data().startsWith(Path.workingDir()) ? Path.data() : Path.workingDir() + Path.sep + Path.data();
if (!File.exists(dataPath + Path.sep + "download.bin")) {
cloud.set("cloud", []);
Log.error(Strings.error5());
return;
}
2021-01-11 11:12:19 +01:00
for (e in Xml.parse(bytes.toString()).firstElement().elementsNamed("Contents")) {
for (k in e.elementsNamed("Key")) {
files.push(k.firstChild().nodeValue);
}
2021-01-19 16:43:18 +01:00
for (k in e.elementsNamed("Size")) {
sizes.push(Std.parseInt(k.firstChild().nodeValue));
}
2021-01-11 11:12:19 +01:00
}
for (file in files) {
if (Path.isFolder(file)) {
cloud.set(file.substr(0, file.length - 1), []);
}
}
2021-01-19 16:43:18 +01:00
for (i in 0...files.length) {
var file = files[i];
2021-01-11 11:12:19 +01:00
var nested = file.indexOf("/") != file.lastIndexOf("/");
if (nested) {
var delim = Path.isFolder(file) ? file.substr(0, file.length - 1).lastIndexOf("/") : file.lastIndexOf("/");
var parent = file.substr(0, delim);
var child = Path.isFolder(file) ? file.substring(delim + 1, file.length - 1) : file.substr(delim + 1);
cloud.get(parent).push(child);
2021-01-19 16:43:18 +01:00
if (!Path.isFolder(file)) {
cloudSizes.set(file, sizes[i]);
}
2021-01-11 11:12:19 +01:00
}
}
}
2019-11-13 13:51:17 +01:00
}