share_util.dart 5.5 KB

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