file:// support
This commit is contained in:
52
flumi/Scripts/Utils/FileUtils.gd
Normal file
52
flumi/Scripts/Utils/FileUtils.gd
Normal file
@@ -0,0 +1,52 @@
|
||||
class_name FileUtils
|
||||
extends RefCounted
|
||||
|
||||
static func read_local_file(file_path: String) -> Dictionary:
|
||||
var result = {"success": false, "content": PackedByteArray(), "error": ""}
|
||||
|
||||
if not FileAccess.file_exists(file_path):
|
||||
result.error = "File not found: " + file_path
|
||||
return result
|
||||
|
||||
var file = FileAccess.open(file_path, FileAccess.READ)
|
||||
if not file:
|
||||
result.error = "Cannot open file: " + file_path
|
||||
return result
|
||||
|
||||
var content = file.get_buffer(file.get_length())
|
||||
file.close()
|
||||
|
||||
result.success = true
|
||||
result.content = content
|
||||
return result
|
||||
|
||||
static func is_directory(path: String) -> bool:
|
||||
return DirAccess.dir_exists_absolute(path)
|
||||
|
||||
static func is_html_file(file_path: String) -> bool:
|
||||
var extension = file_path.get_extension().to_lower()
|
||||
return extension in ["html", "htm"]
|
||||
|
||||
static func is_supported_file(file_path: String) -> bool:
|
||||
var extension = file_path.get_extension().to_lower()
|
||||
return extension in ["html", "htm", "txt", "css", "js"]
|
||||
|
||||
static func create_error_page(title: String, error_message: String) -> PackedByteArray:
|
||||
var html = """<head>
|
||||
<title>""" + title + """ - File Browser</title>
|
||||
<style>
|
||||
body { bg-[#ffffff] text-[#202124] font-sans p-6 m-0 }
|
||||
.error-container { max-w-[600px] mx-auto text-center mt-20 }
|
||||
.error-icon { text-6xl mb-4 }
|
||||
.error-title { text-2xl font-normal mb-4 }
|
||||
.error-message { text-[#5f6368] mb-6 }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div style="error-container">
|
||||
<h1 style="error-title">""" + title + """</h1>
|
||||
<p style="error-message">""" + error_message + """</p>
|
||||
</div>
|
||||
</body>"""
|
||||
|
||||
return html.to_utf8_buffer()
|
||||
1
flumi/Scripts/Utils/FileUtils.gd.uid
Normal file
1
flumi/Scripts/Utils/FileUtils.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://djcyjusk64cyr
|
||||
@@ -3,7 +3,7 @@ extends RefCounted
|
||||
|
||||
static func resolve_url(base_url: String, relative_url: String) -> String:
|
||||
# If relative_url is already absolute, return it as-is
|
||||
if relative_url.begins_with("http://") or relative_url.begins_with("https://") or relative_url.begins_with("gurt://"):
|
||||
if relative_url.begins_with("http://") or relative_url.begins_with("https://") or relative_url.begins_with("gurt://") or relative_url.begins_with("file://"):
|
||||
return relative_url
|
||||
|
||||
# If empty, treat as relative to current domain
|
||||
@@ -20,6 +20,23 @@ static func resolve_url(base_url: String, relative_url: String) -> String:
|
||||
var scheme = clean_base.substr(0, scheme_end + 3)
|
||||
var remainder = clean_base.substr(scheme_end + 3)
|
||||
|
||||
if scheme == "file://":
|
||||
var file_path = remainder
|
||||
|
||||
if OS.get_name() == "Windows":
|
||||
file_path = file_path.replace("/", "\\")
|
||||
|
||||
if relative_url.begins_with("/"):
|
||||
return scheme + relative_url.substr(1)
|
||||
else:
|
||||
var base_dir = file_path.get_base_dir()
|
||||
if base_dir.is_empty():
|
||||
return scheme + relative_url
|
||||
else:
|
||||
var resolved_path = base_dir + "/" + relative_url
|
||||
resolved_path = resolved_path.replace("\\", "/")
|
||||
return scheme + resolved_path
|
||||
|
||||
# Split remainder into host and path
|
||||
var first_slash = remainder.find("/")
|
||||
var host = ""
|
||||
@@ -77,9 +94,44 @@ static func extract_domain(url: String) -> String:
|
||||
clean_url = clean_url.substr(8)
|
||||
elif clean_url.begins_with("http://"):
|
||||
clean_url = clean_url.substr(7)
|
||||
elif clean_url.begins_with("file://"):
|
||||
return "localhost"
|
||||
|
||||
var slash_pos = clean_url.find("/")
|
||||
if slash_pos != -1:
|
||||
clean_url = clean_url.substr(0, slash_pos)
|
||||
|
||||
return clean_url
|
||||
|
||||
static func is_local_file_url(url: String) -> bool:
|
||||
return url.begins_with("file://")
|
||||
|
||||
static func file_url_to_path(url: String) -> String:
|
||||
if not is_local_file_url(url):
|
||||
return ""
|
||||
|
||||
var path = url.substr(7) # Remove "file://"
|
||||
|
||||
if path.begins_with("/") and path.length() > 2 and path.substr(2, 1) == ":":
|
||||
path = path.substr(1)
|
||||
elif path.length() > 1 and path.substr(1, 1) == ":":
|
||||
pass
|
||||
|
||||
if OS.get_name() == "Windows":
|
||||
path = path.replace("/", "\\")
|
||||
|
||||
return path
|
||||
|
||||
static func path_to_file_url(path: String) -> String:
|
||||
var clean_path = path
|
||||
|
||||
clean_path = clean_path.replace("\\", "/")
|
||||
|
||||
if OS.get_name() == "Windows":
|
||||
if not clean_path.begins_with("/"):
|
||||
clean_path = "/" + clean_path
|
||||
else:
|
||||
if not clean_path.begins_with("/"):
|
||||
clean_path = "/" + clean_path
|
||||
|
||||
return "file://" + clean_path
|
||||
|
||||
Reference in New Issue
Block a user