diff --git a/app/src/main/java/fr/free/nrw/commons/explore/media/MediaConverter.kt b/app/src/main/java/fr/free/nrw/commons/explore/media/MediaConverter.kt index 765086c0d..de084ba50 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/media/MediaConverter.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/media/MediaConverter.kt @@ -23,7 +23,7 @@ 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() @@ -31,8 +31,8 @@ class MediaConverter 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()), diff --git a/app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ImageInfo.java b/app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ImageInfo.java deleted file mode 100644 index 2e1349ae9..000000000 --- a/app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ImageInfo.java +++ /dev/null @@ -1,121 +0,0 @@ -package fr.free.nrw.commons.wikidata.model.gallery; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -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 - */ - -public class ImageInfo implements Serializable { - private int size; - private int width; - private int height; - @Nullable private String source; - @SerializedName("thumburl") @Nullable private String thumbUrl; - @SerializedName("thumbwidth") private int thumbWidth; - @SerializedName("thumbheight") private int thumbHeight; - @SerializedName("url") @Nullable private String originalUrl; - @SerializedName("descriptionurl") @Nullable private String descriptionUrl; - @SerializedName("descriptionshorturl") @Nullable private String descriptionShortUrl; - @SerializedName("mime") @Nullable private String mimeType; - @SerializedName("extmetadata")@Nullable private ExtMetadata metadata; - @Nullable private String user; - @Nullable private String timestamp; - - /** - * Query width, default width parameter of the API query in pixels. - */ - final private static int QUERY_WIDTH = 640; - - /** - * Threshold height, the minimum height of the image in pixels. - */ - final private static int THRESHOLD_HEIGHT = 220; - - @NonNull - public String getSource() { - return StringUtils.defaultString(source); - } - - public void setSource(@Nullable String source) { - this.source = source; - } - - public int getSize() { - return size; - } - - public int getWidth() { - return width; - } - - public int getHeight() { - return height; - } - - /** - * Get the thumbnail width. - * @return - */ - public int getThumbWidth() { return thumbWidth; } - - /** - * Get the thumbnail height. - * @return - */ - public int getThumbHeight() { return thumbHeight; } - - @NonNull public String getMimeType() { - return StringUtils.defaultString(mimeType, "*/*"); - } - - @NonNull public String getThumbUrl() { - updateThumbUrl(); - return StringUtils.defaultString(thumbUrl); - } - - @NonNull public String getOriginalUrl() { - return StringUtils.defaultString(originalUrl); - } - - @NonNull public String getUser() { - return StringUtils.defaultString(user); - } - - @NonNull public String getTimestamp() { - return StringUtils.defaultString(timestamp); - } - - @Nullable public ExtMetadata getMetadata() { - return 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 void 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. - final int finalWidth = (int)(THRESHOLD_HEIGHT * getThumbWidth() * 1.0 / getThumbHeight()); - thumbHeight = THRESHOLD_HEIGHT; - thumbWidth = finalWidth; - final String toReplace = "/" + QUERY_WIDTH + "px"; - final int position = thumbUrl.lastIndexOf(toReplace); - thumbUrl = (new StringBuilder(thumbUrl)).replace(position, position + toReplace.length(), "/" + thumbWidth + "px").toString(); - } - } - } - -} diff --git a/app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ImageInfo.kt b/app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ImageInfo.kt new file mode 100644 index 000000000..492e2e1f8 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ImageInfo.kt @@ -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 + } +} diff --git a/app/src/test/kotlin/fr/free/nrw/commons/explore/media/MediaConverterTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/explore/media/MediaConverterTest.kt index e1e1b2ed7..8a3022a35 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/explore/media/MediaConverterTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/explore/media/MediaConverterTest.kt @@ -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") }