On disk we are only caching the full-size images.

So when modify the key to query for the full size image,
because scaling down a larger size image on the device is quicker than
requesting the down-sized image from the network.
This commit is contained in:
tzugen 2021-06-08 13:47:47 +02:00
parent 78cb4d09cf
commit 3c554caf2e
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 18 additions and 5 deletions

View File

@ -57,6 +57,8 @@ public class FileUtil
private static final List<String> VIDEO_FILE_EXTENSIONS = Arrays.asList("flv", "mp4", "m4v", "wmv", "avi", "mov", "mpg", "mkv");
private static final List<String> PLAYLIST_FILE_EXTENSIONS = Collections.singletonList("m3u");
private static final Pattern TITLE_WITH_TRACK = Pattern.compile("^\\d\\d-.*");
public static final String SUFFIX_LARGE = ".jpeg";
public static final String SUFFIX_SMALL = ".jpeg-small";
private static final Lazy<PermissionUtil> permissionUtil = inject(PermissionUtil.class);
@ -141,7 +143,9 @@ public class FileUtil
return null;
}
return String.format(Locale.ROOT, "%s%b.jpeg", Util.md5Hex(albumDir.getPath()), large);
String suffix = (large) ? SUFFIX_LARGE : SUFFIX_SMALL;
return String.format(Locale.ROOT, "%s%s", Util.md5Hex(albumDir.getPath()), suffix);
}

View File

@ -7,6 +7,8 @@ import com.squareup.picasso.RequestHandler
import java.io.IOException
import okio.Okio
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
import org.moire.ultrasonic.util.FileUtil.SUFFIX_LARGE
import org.moire.ultrasonic.util.FileUtil.SUFFIX_SMALL
/**
* Loads cover arts from subsonic api.
@ -25,16 +27,23 @@ class CoverArtRequestHandler(private val apiClient: SubsonicAPIClient) : Request
val size = request.uri.getQueryParameter(SIZE)?.toLong()
// Check if we have a hit in the disk cache
val cache = BitmapUtils.getAlbumArtBitmapFromDisk(request.stableKey!!, size?.toInt())
// Note: Currently we are only caching full size images on disk
// So we modify the key to query for the full size image,
// because scaling down a larger size image on the device is quicker than
// requesting the down-sized image from the network.
val key = request.stableKey!!.replace(SUFFIX_SMALL, SUFFIX_LARGE)
val cache = BitmapUtils.getAlbumArtBitmapFromDisk(key, size?.toInt())
if (cache != null) {
return Result(cache, DISK)
}
// Try to fetch the image from the API
val response = apiClient.getCoverArt(id, size)
if (response.hasError() || response.stream == null) {
throw IOException("${response.apiError}")
} else {
if (!response.hasError() && response.stream != null) {
return Result(Okio.source(response.stream!!), NETWORK)
}
// Throw an error if still not successful
throw IOException("${response.apiError}")
}
}