From ee95c3f77d060f1c4366da377372047efbed4a43 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Fri, 2 Apr 2021 14:03:54 -0400 Subject: [PATCH] fix image resolution mechanism --- .../awais/instagrabber/utils/ResponseBodyUtils.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java index 2722d11e..0d7acc16 100644 --- a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java @@ -1090,11 +1090,19 @@ public final class ResponseBodyUtils { if (imageVersions2 == null) return null; final List candidates = imageVersions2.getCandidates(); if (candidates == null || candidates.isEmpty()) return null; + final boolean isSquare = Integer.compare(media.getOriginalWidth(), media.getOriginalHeight()) == 0; final List sortedCandidates = candidates.stream() .sorted((c1, c2) -> Integer.compare(c2.getWidth(), c1.getWidth())) - .filter(c -> c.getWidth() < type.getValue()) .collect(Collectors.toList()); - final MediaCandidate candidate = sortedCandidates.get(0); + if (sortedCandidates.size() == 1) return sortedCandidates.get(0).getUrl(); + final List filteredCandidates = sortedCandidates.stream() + .filter(c -> + c.getWidth() <= media.getOriginalWidth() + && c.getWidth() <= type.getValue() + && (isSquare || Integer.compare(c.getWidth(), c.getHeight()) != 0) + ) + .collect(Collectors.toList()); + final MediaCandidate candidate = filteredCandidates.get(0); if (candidate == null) return null; return candidate.getUrl(); }