From b1cb64ce25ff30e58ad161f5cffe288bb29113d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Santilio?= Date: Mon, 26 Jul 2021 09:33:27 +0200 Subject: [PATCH] support upsert for storage.upload() --- .gitignore | 16 ------------ addons/supabase/Auth/auth.gd | 1 - addons/supabase/Database/database.gd | 1 + addons/supabase/Database/query.gd | 31 ++++++++++++++--------- addons/supabase/Storage/storage.gd | 7 +---- addons/supabase/Storage/storage_bucket.gd | 9 +++---- 6 files changed, 25 insertions(+), 40 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b6078ba..0000000 --- a/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -# Plugin Specific ignores -default_env.tres -icon.png -icon.png.import -project.godot -scn/ -override.cfg - -# Godot-specific ignores -.import/ -export.cfg -export_presets.cfg - -# Mono-specific ignores -.mono/ -data_*/ diff --git a/addons/supabase/Auth/auth.gd b/addons/supabase/Auth/auth.gd index 2713dcf..9c255b2 100644 --- a/addons/supabase/Auth/auth.gd +++ b/addons/supabase/Auth/auth.gd @@ -228,7 +228,6 @@ func _on_task_completed(task : AuthTask) -> void: elif task.error != null: emit_signal("error", task.error) _pooled_tasks.erase(task) - # A timer used to listen through TCP on the redirect uri of the request func _tcp_stream_timer() -> void: diff --git a/addons/supabase/Database/database.gd b/addons/supabase/Database/database.gd index ce63724..116e171 100644 --- a/addons/supabase/Database/database.gd +++ b/addons/supabase/Database/database.gd @@ -70,3 +70,4 @@ func _on_task_completed(task : DatabaseTask) -> void: emit_signal("rpc_completed", task.data) elif task.error != null: emit_signal("error", task.error) + _pooled_tasks.erase(task) diff --git a/addons/supabase/Database/query.gd b/addons/supabase/Database/query.gd index 229d6fe..f22f0f7 100644 --- a/addons/supabase/Database/query.gd +++ b/addons/supabase/Database/query.gd @@ -18,6 +18,7 @@ var query_struct : Dictionary = { } var query : String = "" +var raw_query : String = "" var header : PoolStringArray = [] var request : int var body : String = "" @@ -58,21 +59,27 @@ enum Filters { WFTS } -func _init(): - pass +func _init(_raw_query : String = "", _raw_type : int = -1, _raw_header : PoolStringArray = [], _raw_body : String = ""): + if _raw_query != "": + raw_query = _raw_query + query = _raw_query + request = _raw_type + header = _raw_header as PoolStringArray + body = _raw_body # Build the query from the scrut func build_query() -> String: - for key in query_struct: - if query_struct[key].empty(): continue - match key: - "table": - query += query_struct[key] - "select", "order": - if query_struct[key].empty(): continue - query += (key + "=" + PoolStringArray(query_struct[key]).join(",")+"&") - "eq", "neq", "lt", "gt", "lte", "gte", "like", "ilike", "IS", "in", "fts", "plfts", "phfts", "wfts": - query += PoolStringArray(query_struct[key]).join("&") + if raw_query == "" and query == raw_query: + for key in query_struct: + if query_struct[key].empty(): continue + match key: + "table": + query += query_struct[key] + "select", "order": + if query_struct[key].empty(): continue + query += (key + "=" + PoolStringArray(query_struct[key]).join(",")+"&") + "eq", "neq", "lt", "gt", "lte", "gte", "like", "ilike", "IS", "in", "fts", "plfts", "phfts", "wfts": + query += PoolStringArray(query_struct[key]).join("&") return query diff --git a/addons/supabase/Storage/storage.gd b/addons/supabase/Storage/storage.gd index 288e735..b8817bb 100644 --- a/addons/supabase/Storage/storage.gd +++ b/addons/supabase/Storage/storage.gd @@ -22,11 +22,6 @@ func _init(config : Dictionary) -> void: _config = config name = "Storage" -func _ready() -> void: - var buckets : Node = Node.new() - buckets.set_name("Buckets") - add_child(buckets) - func list_buckets() -> StorageTask: _bearer = Supabase.auth._bearer var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket" @@ -102,7 +97,7 @@ func delete_bucket(id : String) -> StorageTask: func from(id : String) -> StorageBucket: - for bucket in get_node("Buckets").get_children(): + for bucket in get_children(): if bucket.id == id: return bucket var storage_bucket : StorageBucket = StorageBucket.new(id, _config) diff --git a/addons/supabase/Storage/storage_bucket.gd b/addons/supabase/Storage/storage_bucket.gd index 6703788..2b7e613 100644 --- a/addons/supabase/Storage/storage_bucket.gd +++ b/addons/supabase/Storage/storage_bucket.gd @@ -24,7 +24,7 @@ const MIME_TYPES : Dictionary = { "tiff": "image/tiff", "tres": "text/plain", "tscn": "text/plain", - "txt": "text/plain", + "txt": "text/script", "wav": "audio/wav", "webm": "video/webm", "webp": "video/webm", @@ -84,7 +84,7 @@ func list(prefix : String = "", limit : int = 100, offset : int = 0, sort_by : D return task -func upload(object : String, file_path : String) -> StorageTask: +func upload(object : String, file_path : String, upsert : bool = false) -> StorageTask: requesting_raw = true _bearer = Supabase.auth._bearer var task : StorageTask = StorageTask.new() @@ -92,17 +92,16 @@ func upload(object : String, file_path : String) -> StorageTask: var file : File = File.new() var error : int = file.open(file_path, File.READ) if error != OK: - printerr("There was an error opening the file at path: ", file_path) task.complete({}) return task - var header : PoolStringArray = [_header[0] % MIME_TYPES.get(object.get_extension(), "application/octet-stream")] + var header : PoolStringArray = [_header[0] % MIME_TYPES.get(file_path.get_extension(), "application/octet-stream")] header.append("Content-Length: %s" % file.get_len()) task.connect("completed", self, "_on_task_completed") task._setup( task.METHODS.UPLOAD_OBJECT, endpoint, header + _bearer, - "", + to_json({upsert = upsert}), file.get_buffer(file.get_len()) ) _current_task = task