Skip to content

Commit db1661d

Browse files
committed
add ImageFetchStrategy to either pull from Firebase to memory or to a file
1 parent 7c5234e commit db1661d

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

lib/firebase_image.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ library firebase_image;
22

33
export 'src/firebase_image.dart';
44
export 'src/cache_refresh_strategy.dart';
5+
export 'src/image_fetch_strategy.dart';

lib/src/cache_manager.dart

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import 'package:firebase_image/src/firebase_image.dart';
66
import 'package:firebase_image/src/image_object.dart';
77
import 'package:firebase_core/firebase_core.dart';
88
import 'package:firebase_storage/firebase_storage.dart';
9+
import 'package:flutter/cupertino.dart';
910
import 'package:sqflite/sqflite.dart';
1011
import 'package:path_provider/path_provider.dart';
1112
import 'package:path/path.dart';
1213

14+
import 'image_fetch_strategy.dart';
15+
1316
class FirebaseImageCacheManager {
1417
static const String key = 'firebase_image';
1518

@@ -19,10 +22,12 @@ class FirebaseImageCacheManager {
1922
String basePath;
2023

2124
final CacheRefreshStrategy cacheRefreshStrategy;
25+
final ImageFetchStrategy imageFetchStrategy;
2226

23-
FirebaseImageCacheManager(
24-
this.cacheRefreshStrategy,
25-
);
27+
FirebaseImageCacheManager({
28+
@required this.cacheRefreshStrategy,
29+
this.imageFetchStrategy = ImageFetchStrategy.FETCH_TO_MEMORY,
30+
}) : assert(cacheRefreshStrategy != null);
2631

2732
Future<void> open() async {
2833
db = await openDatabase(
@@ -138,11 +143,30 @@ class FirebaseImageCacheManager {
138143
return null;
139144
}
140145

141-
Future<Uint8List> remoteFileBytes(
142-
FirebaseImageObject object, int maxSizeBytes) {
146+
Future<Uint8List> _fetchToMemory(FirebaseImageObject object, int maxSizeBytes) {
143147
return object.reference.getData(maxSizeBytes);
144148
}
145149

150+
Future<Uint8List> _fetchToFile(FirebaseImageObject object) async {
151+
Directory dir = await getApplicationSupportDirectory();
152+
File file = File('${dir.path}/${object.remotePath}');
153+
file.createSync(recursive: true);
154+
StorageFileDownloadTask task = object.reference.writeToFile(file);
155+
await task.future;
156+
return file.readAsBytesSync();
157+
}
158+
159+
Future<Uint8List> remoteFileBytes(FirebaseImageObject object, int maxSizeBytes) {
160+
if (imageFetchStrategy == ImageFetchStrategy.FETCH_TO_FILE) {
161+
return _fetchToFile(object);
162+
}
163+
else if (imageFetchStrategy == ImageFetchStrategy.FETCH_TO_MEMORY) {
164+
return _fetchToMemory(object, maxSizeBytes);
165+
}
166+
167+
throw(Exception("ImageFetchStrategy missing"));
168+
}
169+
146170
Future<Uint8List> upsertRemoteFileToCache(
147171
FirebaseImageObject object, int maxSizeBytes) async {
148172
if (CacheRefreshStrategy.BY_METADATA_DATE == this.cacheRefreshStrategy) {

lib/src/firebase_image.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'package:firebase_storage/firebase_storage.dart';
99
import 'package:flutter/foundation.dart';
1010
import 'package:flutter/material.dart';
1111

12+
import 'image_fetch_strategy.dart';
13+
1214
class FirebaseImage extends ImageProvider<FirebaseImage> {
1315
// Default: True. Specified whether or not an image should be cached (optional)
1416
final bool shouldCache;
@@ -28,6 +30,9 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
2830
/// The model for the image object
2931
final FirebaseImageObject _imageObject;
3032

33+
/// Default: FETCH_TO_MEMORY. Specifies the strategy in which to fetch the image from Firebase (optional)
34+
final ImageFetchStrategy imageFetchStrategy;
35+
3136
/// Fetches, saves and returns an ImageProvider for any image in a readable Firebase Cloud Storeage bucket.
3237
///
3338
/// [location] The URI of the image, in the bucket, to be displayed
@@ -43,6 +48,7 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
4348
this.maxSizeBytes = 2500 * 1000, // 2.5MB
4449
this.cacheRefreshStrategy = CacheRefreshStrategy.BY_METADATA_DATE,
4550
this.firebaseApp,
51+
this.imageFetchStrategy = ImageFetchStrategy.FETCH_TO_MEMORY,
4652
}) : _imageObject = FirebaseImageObject(
4753
bucket: _getBucket(location),
4854
remotePath: _getImagePath(location),
@@ -73,7 +79,8 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
7379
Future<Uint8List> _fetchImage() async {
7480
Uint8List bytes;
7581
FirebaseImageCacheManager cacheManager = FirebaseImageCacheManager(
76-
cacheRefreshStrategy,
82+
cacheRefreshStrategy: cacheRefreshStrategy,
83+
imageFetchStrategy: imageFetchStrategy,
7784
);
7885

7986
if (shouldCache) {

lib/src/image_fetch_strategy.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enum ImageFetchStrategy {
2+
// Asynchronously downloads the object at the StorageReference to a list in memory.
3+
// A list of the provided max size will be allocated.
4+
FETCH_TO_MEMORY,
5+
// Asynchronously downloads the object to a specified system file.
6+
FETCH_TO_FILE,
7+
}

0 commit comments

Comments
 (0)