share_util.dart 4.5 KB

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