once again safer savings

Co-Authored-By: Ne_Eo <NeeEoo@users.noreply.github.com>
This commit is contained in:
⍚~Nex
2024-03-17 18:24:44 +01:00
co-authored by Ne_Eo
parent 3de8fcc804
commit 69c9030b9c
+25 -2
View File
@@ -78,13 +78,13 @@ class CoolUtil
}
/**
* Safe saves a file and shows a warning box instead of making the program crash
* Safe saves a file (even adding missing folder) and shows a warning box instead of making the program crash
* @param path Path to save the file at.
* @param data Content of the file to save.
*/
@:noUsing public static function safeSaveFile(path:String, content:String, showErrorBox:Bool = true) {
#if sys
try sys.io.File.saveContent(path, content)
try sys.io.File.saveContent(addMissingFolders(path), content)
catch(e) {
var errMsg:String = 'Error while trying to save the file: ${Std.string(e).replace('\n', ' ')}';
Logs.traceColored([Logs.logText(errMsg, RED)], ERROR);
@@ -93,6 +93,29 @@ class CoolUtil
#end
}
/**
* Creates eventual missing folders to the specified `path`
*
* WARNING: eventual files in `path` will be considered as folders! Just to make possible folders be named as `songs.json` for example
*
* @param path Path to check.
* @return The initial Path.
*/
@:noUsing public static function addMissingFolders(path:String):String {
#if sys
var folders:Array<String> = path.split("/");
var currentPath:String = "";
for (folder in folders) {
currentPath += folder + "/";
if (!sys.FileSystem.exists(currentPath)) {
sys.FileSystem.createDirectory(currentPath);
}
}
#end
return path;
}
/**
* Shortcut to parse a JSON string
* @param str Path to the JSON string