Improve interface accessibility and UI reliability
This commit is contained in:
parent
ba4476511f
commit
3734d4a168
29 changed files with 514 additions and 224 deletions
|
|
@ -7,19 +7,21 @@ const MAX_PORTABLE_FILE_BYTES := 16 * 1024 * 1024
|
|||
static func hash_file(path: String) -> String:
|
||||
if not FileAccess.file_exists(path):
|
||||
return ""
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
|
||||
if file == null or file.get_length() > MAX_PORTABLE_FILE_BYTES:
|
||||
return ""
|
||||
var bytes := file.get_buffer(file.get_length())
|
||||
var bytes: PackedByteArray = file.get_buffer(file.get_length())
|
||||
file.close()
|
||||
return hash_bytes(bytes)
|
||||
|
||||
|
||||
static func hash_bytes(bytes: PackedByteArray) -> String:
|
||||
var context := HashingContext.new()
|
||||
if context.start(HashingContext.HASH_SHA256) != OK:
|
||||
var context: HashingContext = HashingContext.new()
|
||||
var start_error: Error = context.start(HashingContext.HASH_SHA256)
|
||||
if start_error != OK:
|
||||
return ""
|
||||
if context.update(bytes) != OK:
|
||||
var update_error: Error = context.update(bytes)
|
||||
if update_error != OK:
|
||||
return ""
|
||||
return context.finish().hex_encode()
|
||||
|
||||
|
|
@ -27,10 +29,10 @@ static func hash_bytes(bytes: PackedByteArray) -> String:
|
|||
static func read_bytes(path: String, maximum_bytes: int = MAX_PORTABLE_FILE_BYTES) -> PackedByteArray:
|
||||
if not FileAccess.file_exists(path):
|
||||
return PackedByteArray()
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
|
||||
if file == null or file.get_length() > maximum_bytes:
|
||||
return PackedByteArray()
|
||||
var bytes := file.get_buffer(file.get_length())
|
||||
var bytes: PackedByteArray = file.get_buffer(file.get_length())
|
||||
file.close()
|
||||
return bytes
|
||||
|
||||
|
|
@ -42,10 +44,10 @@ static func write_guarded(
|
|||
conflict_directory: String,
|
||||
device_id: String,
|
||||
) -> Dictionary:
|
||||
var current_exists := FileAccess.file_exists(path)
|
||||
var current_hash := hash_file(path) if current_exists else ""
|
||||
var current_exists: bool = FileAccess.file_exists(path)
|
||||
var current_hash: String = hash_file(path) if current_exists else ""
|
||||
if current_hash != expected_hash:
|
||||
var conflict_path := _write_conflict_copy(
|
||||
var conflict_path: String = _write_conflict_copy(
|
||||
path, bytes, conflict_directory, device_id
|
||||
)
|
||||
return {
|
||||
|
|
@ -60,14 +62,14 @@ static func write_guarded(
|
|||
}
|
||||
if not _ensure_parent(path):
|
||||
return {"ok": false, "conflict": false, "hash": expected_hash}
|
||||
var temporary := path + ".tmp"
|
||||
var backup := path + ".backup"
|
||||
var file := FileAccess.open(temporary, FileAccess.WRITE)
|
||||
var temporary: String = path + ".tmp"
|
||||
var backup: String = path + ".backup"
|
||||
var file: FileAccess = FileAccess.open(temporary, FileAccess.WRITE)
|
||||
if file == null:
|
||||
return {"ok": false, "conflict": false, "hash": expected_hash}
|
||||
file.store_buffer(bytes)
|
||||
file.flush()
|
||||
var write_error := file.get_error()
|
||||
var write_error: Error = file.get_error()
|
||||
file.close()
|
||||
if write_error != OK:
|
||||
_remove(temporary)
|
||||
|
|
@ -90,13 +92,13 @@ static func write_guarded(
|
|||
|
||||
|
||||
static func has_syncthing_conflict(directory: String) -> bool:
|
||||
var access := DirAccess.open(directory)
|
||||
var access: DirAccess = DirAccess.open(directory)
|
||||
if access == null:
|
||||
return false
|
||||
access.list_dir_begin()
|
||||
var name := access.get_next()
|
||||
var name: String = access.get_next()
|
||||
while not name.is_empty():
|
||||
var lower := name.to_lower()
|
||||
var lower: String = name.to_lower()
|
||||
if "sync-conflict" in lower or ".syncthing." in lower:
|
||||
access.list_dir_end()
|
||||
return true
|
||||
|
|
@ -113,24 +115,24 @@ static func _write_conflict_copy(
|
|||
) -> String:
|
||||
if DirAccess.make_dir_recursive_absolute(conflict_directory) != OK:
|
||||
return ""
|
||||
var filename := canonical_path.get_file()
|
||||
var safe_device := device_id.left(16)
|
||||
var timestamp := Time.get_datetime_string_from_system().replace(":", "-")
|
||||
var destination := conflict_directory.path_join(
|
||||
var filename: String = canonical_path.get_file()
|
||||
var safe_device: String = device_id.left(16)
|
||||
var timestamp: String = Time.get_datetime_string_from_system().replace(":", "-")
|
||||
var destination: String = conflict_directory.path_join(
|
||||
"%s.local-%s-%s" % [filename, safe_device, timestamp]
|
||||
)
|
||||
var file := FileAccess.open(destination, FileAccess.WRITE)
|
||||
var file: FileAccess = FileAccess.open(destination, FileAccess.WRITE)
|
||||
if file == null:
|
||||
return ""
|
||||
file.store_buffer(bytes)
|
||||
file.flush()
|
||||
var ok := file.get_error() == OK
|
||||
var ok: bool = file.get_error() == OK
|
||||
file.close()
|
||||
return destination if ok else ""
|
||||
|
||||
|
||||
static func _ensure_parent(path: String) -> bool:
|
||||
var parent := path.get_base_dir()
|
||||
var parent: String = path.get_base_dir()
|
||||
return (
|
||||
DirAccess.dir_exists_absolute(parent)
|
||||
or DirAccess.make_dir_recursive_absolute(parent) == OK
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue