share_util.dart 5.2 KB

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