Dr-Blank.Vaani/lib/api/image_provider.dart

76 lines
2.4 KiB
Dart
Raw Normal View History

2024-05-08 05:03:49 -04:00
import 'dart:typed_data';
2024-06-28 06:01:56 -04:00
import 'package:logging/logging.dart';
2024-05-08 05:03:49 -04:00
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
2024-08-23 04:21:46 -04:00
import 'package:vaani/api/api_provider.dart';
import 'package:vaani/api/library_item_provider.dart';
2024-08-23 04:21:46 -04:00
import 'package:vaani/db/cache_manager.dart';
2024-05-08 05:03:49 -04:00
/// provides cover images for the audiobooks
///
/// is a stream provider that provides cover images first from the cache then from the server
/// if the image is not found in the cache, it will be fetched from the server and saved to the cache
/// if the image is not found in the server it will throw an error
part 'image_provider.g.dart';
2024-06-28 06:01:56 -04:00
final _logger = Logger('cover_image_provider');
2024-05-14 06:13:16 -04:00
@Riverpod(keepAlive: true)
2024-05-08 05:03:49 -04:00
class CoverImage extends _$CoverImage {
@override
Stream<Uint8List> build(String itemId) async* {
2024-05-08 05:03:49 -04:00
final api = ref.watch(authenticatedApiProvider);
2024-05-08 21:25:06 -04:00
// ! artifical delay for testing
// await Future.delayed(const Duration(seconds: 2));
2024-05-08 05:03:49 -04:00
// try to get the image from the cache
final file = await imageCacheManager.getFileFromMemory(itemId) ??
await imageCacheManager.getFileFromCache(itemId);
2024-05-08 05:03:49 -04:00
if (file != null) {
// if the image is in the cache, yield it
2024-06-28 06:01:56 -04:00
_logger.fine(
'cover image found in cache for $itemId at ${file.file.path}',
2024-05-08 21:25:06 -04:00
);
2024-05-08 05:03:49 -04:00
yield await file.file.readAsBytes();
final libraryItem = await ref.watch(libraryItemProvider(itemId).future);
2024-05-08 05:03:49 -04:00
// return if no need to fetch from the server
if (libraryItem.updatedAt.isBefore(await file.file.lastModified())) {
2024-06-28 06:01:56 -04:00
_logger.fine(
'cover image is up to date for $itemId, no need to fetch from the server',
2024-05-09 23:23:50 -04:00
);
2024-05-08 05:03:49 -04:00
return;
} else {
2024-06-28 06:01:56 -04:00
_logger.fine(
'cover image stale for $itemId, fetching from the server',
2024-05-08 05:03:49 -04:00
);
}
2024-05-12 05:38:30 -04:00
} else {
_logger.fine('cover image not found in cache for $itemId');
2024-05-08 05:03:49 -04:00
}
// check if the image is in the cache
final coverImage = await api.items.getCover(
libraryItemId: itemId,
2024-05-09 23:23:50 -04:00
parameters: const GetImageReqParams(width: 1200),
2024-05-08 05:03:49 -04:00
);
// save the image to the cache
2024-05-09 23:23:50 -04:00
if (coverImage != null) {
final newFile = await imageCacheManager.putFile(
itemId,
2024-05-09 23:23:50 -04:00
coverImage,
key: itemId,
2024-05-12 05:38:30 -04:00
fileExtension: 'jpg',
2024-05-09 23:23:50 -04:00
);
2024-06-28 06:01:56 -04:00
_logger.fine(
'cover image fetched for for $itemId, file time: ${await newFile.lastModified()}',
2024-05-09 23:23:50 -04:00
);
}
2024-05-08 05:03:49 -04:00
yield coverImage ?? Uint8List(0);
}
}