Skip to content

Commit 4c37c14

Browse files
committed
fix storage bucket code
1 parent 1b1c724 commit 4c37c14

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

addons/supabase/Storage/storage.gd

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ var _header : PackedStringArray = ["Content-type: application/json"]
1717

1818
var _pooled_tasks : Array = []
1919

20+
var _tasks: Node = Node.new()
21+
var _buckets: Node = Node.new()
2022

2123
func _init(config : Dictionary) -> void:
2224
_config = config
2325
name = "Storage"
2426

27+
func _ready() -> void:
28+
add_child(_tasks)
29+
add_child(_buckets)
30+
2531
func list_buckets() -> StorageTask:
2632
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket"
2733
var task : StorageTask = StorageTask.new()
@@ -91,18 +97,18 @@ func delete_bucket(id : String) -> StorageTask:
9197

9298

9399
func from(id : String) -> StorageBucket:
94-
for bucket in get_children():
95-
if bucket is StorageBucket and bucket.id == id:
100+
for bucket in _buckets.get_children():
101+
if bucket.id == id:
96102
return bucket
97-
var storage_bucket : StorageBucket = StorageBucket.new(id, _config, get_parent().auth._bearer)
98-
add_child(storage_bucket)
103+
var storage_bucket : StorageBucket = StorageBucket.new(id, _config)
104+
_buckets.add_child(storage_bucket)
99105
return storage_bucket
100106

101107
# ---
102108

103109
func _process_task(task : StorageTask) -> void:
104110
var httprequest : HTTPRequest = HTTPRequest.new()
105-
add_child(httprequest)
111+
_tasks.add_child(httprequest)
106112
_pooled_tasks.append(task)
107113
task.completed.connect(_on_task_completed)
108114
task.push_request(httprequest)

addons/supabase/Storage/storage_bucket.gd

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const _rest_endpoint : String = "/storage/v1/object/"
4545

4646
var _config : Dictionary
4747
var _header : PackedStringArray = ["Content-Type: %s", "Content-Disposition: attachment"]
48-
var _bearer : PackedStringArray = ["Authorization: Bearer %s"]
4948

5049
var _pooled_tasks : Array = []
5150

@@ -63,23 +62,23 @@ var _response_code : int
6362
var id : String
6463

6564

66-
func _init(id : String , config : Dictionary, bearer : PackedStringArray) -> void:
65+
func _init(id : String , config : Dictionary) -> void:
6766
_config = config
6867
self.id = id
69-
_bearer = bearer
7068
name = "Bucket_"+id
7169
set_process_internal(false)
7270

7371

7472
func list(prefix : String = "", limit : int = 100, offset : int = 0, sort_by : Dictionary = {column = "name", order = "asc"} ) -> StorageTask:
75-
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "list/" + id
73+
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "/list/" + id
7674
var task : StorageTask = StorageTask.new()
7775
var header : PackedStringArray = [_header[0] % "application/json"]
7876
task._setup(
7977
task.METHODS.LIST_OBJECTS,
8078
endpoint,
81-
header + _bearer,
82-
JSON.stringify({prefix = prefix, limit = limit, offset = offset, sort_by = sort_by}))
79+
header + get_parent().get_parent().get_parent().auth.__get_session_header(),
80+
JSON.stringify({ prefix = prefix, limit = limit, offset = offset, sortBy = sort_by })
81+
)
8382
_process_task(task)
8483
return task
8584

@@ -94,15 +93,15 @@ func upload(object : String, file_path : String, upsert : bool = false) -> Stora
9493
task.complete({})
9594
return task
9695
var header : PackedStringArray = [_header[0] % MIME_TYPES.get(file_path.get_extension(), "application/octet-stream")]
97-
header.append("Content-Length: %s" % file.get_len())
96+
header.append("Content-Length: %s" % file.get_length())
9897
header.append("x-upsert: %s" % upsert)
9998
task.completed.connect(_on_task_completed)
10099
task._setup(
101100
task.METHODS.UPLOAD_OBJECT,
102101
endpoint,
103-
header + _bearer
102+
header + get_parent().get_parent().get_parent().auth.__get_session_header()
104103
)
105-
task.bytepayload = file.get_buffer(file.get_len())
104+
task.bytepayload = file.get_buffer(file.get_length())
106105

107106
_current_task = task
108107
set_process_internal(requesting_raw)
@@ -127,7 +126,7 @@ func update(bucket_path : String, file_path : String) -> StorageTask:
127126
task._setup(
128127
task.METHODS.UPDATE_OBJECT,
129128
endpoint,
130-
header + _bearer
129+
header + get_parent().get_parent().get_parent().auth.__get_session_header()
131130
)
132131
task.bytepayload = file.get_buffer(file.get_len())
133132

