mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Convert ImageInfo to kotlin
This commit is contained in:
		
							parent
							
								
									fd18f6a67a
								
							
						
					
					
						commit
						164dd3df34
					
				
					 4 changed files with 142 additions and 134 deletions
				
			
		|  | @ -23,7 +23,7 @@ class MediaConverter | ||||||
|             entity: Entities.Entity, |             entity: Entities.Entity, | ||||||
|             imageInfo: ImageInfo, |             imageInfo: ImageInfo, | ||||||
|         ): Media { |         ): Media { | ||||||
|             val metadata = imageInfo.metadata |             val metadata = imageInfo.getMetadata() | ||||||
|             requireNotNull(metadata) { "No metadata" } |             requireNotNull(metadata) { "No metadata" } | ||||||
|             // Stores mapping of title attribute to hidden attribute of each category |             // Stores mapping of title attribute to hidden attribute of each category | ||||||
|             val myMap = mutableMapOf<String, Boolean>() |             val myMap = mutableMapOf<String, Boolean>() | ||||||
|  | @ -31,8 +31,8 @@ class MediaConverter | ||||||
| 
 | 
 | ||||||
|             return Media( |             return Media( | ||||||
|                 page.pageId().toString(), |                 page.pageId().toString(), | ||||||
|                 imageInfo.thumbUrl.takeIf { it.isNotBlank() } ?: imageInfo.originalUrl, |                 imageInfo.getThumbUrl().takeIf { it.isNotBlank() } ?: imageInfo.getOriginalUrl(), | ||||||
|                 imageInfo.originalUrl, |                 imageInfo.getOriginalUrl(), | ||||||
|                 page.title(), |                 page.title(), | ||||||
|                 metadata.imageDescription(), |                 metadata.imageDescription(), | ||||||
|                 safeParseDate(metadata.dateTime()), |                 safeParseDate(metadata.dateTime()), | ||||||
|  |  | ||||||
|  | @ -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(); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
|  | @ -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 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -42,22 +42,22 @@ class MediaConverterTest { | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     fun testConvertIfThumbUrlBlank() { |     fun testConvertIfThumbUrlBlank() { | ||||||
|         Mockito.`when`(imageInfo.metadata).thenReturn(metadata) |         Mockito.`when`(imageInfo.getMetadata()).thenReturn(metadata) | ||||||
|         Mockito.`when`(imageInfo.thumbUrl).thenReturn("") |         Mockito.`when`(imageInfo.getThumbUrl()).thenReturn("") | ||||||
|         Mockito.`when`(imageInfo.originalUrl).thenReturn("originalUrl") |         Mockito.`when`(imageInfo.getOriginalUrl()).thenReturn("originalUrl") | ||||||
|         Mockito.`when`(imageInfo.metadata?.licenseUrl()).thenReturn("licenseUrl") |         Mockito.`when`(imageInfo.getMetadata()?.licenseUrl()).thenReturn("licenseUrl") | ||||||
|         Mockito.`when`(imageInfo.metadata?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss") |         Mockito.`when`(imageInfo.getMetadata()?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss") | ||||||
|         media = mediaConverter.convert(page, entity, imageInfo) |         media = mediaConverter.convert(page, entity, imageInfo) | ||||||
|         assertEquals(media.thumbUrl, media.imageUrl, "originalUrl") |         assertEquals(media.thumbUrl, media.imageUrl, "originalUrl") | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     fun testConvertIfThumbUrlNotBlank() { |     fun testConvertIfThumbUrlNotBlank() { | ||||||
|         Mockito.`when`(imageInfo.metadata).thenReturn(metadata) |         Mockito.`when`(imageInfo.getMetadata()).thenReturn(metadata) | ||||||
|         Mockito.`when`(imageInfo.thumbUrl).thenReturn("thumbUrl") |         Mockito.`when`(imageInfo.getThumbUrl()).thenReturn("thumbUrl") | ||||||
|         Mockito.`when`(imageInfo.originalUrl).thenReturn("originalUrl") |         Mockito.`when`(imageInfo.getOriginalUrl()).thenReturn("originalUrl") | ||||||
|         Mockito.`when`(imageInfo.metadata?.licenseUrl()).thenReturn("licenseUrl") |         Mockito.`when`(imageInfo.getMetadata()?.licenseUrl()).thenReturn("licenseUrl") | ||||||
|         Mockito.`when`(imageInfo.metadata?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss") |         Mockito.`when`(imageInfo.getMetadata()?.dateTime()).thenReturn("yyyy-MM-dd HH:mm:ss") | ||||||
|         media = mediaConverter.convert(page, entity, imageInfo) |         media = mediaConverter.convert(page, entity, imageInfo) | ||||||
|         assertEquals(media.thumbUrl, "thumbUrl") |         assertEquals(media.thumbUrl, "thumbUrl") | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Paul Hawke
						Paul Hawke