Skip to content

Commit 7ae1612

Browse files
Add get_download_url method for files in Cloud Storage
1 parent 022c8a1 commit 7ae1612

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

demo/scenes/storage.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ func _on_list_files_pressed() -> void:
4848
func _on_file_dialog_file_selected(path: String) -> void:
4949
print(path)
5050
selected_image_path = path
51+
52+
53+
func _on_download_url_pressed() -> void:
54+
Firebase.storage.get_download_url(cloud_storage_path.text)

demo/scenes/storage.tscn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ theme_override_styles/pressed = SubResource("StyleBoxFlat_taf4q")
9191
theme_override_styles/normal = SubResource("StyleBoxFlat_ykan5")
9292
text = "Upload File"
9393

94+
[node name="download_url" type="Button" parent="MarginContainer/VBoxContainer"]
95+
custom_minimum_size = Vector2(120, 60)
96+
layout_mode = 2
97+
theme_override_font_sizes/font_size = 32
98+
theme_override_styles/focus = SubResource("StyleBoxFlat_nnb4j")
99+
theme_override_styles/hover = SubResource("StyleBoxFlat_ykan5")
100+
theme_override_styles/pressed = SubResource("StyleBoxFlat_taf4q")
101+
theme_override_styles/normal = SubResource("StyleBoxFlat_ykan5")
102+
text = "Get Download URL"
103+
94104
[node name="download_file" type="Button" parent="MarginContainer/VBoxContainer"]
95105
custom_minimum_size = Vector2(120, 60)
96106
layout_mode = 2
@@ -147,6 +157,7 @@ use_native_dialog = true
147157

148158
[connection signal="pressed" from="MarginContainer/VBoxContainer/get_image_path" to="." method="_on_get_image_path_pressed"]
149159
[connection signal="pressed" from="MarginContainer/VBoxContainer/upload_file" to="." method="_on_upload_file_pressed"]
160+
[connection signal="pressed" from="MarginContainer/VBoxContainer/download_url" to="." method="_on_download_url_pressed"]
150161
[connection signal="pressed" from="MarginContainer/VBoxContainer/download_file" to="." method="_on_download_file_pressed"]
151162
[connection signal="pressed" from="MarginContainer/VBoxContainer/get_metadata" to="." method="_on_get_metadata_pressed"]
152163
[connection signal="pressed" from="MarginContainer/VBoxContainer/delete_file" to="." method="_on_delete_file_pressed"]

docs/storage.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ Firebase.storage.upload_file("uploads/image.png", "user://images/image.png")
3737

3838
---
3939

40+
{: .text-green-100 }
41+
### get_download_url(path: String)
42+
43+
Get the download URL for a file in Firebase Storage.
44+
45+
**Emits:** `download_task_completed`
46+
47+
```gdscript
48+
Firebase.storage.get_download_url("uploads/image.png")
49+
```
50+
51+
---
52+
4053
{: .text-green-100 }
4154
### download_file(path: String, destinationPath: String)
4255

firebase/export_scripts_template/modules/Storage.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func download_file(path: String, destinationPath: String) -> void:
2323
if _plugin_singleton:
2424
_plugin_singleton.storageDownloadFile(path, destinationPath)
2525

26+
func get_download_url(path: String) -> void:
27+
if _plugin_singleton:
28+
_plugin_singleton.storageGetDownloadUrl(path)
29+
2630
func get_metadata(path: String) -> void:
2731
if _plugin_singleton:
2832
_plugin_singleton.storageGetMetadata(path)

firebase/src/main/java/org/godotengine/plugin/firebase/CloudStorage.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,29 @@ class CloudStorage(private val plugin: FirebasePlugin) {
6868
ref.getFile(localFile)
6969
.addOnSuccessListener {
7070
Log.d(TAG, "File downloaded successfully to $destinationPath")
71-
plugin.emitGodotSignal("storage_download_task_completed", createResultDict(false, destinationPath))
71+
plugin.emitGodotSignal("storage_download_task_completed", createResultDict(true, destinationPath))
7272
}
7373
.addOnFailureListener { e ->
7474
Log.e(TAG, "Download failed: $path")
7575
plugin.emitGodotSignal("storage_download_task_completed", createResultDict(false, destinationPath, e.message))
7676
}
7777
}
7878

79+
fun getDownloadUrl(path: String) {
80+
val ref = storageRef.child(path)
81+
ref.downloadUrl
82+
.addOnSuccessListener { uri ->
83+
val result = Dictionary()
84+
result["url"] = uri.toString()
85+
Log.d(TAG, "Download URL retrieved for $path")
86+
plugin.emitGodotSignal("storage_download_task_completed", createResultDict(true, path, data = result))
87+
}
88+
.addOnFailureListener { e ->
89+
Log.e(TAG, "Failed to get download URL for $path", e)
90+
plugin.emitGodotSignal("storage_download_task_completed", createResultDict(false, path, e.message))
91+
}
92+
}
93+
7994
fun getMetadata(path: String) {
8095
val ref = storageRef.child(path)
8196

firebase/src/main/java/org/godotengine/plugin/firebase/FirebasePlugin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class FirebasePlugin(godot: Godot) : GodotPlugin(godot) {
111111
@UsedByGodot
112112
fun storageUploadFile(path: String, localFilePath: String) = storage.uploadFile(path, localFilePath)
113113

114+
@UsedByGodot
115+
fun storageGetDownloadUrl(path: String) = storage.getDownloadUrl(path)
116+
114117
@UsedByGodot
115118
fun storageDownloadFile(path: String, destinationPath: String) = storage.downloadFile(path, destinationPath)
116119

0 commit comments

Comments
 (0)