file_sync_util.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'dart:math';
  2. import 'package:logging/logging.dart';
  3. import 'package:photo_manager/photo_manager.dart';
  4. import 'package:photos/models/file.dart';
  5. final _logger = Logger("FileSyncUtil");
  6. Future<List<File>> getDeviceFiles(int fromTime, int toTime) async {
  7. final pathEntities = await _getGalleryList(fromTime, toTime);
  8. final files = List<File>();
  9. AssetPathEntity recents;
  10. for (AssetPathEntity pathEntity in pathEntities) {
  11. if (pathEntity.name == "Recent" || pathEntity.name == "Recents") {
  12. recents = pathEntity;
  13. } else {
  14. await _addToPhotos(pathEntity, fromTime, files);
  15. }
  16. }
  17. if (recents != null) {
  18. await _addToPhotos(recents, fromTime, files);
  19. }
  20. files.sort(
  21. (first, second) => first.creationTime.compareTo(second.creationTime));
  22. return files;
  23. }
  24. Future<List<AssetPathEntity>> _getGalleryList(
  25. final int fromTime, final int toTime) async {
  26. final filterOptionGroup = FilterOptionGroup();
  27. filterOptionGroup.setOption(AssetType.image, FilterOption(needTitle: true));
  28. filterOptionGroup.setOption(AssetType.video, FilterOption(needTitle: true));
  29. filterOptionGroup.createTimeCond = DateTimeCond(
  30. min: DateTime.fromMicrosecondsSinceEpoch(fromTime),
  31. max: DateTime.fromMicrosecondsSinceEpoch(toTime),
  32. );
  33. final galleryList = await PhotoManager.getAssetPathList(
  34. hasAll: true,
  35. type: RequestType.common,
  36. filterOption: filterOptionGroup,
  37. );
  38. galleryList.sort((s1, s2) {
  39. return s2.assetCount.compareTo(s1.assetCount);
  40. });
  41. return galleryList;
  42. }
  43. Future _addToPhotos(
  44. AssetPathEntity pathEntity, int fromTime, List<File> files) async {
  45. final assetList = await pathEntity.assetList;
  46. for (AssetEntity entity in assetList) {
  47. if (max(entity.createDateTime.microsecondsSinceEpoch,
  48. entity.modifiedDateTime.microsecondsSinceEpoch) >
  49. fromTime) {
  50. try {
  51. final file = await File.fromAsset(pathEntity, entity);
  52. if (!files.contains(file)) {
  53. files.add(file);
  54. }
  55. } catch (e) {
  56. _logger.severe(e);
  57. }
  58. }
  59. }
  60. }