@@ -6,10 +6,13 @@ import 'package:firebase_image/src/firebase_image.dart';
66import 'package:firebase_image/src/image_object.dart' ;
77import 'package:firebase_core/firebase_core.dart' ;
88import 'package:firebase_storage/firebase_storage.dart' ;
9+ import 'package:flutter/cupertino.dart' ;
910import 'package:sqflite/sqflite.dart' ;
1011import 'package:path_provider/path_provider.dart' ;
1112import 'package:path/path.dart' ;
1213
14+ import 'image_fetch_strategy.dart' ;
15+
1316class 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 (
@@ -139,11 +144,30 @@ class FirebaseImageCacheManager {
139144 return null ;
140145 }
141146
142- Future <Uint8List > remoteFileBytes (
143- FirebaseImageObject object, int maxSizeBytes) {
147+ Future <Uint8List > _fetchToMemory (FirebaseImageObject object, int maxSizeBytes) {
144148 return object.reference.getData (maxSizeBytes);
145149 }
146150
151+ Future <Uint8List > _fetchToFile (FirebaseImageObject object) async {
152+ Directory dir = await getApplicationSupportDirectory ();
153+ File file = File ('${dir .path }/${object .remotePath }' );
154+ file.createSync (recursive: true );
155+ StorageFileDownloadTask task = object.reference.writeToFile (file);
156+ await task.future;
157+ return file.readAsBytesSync ();
158+ }
159+
160+ Future <Uint8List > remoteFileBytes (FirebaseImageObject object, int maxSizeBytes) {
161+ if (imageFetchStrategy == ImageFetchStrategy .FETCH_TO_FILE ) {
162+ return _fetchToFile (object);
163+ }
164+ else if (imageFetchStrategy == ImageFetchStrategy .FETCH_TO_MEMORY ) {
165+ return _fetchToMemory (object, maxSizeBytes);
166+ }
167+
168+ throw (Exception ("ImageFetchStrategy missing" ));
169+ }
170+
147171 Future <Uint8List > upsertRemoteFileToCache (
148172 FirebaseImageObject object, int maxSizeBytes) async {
149173 if (CacheRefreshStrategy .BY_METADATA_DATE == this .cacheRefreshStrategy) {
0 commit comments