Skip to content

High performance self-hosted photo and video management solution.

perf(mobile): remove small thumbnail and cache generated thumbnails (#17792)

Toni59fa8fbd0e6f
diff --git a/mobile/lib/providers/image/immich_local_thumbnail_provider.dart b/mobile/lib/providers/image/immich_local_thumbnail_provider.dart
index 1e2f5d312e..edcf8a9458 100644
--- a/mobile/lib/providers/image/immich_local_thumbnail_provider.dart
+++ b/mobile/lib/providers/image/immich_local_thumbnail_provider.dart
@@ -2,11 +2,14 @@ import 'dart:async';
import 'dart:ui' as ui;
import 'package:cached_network_image/cached_network_image.dart';
+import 'package:flutter_cache_manager/flutter_cache_manager.dart';
+import 'package:immich_mobile/providers/image/cache/thumbnail_image_cache_manager.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:photo_manager/photo_manager.dart' show ThumbnailSize;
+import 'package:logging/logging.dart';
/// The local image provider for an asset
/// Only viable
@@ -15,11 +18,16 @@ class ImmichLocalThumbnailProvider
final Asset asset;
final int height;
final int width;
+ final CacheManager? cacheManager;
+ final Logger log = Logger("ImmichLocalThumbnailProvider");
+ final String? userId;
ImmichLocalThumbnailProvider({
required this.asset,
this.height = 256,
this.width = 256,
+ this.cacheManager,
+ this.userId,
}) : assert(asset.local != null, 'Only usable when asset.local is set');
/// Converts an [ImageProvider]'s settings plus an [ImageConfiguration] to a key
@@ -36,11 +44,10 @@ class ImmichLocalThumbnailProvider
ImmichLocalThumbnailProvider key,
ImageDecoderCallback decode,
) {
- final chunkEvents = StreamController<ImageChunkEvent>();
+ final cache = cacheManager ?? ThumbnailImageCacheManager();
return MultiImageStreamCompleter(
- codec: _codec(key.asset, decode, chunkEvents),
+ codec: _codec(key.asset, cache, decode),
scale: 1.0,
- chunkEvents: chunkEvents.stream,
informationCollector: () sync* {
yield ErrorDescription(key.asset.fileName);
},
@@ -50,25 +57,38 @@ class ImmichLocalThumbnailProvider
// Streams in each stage of the image as we ask for it
Stream<ui.Codec> _codec(
Asset assetData,
+ CacheManager cache,
ImageDecoderCallback decode,
- StreamController<ImageChunkEvent> chunkEvents,
) async* {
- final thumbBytes = await assetData.local
- ?.thumbnailDataWithSize(ThumbnailSize(width, height));
- if (thumbBytes == null) {
- chunkEvents.close();
+ final cacheKey =
+ '$userId${assetData.localId}${assetData.checksum}$width$height';
+ final fileFromCache = await cache.getFileFromCache(cacheKey);
+ if (fileFromCache != null) {
+ try {
+ final buffer =
+ await ui.ImmutableBuffer.fromFilePath(fileFromCache.file.path);
+ final codec = await decode(buffer);
+ yield codec;
+ return;
+ } catch (error) {
+ log.severe('Found thumbnail in cache, but loading it failed', error);
+ }
+ }
+
+ final thumbnailBytes = await assetData.local?.thumbnailDataWithSize(
+ ThumbnailSize(width, height),
+ quality: 80,
+ );
+ if (thumbnailBytes == null) {
throw StateError(
- "Loading thumb for local photo ${asset.fileName} failed",
+ "Loading thumb for local photo ${assetData.fileName} failed",
);
}
- try {
- final buffer = await ui.ImmutableBuffer.fromUint8List(thumbBytes);
- final codec = await decode(buffer);
- yield codec;
- } finally {
- chunkEvents.close();
- }
+ final buffer = await ui.ImmutableBuffer.fromUint8List(thumbnailBytes);
+ final codec = await decode(buffer);
+ yield codec;
+ await cache.putFile(cacheKey, thumbnailBytes);
}
@override
diff --git a/mobile/lib/widgets/common/immich_thumbnail.dart b/mobile/lib/widgets/common/immich_thumbnail.dart
index 2ebead0083..35729ead7b 100644
--- a/mobile/lib/widgets/common/immich_thumbnail.dart
+++ b/mobile/lib/widgets/common/immich_thumbnail.dart
@@ -1,7 +1,7 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
-import 'package:flutter_hooks/flutter_hooks.dart';
+import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/providers/image/immich_local_thumbnail_provider.dart';
import 'package:immich_mobile/providers/image/immich_remote_thumbnail_provider.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
@@ -9,8 +9,9 @@ import 'package:immich_mobile/utils/hooks/blurhash_hook.dart';
import 'package:immich_mobile/widgets/common/immich_image.dart';
import 'package:immich_mobile/widgets/common/thumbhash_placeholder.dart';
import 'package:octo_image/octo_image.dart';
+import 'package:immich_mobile/providers/user.provider.dart';
-class ImmichThumbnail extends HookWidget {
+class ImmichThumbnail extends HookConsumerWidget {
const ImmichThumbnail({
this.asset,
this.width = 250,
@@ -31,6 +32,7 @@ class ImmichThumbnail extends HookWidget {
static ImageProvider imageProvider({
Asset? asset,
String? assetId,
+ String? userId,
int thumbnailSize = 256,
}) {
if (asset == null && assetId == null) {
@@ -48,6 +50,7 @@ class ImmichThumbnail extends HookWidget {
asset: asset,
height: thumbnailSize,
width: thumbnailSize,
+ userId: userId,
);
} else {
return ImmichRemoteThumbnailProvider(
@@ -59,8 +62,10 @@ class ImmichThumbnail extends HookWidget {
}
@override
- Widget build(BuildContext context) {
+ Widget build(BuildContext context, WidgetRef ref) {
Uint8List? blurhash = useBlurHashRef(asset).value;
+ final userId = ref.watch(currentUserProvider)?.id;
+
if (asset == null) {
return Container(
color: Colors.grey,
@@ -79,6 +84,7 @@ class ImmichThumbnail extends HookWidget {
octoSet: blurHashOrPlaceholder(blurhash),
image: ImmichThumbnail.imageProvider(
asset: asset,
+ userId: userId,
),
width: width,
height: height,