file_uploader_util.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:async';
  2. import 'dart:io' as io;
  3. import 'dart:typed_data';
  4. import 'package:logging/logging.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import 'package:photos/core/constants.dart';
  7. import 'package:photos/core/errors.dart';
  8. import 'package:photos/models/file.dart' as ente;
  9. import 'package:photos/models/location.dart';
  10. import 'file_util.dart';
  11. final _logger = Logger("FileUtil");
  12. const kMaximumThumbnailCompressionAttempts = 2;
  13. class MediaUploadData {
  14. final io.File sourceFile;
  15. final Uint8List thumbnail;
  16. final bool isDeleted;
  17. MediaUploadData(this.sourceFile, this.thumbnail, this.isDeleted);
  18. }
  19. Future<MediaUploadData> getUploadDataFromEnteFile(ente.File file) async {
  20. if (file.isSharedMediaToAppSandbox()) {
  21. return await _getMediaUploadDataFromAppCache(file);
  22. } else {
  23. return await _getMediaUploadDataFromAssetFile(file);
  24. }
  25. }
  26. Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
  27. io.File sourceFile;
  28. Uint8List thumbnailData;
  29. bool isDeleted;
  30. // The timeouts are to safeguard against https://github.com/CaiJingLong/flutter_photo_manager/issues/467
  31. final asset =
  32. await file.getAsset().timeout(Duration(seconds: 3)).catchError((e) async {
  33. if (e is TimeoutException) {
  34. _logger.info("Asset fetch timed out for " + file.toString());
  35. return await file.getAsset();
  36. } else {
  37. throw e;
  38. }
  39. });
  40. if (asset == null) {
  41. throw InvalidFileError();
  42. }
  43. sourceFile = await asset.originFile
  44. .timeout(Duration(seconds: 3))
  45. .catchError((e) async {
  46. if (e is TimeoutException) {
  47. _logger.info("Origin file fetch timed out for " + file.toString());
  48. return await asset.originFile;
  49. } else {
  50. throw e;
  51. }
  52. });
  53. if (!sourceFile.existsSync()) {
  54. throw InvalidFileError();
  55. }
  56. thumbnailData = await asset.thumbDataWithSize(
  57. kThumbnailLargeSize,
  58. kThumbnailLargeSize,
  59. quality: kThumbnailQuality,
  60. );
  61. int compressionAttempts = 0;
  62. while (thumbnailData.length > kThumbnailDataLimit &&
  63. compressionAttempts < kMaximumThumbnailCompressionAttempts) {
  64. _logger.info("Thumbnail size " + thumbnailData.length.toString());
  65. thumbnailData = await compressThumbnail(thumbnailData);
  66. _logger
  67. .info("Compressed thumbnail size " + thumbnailData.length.toString());
  68. compressionAttempts++;
  69. }
  70. isDeleted = asset == null || !(await asset.exists);
  71. // h4ck to fetch location data if missing (thank you Android Q+) lazily only during uploads
  72. await _decorateEnteFileData(file, asset);
  73. return MediaUploadData(sourceFile, thumbnailData, isDeleted);
  74. }
  75. Future<void> _decorateEnteFileData(ente.File file, AssetEntity asset) async {
  76. // h4ck to fetch location data if missing (thank you Android Q+) lazily only during uploads
  77. if (file.location == null ||
  78. (file.location.latitude == 0 && file.location.longitude == 0)) {
  79. final latLong = await asset.latlngAsync();
  80. file.location = Location(latLong.latitude, latLong.longitude);
  81. }
  82. if (file.title == null || file.title.isEmpty) {
  83. _logger.severe("Title was missing");
  84. file.title = await asset.titleAsync;
  85. }
  86. }
  87. Future<MediaUploadData> _getMediaUploadDataFromAppCache(ente.File file) async {
  88. io.File sourceFile;
  89. Uint8List thumbnailData;
  90. bool isDeleted = false;
  91. var localPath = getSharedMediaFilePath(file);
  92. sourceFile = io.File(localPath);
  93. if (!sourceFile.existsSync()) {
  94. _logger.warning("File doesn't exist in app sandbox");
  95. throw InvalidFileError();
  96. }
  97. thumbnailData = await getThumbnailFromInAppCacheFile(file);
  98. return MediaUploadData(sourceFile, thumbnailData, isDeleted);
  99. }
  100. Future<Uint8List> getThumbnailFromInAppCacheFile(ente.File file) async {
  101. var localFile = io.File(getSharedMediaFilePath(file));
  102. if (!localFile.existsSync()) {
  103. return null;
  104. }
  105. var thumbnailData = localFile.readAsBytesSync();
  106. int compressionAttempts = 0;
  107. while (thumbnailData.length > kThumbnailDataLimit &&
  108. compressionAttempts < kMaximumThumbnailCompressionAttempts) {
  109. _logger.info("Thumbnail size " + thumbnailData.length.toString());
  110. thumbnailData = await compressThumbnail(thumbnailData);
  111. _logger
  112. .info("Compressed thumbnail size " + thumbnailData.length.toString());
  113. compressionAttempts++;
  114. }
  115. return thumbnailData;
  116. }