file_util.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import 'package:photo_manager/photo_manager.dart';
  2. import 'package:photos/core/cache/image_cache.dart';
  3. import 'package:photos/core/cache/thumbnail_cache.dart';
  4. import 'package:photos/core/constants.dart';
  5. import 'package:photos/db/files_db.dart';
  6. import 'package:photos/models/file.dart';
  7. import 'package:photos/models/file_type.dart';
  8. Future<void> deleteFiles(List<File> files,
  9. {bool deleteEveryWhere = false}) async {
  10. await PhotoManager.editor
  11. .deleteWithIds(files.map((file) => file.localId).toList());
  12. for (File file in files) {
  13. deleteEveryWhere
  14. ? await FilesDB.instance.markForDeletion(file)
  15. : await FilesDB.instance.delete(file);
  16. }
  17. }
  18. void preloadFile(File file) {
  19. if (file.fileType == FileType.video) {
  20. return;
  21. }
  22. if (file.localId == null) {
  23. if (BytesLruCache.get(file) == null) {
  24. file.getBytes().then((data) {
  25. BytesLruCache.put(file, data);
  26. });
  27. }
  28. } else {
  29. if (FileLruCache.get(file) == null) {
  30. file.getAsset().then((asset) {
  31. asset.file.then((assetFile) {
  32. FileLruCache.put(file, assetFile);
  33. });
  34. });
  35. }
  36. }
  37. }
  38. void preloadLocalFileThumbnail(File file) {
  39. if (file.localId == null ||
  40. ThumbnailLruCache.get(file, THUMBNAIL_SMALL_SIZE) != null) {
  41. return;
  42. }
  43. file.getAsset().then((asset) {
  44. asset
  45. .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
  46. .then((data) {
  47. ThumbnailLruCache.put(file, THUMBNAIL_SMALL_SIZE, data);
  48. });
  49. });
  50. }