Skip to content

Commit

Permalink
feat: format option for webp support supabase/storage-js#142
Browse files Browse the repository at this point in the history
  • Loading branch information
fenix-hub committed Apr 10, 2023
1 parent ac34e9a commit 90a0572
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
65 changes: 32 additions & 33 deletions addons/supabase/Storage/storage_bucket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,25 @@ func move(source_path : String, destination_path : String) -> StorageTask:
return task


func download(object : String, to_path : String = "", private : bool = false, options: Dictionary = {
transform = { height = 100, width = 100, format = "origin", resize = "cover", quality = 80 }
} ) -> StorageTask:
var endpoint : String = _config.supabaseUrl + _rest_endpoint + \
("authenticated/" if private else "public/") + id + "/" + object + \
_get_transform_query(options.get("transform", {}))
var task : StorageTask = StorageTask.new()
var header : PackedStringArray = [_header[0] % "application/json"]
task._setup(
task.METHODS.DOWNLOAD,
endpoint,
header + get_parent().get_parent().get_parent().auth.__get_session_header()
)
_process_task(task, {download_file = to_path})
return task


func create_signed_url(object : String, expires_in : int = 60000, options: Dictionary = {
download = "file", transform = { format = "origin" , quality = 80 , resize = "cover" , height = 100, width = 100 }
download = true, transform = { format = "origin" , quality = 80 , resize = "cover" , height = 100, width = 100 }
}) -> StorageTask:
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "sign/" + id + "/" + object
var task : StorageTask = StorageTask.new()
Expand All @@ -160,46 +177,19 @@ func create_signed_url(object : String, expires_in : int = 60000, options: Dicti
header + get_parent().get_parent().get_parent().auth.__get_session_header(),
JSON.stringify({expiresIn = expires_in, transform = options.get("transform", {}) })
)
task.set_meta("object", object)
task.set_meta("options", options)
task.set_meta("base_url", _config.supabaseUrl + _rest_endpoint.replace("/object/", ""))
_process_task(task)
return task


func download(object : String, to_path : String = "", private : bool = false) -> StorageTask:
if not private:
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "public/" + id + "/" + object
var task : StorageTask = StorageTask.new()
var header : PackedStringArray = [_header[0] % "application/json"]
task._setup(
task.METHODS.DOWNLOAD,
endpoint,
header + get_parent().get_parent().get_parent().auth.__get_session_header()
)
_process_task(task, {download_file = to_path})
return task
else:
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "authenticated/" + id + "/" + object
var task : StorageTask = StorageTask.new()
var header : PackedStringArray = [_header[0] % "application/json"]
task._setup(
task.METHODS.DOWNLOAD,
endpoint,
header + get_parent().get_parent().get_parent().auth.__get_session_header()
)
_process_task(task, {download_file = to_path})
return task


func get_public_url(object: String, transform: Dictionary = {

func get_public_url(object: String, options: Dictionary = { transform = {
height = 100, width = 100, format = "origin", resize = "cover", quality = 80
} ) -> String:
} }) -> String:
var url: String = _config.supabaseUrl + _rest_endpoint + "public/" + id + "/" + object
if not transform.keys().is_empty():
url += "?"
for key in transform.keys():
url += "&%s=%s" % [key, transform.get(key)]
return url
return url + _get_transform_query(options.get("transform", {}))


func remove(objects : PackedStringArray) -> StorageTask:
Expand All @@ -215,6 +205,15 @@ func remove(objects : PackedStringArray) -> StorageTask:
return task



func _get_transform_query(transform: Dictionary) -> String:
var query: String = ""
if not transform.keys().is_empty():
query += "?"
for key in transform.keys():
query += "&%s=%s" % [key, transform.get(key)]
return query

func _notification(what : int) -> void:
if what == NOTIFICATION_INTERNAL_PROCESS:
_internal_process(get_process_delta_time())
Expand Down
5 changes: 4 additions & 1 deletion addons/supabase/Storage/storage_task.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func _on_task_completed(result : int, response_code : int, headers : PackedStrin
complete(body)
else:
if _code == METHODS.CREATE_SIGNED_URL:
result_body.signedURL = get_meta("base_url") + result_body.signedURL + "&download=%s" % get_meta("options").get("download")
result_body.signedURL = get_meta("base_url") + result_body.signedURL
var download = get_meta("options").get("download")
if download:
result_body.signedURL += "&download=%s" % download if (download is String) else get_meta("object")
complete(result_body)
else:
if result_body.is_empty():
Expand Down

0 comments on commit 90a0572

Please sign in to comment.