Skip to content

Commit

Permalink
Merge pull request #30 from fenix-hub/dev
Browse files Browse the repository at this point in the history
support upsert for storage.upload()
  • Loading branch information
fenix-hub authored Jul 26, 2021
2 parents 06028e8 + b1cb64c commit f06f699
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
16 changes: 0 additions & 16 deletions .gitignore

This file was deleted.

1 change: 0 additions & 1 deletion addons/supabase/Auth/auth.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions addons/supabase/Database/database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
31 changes: 19 additions & 12 deletions addons/supabase/Database/query.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var query_struct : Dictionary = {
}

var query : String = ""
var raw_query : String = ""
var header : PoolStringArray = []
var request : int
var body : String = ""
Expand Down Expand Up @@ -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


Expand Down
7 changes: 1 addition & 6 deletions addons/supabase/Storage/storage.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions addons/supabase/Storage/storage_bucket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -84,25 +84,24 @@ 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()
var endpoint : String = _config.supabaseUrl + _rest_endpoint + id + "/" + object
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
Expand Down

0 comments on commit f06f699

Please sign in to comment.