Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gzipped and tgs files from network #2454

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
@RestrictTo(RestrictTo.Scope.LIBRARY)
public enum FileExtension {
JSON(".json"),
ZIP(".zip");
ZIP(".zip"),
GZIP(".gz");

public final String extension;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Pair<FileExtension, InputStream> fetch(String url) {
FileExtension extension;
if (cachedFile.getAbsolutePath().endsWith(".zip")) {
extension = FileExtension.ZIP;
} else if (cachedFile.getAbsolutePath().endsWith(".gz")) {
extension = FileExtension.GZIP;
} else {
extension = FileExtension.JSON;
}
Expand Down Expand Up @@ -143,6 +145,10 @@ private File getCachedFile(String url) throws FileNotFoundException {
if (zipFile.exists()) {
return zipFile;
}
File gzipFile = new File(parentDir(), filenameForUrl(url, FileExtension.GZIP, false));
if (gzipFile.exists()) {
return gzipFile;
}
return null;
}

Expand Down
34 changes: 30 additions & 4 deletions lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipInputStream;

@RestrictTo(RestrictTo.Scope.LIBRARY)
Expand Down Expand Up @@ -59,10 +60,19 @@ private LottieComposition fetchFromCache(Context context, @NonNull String url, @
FileExtension extension = cacheResult.first;
InputStream inputStream = cacheResult.second;
LottieResult<LottieComposition> result;
if (extension == FileExtension.ZIP) {
result = LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(inputStream), cacheKey);
} else {
result = LottieCompositionFactory.fromJsonInputStreamSync(inputStream, cacheKey);
switch (extension) {
case ZIP:
result = LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(inputStream), cacheKey);
break;
case GZIP:
try {
result = LottieCompositionFactory.fromJsonInputStreamSync(new GZIPInputStream(inputStream), cacheKey);
} catch (IOException e) {
result = new LottieResult<>(e);
}
break;
default:
result = LottieCompositionFactory.fromJsonInputStreamSync(inputStream, cacheKey);
}
if (result.getValue() != null) {
return result.getValue();
Expand Down Expand Up @@ -117,6 +127,12 @@ private LottieResult<LottieComposition> fromInputStream(Context context, @NonNul
Logger.debug("Handling zip response.");
extension = FileExtension.ZIP;
result = fromZipStream(context, url, inputStream, cacheKey);
} else if (contentType.contains("application/gzip") ||
contentType.contains("application/x-gzip") ||
url.split("\\?")[0].endsWith(".tgs")) {
Logger.debug("Handling gzip response.");
extension = FileExtension.GZIP;
result = fromGzipStream(url, inputStream, cacheKey);
} else {
Logger.debug("Received json response.");
extension = FileExtension.JSON;
Expand All @@ -140,6 +156,16 @@ private LottieResult<LottieComposition> fromZipStream(Context context, @NonNull
return LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(new FileInputStream(file)), url);
}

@NonNull
private LottieResult<LottieComposition> fromGzipStream(@NonNull String url, @NonNull InputStream inputStream, @Nullable String cacheKey)
throws IOException {
if (cacheKey == null || networkCache == null) {
return LottieCompositionFactory.fromJsonInputStreamSync(new GZIPInputStream(inputStream), null);
}
File file = networkCache.writeTempCacheFile(url, inputStream, FileExtension.GZIP);
return LottieCompositionFactory.fromJsonInputStreamSync(new GZIPInputStream(new FileInputStream(file)), url);
}

@NonNull
private LottieResult<LottieComposition> fromJsonStream(@NonNull String url, @NonNull InputStream inputStream, @Nullable String cacheKey)
throws IOException {
Expand Down
Loading