Skip to content

Convert wikidata/mwapi to kotlin (part 3) #6004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert ImageInfo to kotlin
  • Loading branch information
psh committed Dec 6, 2024
commit 164dd3df347303477a166572f89e32ccec7dcff3
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class MediaConverter
entity: Entities.Entity,
imageInfo: ImageInfo,
): Media {
val metadata = imageInfo.metadata
val metadata = imageInfo.getMetadata()
requireNotNull(metadata) { "No metadata" }
// Stores mapping of title attribute to hidden attribute of each category
val myMap = mutableMapOf<String, Boolean>()
page.categories()?.forEach { myMap[it.title()] = (it.hidden()) }

return Media(
page.pageId().toString(),
imageInfo.thumbUrl.takeIf { it.isNotBlank() } ?: imageInfo.originalUrl,
imageInfo.originalUrl,
imageInfo.getThumbUrl().takeIf { it.isNotBlank() } ?: imageInfo.getOriginalUrl(),
imageInfo.getOriginalUrl(),
page.title(),
metadata.imageDescription(),
safeParseDate(metadata.dateTime()),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package fr.free.nrw.commons.wikidata.model.gallery

import com.google.gson.annotations.SerializedName
import org.apache.commons.lang3.StringUtils
import java.io.Serializable

/**
* Gson POJO for a standard image info object as returned by the API ImageInfo module
*/
open class ImageInfo : Serializable {
private val size = 0
private val width = 0
private val height = 0
private var source: String? = null

@SerializedName("thumburl")
private var thumbUrl: String? = null

@SerializedName("thumbwidth")
private var thumbWidth = 0

@SerializedName("thumbheight")
private var thumbHeight = 0

@SerializedName("url")
private val originalUrl: String? = null

@SerializedName("descriptionurl")
private val descriptionUrl: String? = null

@SerializedName("descriptionshorturl")
private val descriptionShortUrl: String? = null

@SerializedName("mime")
private val mimeType: String? = null

@SerializedName("extmetadata")
private val metadata: ExtMetadata? = null
private val user: String? = null
private val timestamp: String? = null

fun getSource(): String {
return source ?: ""
}

fun setSource(source: String?) {
this.source = source
}

fun getSize(): Int {
return size
}

fun getWidth(): Int {
return width
}

fun getHeight(): Int {
return height
}

fun getThumbWidth(): Int {
return thumbWidth
}

fun getThumbHeight(): Int {
return thumbHeight
}

fun getMimeType(): String {
return mimeType ?: "*/*"
}

fun getThumbUrl(): String {
updateThumbUrl()
return thumbUrl ?: ""
}

fun getOriginalUrl(): String {
return originalUrl ?: ""
}

fun getUser(): String {
return user ?: ""
}

fun getTimestamp(): String {
return timestamp ?: ""
}

fun getMetadata(): ExtMetadata? = metadata

/**
* Updates the ThumbUrl if image dimensions are not sufficient. Specifically, in panoramic
* images the height retrieved is less than required due to large width to height ratio, so we
* update the thumb url keeping a minimum height threshold.
*/
private fun updateThumbUrl() {
// If thumbHeight retrieved from API is less than THRESHOLD_HEIGHT
if (getThumbHeight() < THRESHOLD_HEIGHT) {
// If thumbWidthRetrieved is same as queried width ( If not tells us that the image has no larger dimensions. )
if (getThumbWidth() == QUERY_WIDTH) {
// Calculate new width depending on the aspect ratio.
val finalWidth = (THRESHOLD_HEIGHT * getThumbWidth() * 1.0
/ getThumbHeight()).toInt()
thumbHeight = THRESHOLD_HEIGHT
thumbWidth = finalWidth
val toReplace = "/" + QUERY_WIDTH + "px"
val position = thumbUrl!!.lastIndexOf(toReplace)
thumbUrl = (StringBuilder(thumbUrl ?: "")).replace(
position,
position + toReplace.length, "/" + thumbWidth + "px"
).toString()
}
}
}

companion object {
/**
* Query width, default width parameter of the API query in pixels.
*/
private const val QUERY_WIDTH = 640

/**
* Threshold height, the minimum height of the image in pixels.
*/
private const val THRESHOLD_HEIGHT = 220
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ class MediaConverterTest {

@Test
fun testConvertIfThumbUrlBlank() {
Mockito.`when`(imageInfo.metadata).thenReturn(metadata)
Mockito.`when`(imageInfo.thumbUrl).thenReturn("")
Mockito.`when`(imageInfo.originalUrl).thenReturn("originalUrl")
Mockito.`when`(imageInfo.metadata?.licenseUrl()).thenReturn("licenseUrl")
Mockito.`when`(imageInfo.metadata?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss")
Mockito.`when`(imageInfo.getMetadata()).thenReturn(metadata)
Mockito.`when`(imageInfo.getThumbUrl()).thenReturn("")
Mockito.`when`(imageInfo.getOriginalUrl()).thenReturn("originalUrl")
Mockito.`when`(imageInfo.getMetadata()?.licenseUrl()).thenReturn("licenseUrl")
Mockito.`when`(imageInfo.getMetadata()?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss")
media = mediaConverter.convert(page, entity, imageInfo)
assertEquals(media.thumbUrl, media.imageUrl, "originalUrl")
}

@Test
fun testConvertIfThumbUrlNotBlank() {
Mockito.`when`(imageInfo.metadata).thenReturn(metadata)
Mockito.`when`(imageInfo.thumbUrl).thenReturn("thumbUrl")
Mockito.`when`(imageInfo.originalUrl).thenReturn("originalUrl")
Mockito.`when`(imageInfo.metadata?.licenseUrl()).thenReturn("licenseUrl")
Mockito.`when`(imageInfo.metadata?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss")
Mockito.`when`(imageInfo.getMetadata()).thenReturn(metadata)
Mockito.`when`(imageInfo.getThumbUrl()).thenReturn("thumbUrl")
Mockito.`when`(imageInfo.getOriginalUrl()).thenReturn("originalUrl")
Mockito.`when`(imageInfo.getMetadata()?.licenseUrl()).thenReturn("licenseUrl")
Mockito.`when`(imageInfo.getMetadata()?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss")
media = mediaConverter.convert(page, entity, imageInfo)
assertEquals(media.thumbUrl, "thumbUrl")
}
Expand Down