share_util.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // @dart=2.9
  2. import 'dart:async';
  3. import 'dart:io' as dartio;
  4. import 'package:flutter/widgets.dart';
  5. import 'package:logging/logging.dart';
  6. import 'package:path/path.dart';
  7. import 'package:photos/core/configuration.dart';
  8. import 'package:photos/core/constants.dart';
  9. import 'package:photos/models/file.dart';
  10. import 'package:photos/models/file_type.dart';
  11. import 'package:photos/utils/date_time_util.dart';
  12. import 'package:photos/utils/dialog_util.dart';
  13. import 'package:photos/utils/exif_util.dart';
  14. import 'package:photos/utils/file_util.dart';
  15. import 'package:receive_sharing_intent/receive_sharing_intent.dart';
  16. import 'package:share_plus/share_plus.dart';
  17. final _logger = Logger("ShareUtil");
  18. // share is used to share media/files from ente to other apps
  19. Future<void> share(
  20. BuildContext context,
  21. List<File> files, {
  22. GlobalKey shareButtonKey,
  23. }) async {
  24. final dialog = createProgressDialog(context, "Preparing...");
  25. await dialog.show();
  26. try {
  27. final List<Future<String>> pathFutures = [];
  28. for (File file in files) {
  29. // Note: We are requesting the origin file for performance reasons on iOS.
  30. // This will eat up storage, which will be reset only when the app restarts.
  31. // We could have cleared the cache had there been a callback to the share API.
  32. pathFutures.add(
  33. getFile(file, isOrigin: true).then((fetchedFile) => fetchedFile.path),
  34. );
  35. if (file.fileType == FileType.livePhoto) {
  36. pathFutures.add(
  37. getFile(file, liveVideo: true)
  38. .then((fetchedFile) => fetchedFile.path),
  39. );
  40. }
  41. }
  42. final paths = await Future.wait(pathFutures);
  43. await dialog.hide();
  44. return Share.shareFiles(
  45. paths,
  46. // required for ipad https://github.com/flutter/flutter/issues/47220#issuecomment-608453383
  47. sharePositionOrigin: shareButtonRect(context, shareButtonKey),
  48. );
  49. } catch (e, s) {
  50. _logger.severe(
  51. "failed to fetch files for system share ${files.length}",
  52. e,
  53. s,
  54. );
  55. await dialog.hide();
  56. await showGenericErrorDialog(context);
  57. }
  58. }
  59. Rect shareButtonRect(BuildContext context, GlobalKey shareButtonKey) {
  60. Size size = MediaQuery.of(context).size;
  61. final RenderBox renderBox =
  62. shareButtonKey?.currentContext?.findRenderObject();
  63. if (renderBox == null) {
  64. return Rect.fromLTWH(0, 0, size.width, size.height / 2);
  65. }
  66. size = renderBox.size;
  67. final Offset position = renderBox.localToGlobal(Offset.zero);
  68. return Rect.fromCenter(
  69. center: position + Offset(size.width / 2, size.height / 2),
  70. width: size.width,
  71. height: size.height,
  72. );
  73. }
  74. Future<void> shareText(String text) async {
  75. return Share.share(text);
  76. }
  77. Future<List<File>> convertIncomingSharedMediaToFile(
  78. List<SharedMediaFile> sharedMedia,
  79. int collectionID,
  80. ) async {
  81. final List<File> localFiles = [];
  82. for (var media in sharedMedia) {
  83. if (!(media.type == SharedMediaType.IMAGE ||
  84. media.type == SharedMediaType.VIDEO)) {
  85. _logger.warning(
  86. "ignore unsupported file type ${media.type.toString()} path: ${media.path}",
  87. );
  88. continue;
  89. }
  90. final enteFile = File();
  91. // fileName: img_x.jpg
  92. enteFile.title = basename(media.path);
  93. var ioFile = dartio.File(media.path);
  94. ioFile = ioFile.renameSync(
  95. Configuration.instance.getSharedMediaDirectory() + "/" + enteFile.title,
  96. );
  97. enteFile.localID = sharedMediaIdentifier + enteFile.title;
  98. enteFile.collectionID = collectionID;
  99. enteFile.fileType =
  100. media.type == SharedMediaType.IMAGE ? FileType.image : FileType.video;
  101. if (enteFile.fileType == FileType.image) {
  102. final exifTime = await getCreationTimeFromEXIF(ioFile);
  103. if (exifTime != null) {
  104. enteFile.creationTime = exifTime.microsecondsSinceEpoch;
  105. }
  106. } else if (enteFile.fileType == FileType.video) {
  107. enteFile.duration = media.duration ~/ 1000 ?? 0;
  108. }
  109. if (enteFile.creationTime == null || enteFile.creationTime == 0) {
  110. final parsedDateTime =
  111. parseDateTimeFromFileNameV2(basenameWithoutExtension(media.path));
  112. if (parsedDateTime != null) {
  113. enteFile.creationTime = parsedDateTime.microsecondsSinceEpoch;
  114. } else {
  115. enteFile.creationTime = DateTime.now().microsecondsSinceEpoch;
  116. }
  117. }
  118. enteFile.modificationTime = enteFile.creationTime;
  119. localFiles.add(enteFile);
  120. }
  121. return localFiles;
  122. }
  123. DateTime parseDateFromFileNam1e(String fileName) {
  124. if (fileName.startsWith('IMG-') || fileName.startsWith('VID-')) {
  125. // Whatsapp media files
  126. return DateTime.tryParse(fileName.split('-')[1]);
  127. } else if (fileName.startsWith("Screenshot_")) {
  128. // Screenshots on droid
  129. return DateTime.tryParse(
  130. (fileName).replaceAll('Screenshot_', '').replaceAll('-', 'T'),
  131. );
  132. } else {
  133. return DateTime.tryParse(
  134. (fileName)
  135. .replaceAll("IMG_", "")
  136. .replaceAll("VID_", "")
  137. .replaceAll("DCIM_", "")
  138. .replaceAll("_", " "),
  139. );
  140. }
  141. }