share_util.dart 4.5 KB

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