@@ -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 (
@@ -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) {
0 commit comments