diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.java deleted file mode 100644 index b5c7812d4004..000000000000 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.wordpress.android.ui.reader.utils; - -import android.net.Uri; -import android.text.TextUtils; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.json.JSONException; -import org.json.JSONObject; -import org.wordpress.android.util.AppLog; -import org.wordpress.android.util.JSONUtils; -import org.wordpress.android.util.UrlUtils; - -import java.util.HashMap; -import java.util.Iterator; - -/** - * hash map of sizes of attachments in a reader post - created from the json "attachments" section - * of the post endpoints - */ -public class ImageSizeMap extends HashMap { - private static final String EMPTY_JSON = "{}"; - - public ImageSizeMap(@NonNull String postContent, String jsonString) { - if (TextUtils.isEmpty(jsonString) || jsonString.equals(EMPTY_JSON)) { - return; - } - - try { - JSONObject json = new JSONObject(jsonString); - Iterator it = json.keys(); - if (!it.hasNext()) { - return; - } - - while (it.hasNext()) { - JSONObject jsonAttach = json.optJSONObject(it.next()); - if (jsonAttach != null && JSONUtils.getString(jsonAttach, "mime_type").startsWith("image")) { - String normUrl = - UrlUtils.normalizeUrl(UrlUtils.removeQuery(JSONUtils.getString(jsonAttach, "URL"))); - - // make sure this image actually appears in the post content - it's possible for - // an image to be in the attachments but not in the post itself - String path = Uri.parse(normUrl).getPath(); - if (postContent.contains(path)) { - int width = jsonAttach.optInt("width"); - int height = jsonAttach.optInt("height"); - - // chech if data-orig-size is present and use it - String originalSize = jsonAttach.optString("data-orig-size", null); - if (originalSize != null) { - String[] sizes = originalSize.split(","); - if (sizes != null && sizes.length == 2) { - width = Integer.parseInt(sizes[0]); - height = Integer.parseInt(sizes[1]); - } - } - - this.put(normUrl, new ImageSize(width, height)); - } - } - } - } catch (JSONException e) { - AppLog.e(AppLog.T.READER, e); - } - } - - @Nullable - public ImageSize getImageSize(final String imageUrl) { - if (imageUrl == null) { - return null; - } else { - return super.get(UrlUtils.normalizeUrl(UrlUtils.removeQuery(imageUrl))); - } - } - - public String getLargestImageUrl(int minImageWidth) { - String currentImageUrl = null; - int currentMaxWidth = minImageWidth; - for (Entry attach : this.entrySet()) { - if (attach.getValue().width > currentMaxWidth) { - currentImageUrl = attach.getKey(); - currentMaxWidth = attach.getValue().width; - } - } - - return currentImageUrl; - } - - public static class ImageSize { - public final int width; - public final int height; - - public ImageSize(int width, int height) { - this.width = width; - this.height = height; - } - } -} diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.kt new file mode 100644 index 000000000000..54f27858b312 --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ImageSizeMap.kt @@ -0,0 +1,82 @@ +package org.wordpress.android.ui.reader.utils + +import org.json.JSONException +import org.json.JSONObject +import org.wordpress.android.util.AppLog +import org.wordpress.android.util.JSONUtils +import org.wordpress.android.util.UrlUtils +import androidx.core.net.toUri + +/** + * hash map of sizes of attachments in a reader post - created from the json "attachments" section + * of the post endpoints + */ +class ImageSizeMap(private val postContent: String, private val jsonString: String) : + HashMap() { + init { + if (jsonString.isNotEmpty() && jsonString != EMPTY_JSON) { + parseJson() + } + } + + @Suppress("NestedBlockDepth") + private fun parseJson() { + try { + val json = JSONObject(jsonString) + val keys = json.keys() + while (keys.hasNext()) { + json.optJSONObject(keys.next())?.let { jsonAttach -> + val mimeType = JSONUtils.getString(jsonAttach, "mime_type") + if (mimeType.startsWith("image")) { + parseJsonImage(jsonAttach) + } + } + } + } catch (e: JSONException) { + AppLog.e(AppLog.T.READER, e) + } + } + + private fun parseJsonImage(jsonImage: JSONObject) { + val imageUrl = JSONUtils.getString( + jsonImage, + "URL" + ) + val key = getKeyFromImageUrl(imageUrl) + + // make sure this image actually appears in the post content - it's possible for + // an image to be in the attachments but not in the post itself + val path = key.toUri().path + if (path != null && postContent.contains(path)) { + var width = jsonImage.optInt("width") + var height = jsonImage.optInt("height") + + // check if data-orig-size is present and use it + val originalSize = jsonImage.optString("data-orig-size") + val sizes = originalSize.split(",".toRegex()).dropLastWhile { it.isEmpty() } + .toTypedArray() + if (sizes.size == 2) { + width = sizes[0].toInt() + height = sizes[1].toInt() + } + + this[key] = + ImageSize( + width, + height + ) + } + } + + fun getImageSize(imageUrl: String): ImageSize? { + return get(getKeyFromImageUrl(imageUrl)) + } + + private fun getKeyFromImageUrl(imageUrl: String) = UrlUtils.normalizeUrl(UrlUtils.removeQuery(imageUrl)) + + class ImageSize(val width: Int, val height: Int) + + companion object { + private const val EMPTY_JSON = "{}" + } +}