share_util.dart 4.6 KB

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