-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for JSON->proto conversion.
This is the basis for converting from GAE JSON structures to proto "v2" (that is, the proto structures defined as part of oppia-proto-api). "v1" conversion (that is, the proto structures used in Oppia Android's data layer) will happen at a higher level in a downstream PR.
- Loading branch information
1 parent
e250f69
commit 85f2be9
Showing
7 changed files
with
3,017 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
55 changes: 55 additions & 0 deletions
55
scripts/src/java/org/oppia/android/scripts/gae/proto/ImageDownloader.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.oppia.android.scripts.gae.proto | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.async | ||
import org.oppia.android.scripts.gae.gcs.GcsService | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class ImageDownloader( | ||
private val gcsService: GcsService, | ||
private val coroutineDispatcher: CoroutineDispatcher | ||
) { | ||
private val imageLengths = ConcurrentHashMap<ImageId, Long>() | ||
|
||
fun <T> retrieveImageLengthAsync( | ||
imageContainerType: GcsService.ImageContainerType, | ||
imageType: GcsService.ImageType, | ||
entityId: String, | ||
filename: String, | ||
transform: (Int) -> T | ||
): Deferred<T> { | ||
return CoroutineScope(coroutineDispatcher).async { | ||
val imageId = ImageId(imageContainerType, entityId, imageType, filename) | ||
val length = imageLengths.getOrPut(imageId) { | ||
gcsService.fetchImageContentLengthAsync( | ||
imageContainerType, imageType, entityId, filename | ||
).await() ?: -1 | ||
} | ||
return@async transform(length.toInt()) | ||
} | ||
} | ||
|
||
fun retrieveImageContentAsync( | ||
imageContainerType: GcsService.ImageContainerType, | ||
imageType: GcsService.ImageType, | ||
entityId: String, | ||
filename: String | ||
): Deferred<ByteArray?> = | ||
gcsService.fetchImageContentDataAsync(imageContainerType, imageType, entityId, filename) | ||
|
||
fun computeImageUrl( | ||
imageContainerType: GcsService.ImageContainerType, | ||
imageType: GcsService.ImageType, | ||
entityId: String, | ||
filename: String | ||
): String = gcsService.computeImageUrl(imageContainerType, imageType, entityId, filename) | ||
|
||
private data class ImageId( | ||
val imageContainerType: GcsService.ImageContainerType, | ||
val entityId: String, | ||
val imageType: GcsService.ImageType, | ||
val filename: String | ||
) | ||
} |
Oops, something went wrong.