From 3ecee11e11b0be5e18edf42fa8dc3aa642e38178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A4in=C3=B6=20M=C3=A4kel=C3=A4?= Date: Wed, 5 Aug 2020 21:49:23 +0300 Subject: [PATCH] Resolved #2893 - Use list_folder/continue to get the whole folder (#2930) listing when downloading a file --- .../com/unciv/ui/mapeditor/MapDownloadPopup.kt | 2 +- .../unciv/ui/worldscreen/mainmenu/DropBox.kt | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt index 83d94e2a..ce7bcbba 100644 --- a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt @@ -49,7 +49,7 @@ class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) { val folderList = DropBox.getFolderList("/Maps") Gdx.app.postRunnable { scrollableMapTable.apply { defaults().pad(10f) } - for (downloadableMap in folderList.entries) { + for (downloadableMap in folderList) { val downloadMapButton = downloadableMap.name.toTextButton() listOfMaps.add(downloadMapButton) downloadMapButton.onClick { diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt index 9f6d6b0f..632c3230 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt @@ -45,10 +45,22 @@ object DropBox { } } - fun getFolderList(folder:String):FolderList{ + fun getFolderList(folder:String): ArrayList { + val folderList = ArrayList() + // The DropBox API returns only partial file listings from one request. list_folder and + // list_folder/continue return similar responses, but list_folder/continue requires a cursor + // instead of the path. val response = dropboxApi("https://api.dropboxapi.com/2/files/list_folder", "{\"path\":\"$folder\"}","application/json") - return GameSaver.json().fromJson(FolderList::class.java,response) + var currentFolderListChunk = GameSaver.json().fromJson(FolderList::class.java, response) + folderList.addAll(currentFolderListChunk.entries) + while (currentFolderListChunk.has_more) { + val continuationResponse = dropboxApi("https://api.dropboxapi.com/2/files/list_folder/continue", + "{\"cursor\":\"${currentFolderListChunk.cursor}\"}","application/json") + currentFolderListChunk = GameSaver.json().fromJson(FolderList::class.java, continuationResponse) + folderList.addAll(currentFolderListChunk.entries) + } + return folderList } fun downloadFile(fileName:String): InputStream { @@ -84,6 +96,8 @@ object DropBox { class FolderList{ var entries = ArrayList() + var cursor = "" + var has_more = false } class FolderListEntry{