Last active
May 16, 2021 22:01
-
-
Save leviyehonatan/3053957f6040343b391aab78290d550e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun syncResults(progress: (message: String) -> Unit) { | |
val directory = resultsDirectory | |
val files = directory.listFiles() | |
if (files.size == 0) return | |
progress("starting results sync") | |
files.map { | |
ioScope.launch { | |
progress("uploading planogram $it") | |
val jsonString = it.readText() | |
val planogramResultsJSON = | |
gson.fromJson(jsonString, PlanogramResults::class.java) | |
// upload bitmaps | |
val uploadedBitmaps = | |
planogramResultsJSON.bitmaps.map { File(it) }.map { bitmapFile -> | |
ioScope.async { | |
getSignedUrl( | |
planogramResultsJSON.planogram.planogramId, | |
bitmapFile | |
) | |
.let { | |
progress("uploading image $bitmapFile") | |
putImage(bitmapFile, it.writeUrl) | |
it.readUrl | |
} | |
} | |
}.map { it.await() } | |
progress("finished uploading photos: $uploadedBitmaps") | |
val bitmapsToDelete = planogramResultsJSON.bitmaps | |
planogramResultsJSON.bitmaps = uploadedBitmaps | |
postPlanogram(planogramResultsJSON) | |
bitmapsToDelete.map { File(it) }.apply { | |
if (!it.delete()) println("could not delete $it") | |
} | |
println("deleting planogram $it") | |
if (!it.delete()) println("could not delete $it") | |
} | |
progress("finishing upload results $it") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment