share_util.dart 5.3 KB

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