@@ -144,7 +143,7 @@ func move(source_path : String, destination_path : String) -> StorageTask:
144143
task._setup(
145144
task.METHODS.MOVE_OBJECT,
146145
endpoint,
147-
header + _bearer,
146+
header + get_parent().get_parent().get_parent().auth.__get_session_header(),
148147
JSON.stringify({bucketId = id, sourceKey = source_path, destinationKey = destination_path}))
149148
_process_task(task)
150149
return task
@@ -157,7 +156,7 @@ func create_signed_url(object : String, expires_in : int = 60000) -> StorageTask
157156
task._setup(
158157
task.METHODS.CREATE_SIGNED_URL,
159158
endpoint,
160-
header + _bearer,
159+
header + get_parent().get_parent().get_parent().auth.__get_session_header(),
161160
JSON.stringify({expiresIn = expires_in})
162161
)
163162
_process_task(task)
@@ -172,7 +171,7 @@ func download(object : String, to_path : String = "", private : bool = false) ->
172171
task._setup(
173172
task.METHODS.DOWNLOAD,
174173
endpoint,
175-
header + _bearer
174+
header + get_parent().get_parent().get_parent().auth.__get_session_header()
176175
)
177176
_process_task(task, {download_file = to_path})
178177
return task
@@ -183,7 +182,7 @@ func download(object : String, to_path : String = "", private : bool = false) ->
183182
task._setup(
184183
task.METHODS.DOWNLOAD,
185184
endpoint,
186-
header + _bearer
185+
header + get_parent().get_parent().get_parent().auth.__get_session_header()
187186
)
188187
_process_task(task, {download_file = to_path})
189188
return task
@@ -200,7 +199,7 @@ func remove(objects : PackedStringArray) -> StorageTask:
200199
task._setup(
201200
task.METHODS.REMOVE,
202201
endpoint,
203-
header + _bearer,
202+
header + get_parent().get_parent().get_parent().auth.__get_session_header(),
204203
JSON.stringify({prefixes = objects}) if objects.size() > 1 else "" )
205204
_process_task(task)
206205
return task
@@ -225,7 +224,7 @@ func _internal_process(_delta : float) -> void:
225224
_http_client.poll()
226225

227226
HTTPClient.STATUS_CONNECTED:
228-
var err : int = _http_client.request_raw(task._method, task._endpoint.replace(_config.supabaseUrl, ""), task._headers, task._bytepayload)
227+
var err : int = _http_client.request_raw(task._method, task._endpoint.replace(_config.supabaseUrl, ""), task._headers, task.bytepayload)
229228
if err :
230229
task.error = SupabaseStorageError.new({statusCode = HTTPRequest.RESULT_CONNECTION_ERROR})
231230
_on_task_completed(task)
@@ -280,10 +279,9 @@ func _process_task(task : StorageTask, _params : Dictionary = {}) -> void:
280279

281280
# .............. HTTPRequest completed
282281
func _on_task_completed(task : StorageTask) -> void:
283-
if task._handler : task._handler.queue_free()
284282
if requesting_raw:
285283
_clear_raw_request()
286-
if task.data!=null and not task.data.empty():
284+
if task.data!=null and not task.data.is_empty():
287285
match task._code:
288286
task.METHODS.LIST_OBJECTS: emit_signal("listed_objects", task.data)
289287
task.METHODS.UPLOAD_OBJECT: emit_signal("uploaded_object", task.data)

addons/supabase/Storage/storage_error.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ class_name SupabaseStorageError
55
func _init(dictionary : Dictionary = {}) -> void:
66
_error = dictionary
77
if not _error.is_empty():
8-
code = _error.get("statusCode", "empty") as String
8+
code = _error.get("statusCode", "empty")
99
details = _error.get("error", "empty")
1010
message = _error.get("message", "empty")

addons/supabase/Storage/storage_task.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum METHODS {
2121
}
2222

2323
var bytepayload : PackedByteArray
24+
var __json: JSON = JSON.new()
2425

2526
func match_code(code : int) -> int:
2627
match code:
@@ -33,13 +34,16 @@ func match_code(code : int) -> int:
3334
_: return HTTPClient.METHOD_GET
3435

3536
func _on_task_completed(result : int, response_code : int, headers : PackedStringArray, body : PackedByteArray, handler: HTTPRequest) -> void:
36-
var result_body = JSON.parse_string(body.get_string_from_utf8())
37+
var err: int = __json.parse(body.get_string_from_utf8())
38+
var result_body = __json.data if err == OK else {}
3739
if response_code in [200, 201, 204]:
3840
if _code == METHODS.DOWNLOAD:
3941
complete(body)
4042
else:
4143
complete(result_body)
4244
else:
45+
if result_body.is_empty():
46+
result_body.statusCode = str(response_code)
4347
var supabase_error : SupabaseStorageError = SupabaseStorageError.new(result_body)
4448
complete(null, supabase_error)
4549
if handler != null: handler.queue_free()

0 commit comments

Comments
 (0)