Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow user to cache image being uploaded to prevent re-download #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:firebase_cached_image/firebase_cached_image.dart';
import 'package:firebase_cached_image/src/core/cached_object.dart';

Expand Down Expand Up @@ -75,6 +77,12 @@ abstract class BaseFirebaseCacheManager {
/// Delete specific file from cache
Future<void> delete(FirebaseUrl firebaseUrl);

/// Add file to cache
///
/// When uploading the item to Firebase Storage, you can also add it to the cache
/// This prevents the user from having to download the file again after uploading it
Future<void> copyFileToCache(File fileToCache, FirebaseUrl firebaseUrl);

/// Check whether the file is cached or not.
///
/// On web it always return false.
Expand Down
19 changes: 19 additions & 0 deletions lib/src/firebase_cache_manager/mobile_firebase_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ class FirebaseCacheManager extends BaseFirebaseCacheManager {
]);
}

@override
Future<void> copyFileToCache(
File fileToCache, FirebaseUrl firebaseUrl,) async {
final manager = await _cacheManager;
final localPath = await getFullLocalPath(firebaseUrl.uniqueId);

await Future.wait([
fileToCache.copy(localPath),
manager.put(
CachedObject(
id: firebaseUrl.uniqueId,
fullLocalPath: localPath,
url: firebaseUrl.url.toString(),
modifiedAt: DateTime.now().millisecondsSinceEpoch,
),
),
]);
}

Future<String> getFullLocalPath(String fileName) async {
return join(await _cacheDirectoryPath, fileName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:io';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importing "dart:io" in this file will break the package's compatibility with Flutter Web.


import 'package:firebase_cached_image/src/core/cache_options.dart';
import 'package:firebase_cached_image/src/core/cached_object.dart';
import 'package:firebase_cached_image/src/core/firebase_url.dart';
import 'package:firebase_cached_image/src/firebase_cache_manager/base_firebase_cache_manager.dart';
import 'package:firebase_storage/firebase_storage.dart';

class FirebaseCacheManager extends BaseFirebaseCacheManager {
class FirebaseCacheManager extends BaseFirebaseCacheManager{
FirebaseCacheManager({super.subDir});

@override
Expand All @@ -28,6 +30,9 @@ class FirebaseCacheManager extends BaseFirebaseCacheManager {
@override
Future<void> refreshCachedFile(FirebaseUrl firebaseUrl) => Future.value();

@override
Future<void> copyFileToCache(File fileToCache, FirebaseUrl firebaseUrl) => Future.value();

@override
Future<CachedObject> getSingleObject(
FirebaseUrl firebaseUrl, {
Expand Down