ente/lib/utils/share_util.dart

132 lines
4.5 KiB
Dart
Raw Normal View History

// @dart=2.9
import 'dart:async';
import 'dart:io' as dartio;
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/constants.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/exif_util.dart';
import 'package:photos/utils/file_util.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
2021-10-05 04:56:48 +00:00
import 'package:share_plus/share_plus.dart';
final _logger = Logger("ShareUtil");
// share is used to share media/files from ente to other apps
2022-06-11 08:23:52 +00:00
Future<void> share(
BuildContext context,
List<File> files, {
GlobalKey shareButtonKey,
}) async {
2022-06-06 16:13:29 +00:00
final dialog = createProgressDialog(context, "Preparing...");
await dialog.show();
2021-06-11 07:19:19 +00:00
final List<Future<String>> pathFutures = [];
for (File file in files) {
2021-10-05 04:51:54 +00:00
// Note: We are requesting the origin file for performance reasons on iOS.
// This will eat up storage, which will be reset only when the app restarts.
// We could have cleared the cache had there been a callback to the share API.
pathFutures.add(getFile(file, isOrigin: true).then((file) => file.path));
if (file.fileType == FileType.livePhoto) {
pathFutures.add(getFile(file, liveVideo: true).then((file) => file.path));
}
}
2021-01-13 10:16:31 +00:00
final paths = await Future.wait(pathFutures);
await dialog.hide();
2022-04-22 15:37:12 +00:00
return Share.shareFiles(
paths,
// required for ipad https://github.com/flutter/flutter/issues/47220#issuecomment-608453383
sharePositionOrigin: shareButtonRect(context, shareButtonKey),
2022-04-22 15:37:12 +00:00
);
}
Rect shareButtonRect(BuildContext context, GlobalKey shareButtonKey) {
2022-04-22 16:17:32 +00:00
Size size = MediaQuery.of(context).size;
2022-08-29 14:43:31 +00:00
final RenderBox renderBox = shareButtonKey?.currentContext?.findRenderObject();
if (renderBox == null) {
return Rect.fromLTWH(0, 0, size.width, size.height / 2);
}
size = renderBox.size;
2022-08-29 14:43:31 +00:00
final Offset position = renderBox.localToGlobal(Offset.zero);
return Rect.fromCenter(
center: position + Offset(size.width / 2, size.height / 2),
width: size.width,
height: size.height,
);
}
2022-04-22 16:17:32 +00:00
2020-10-09 23:47:14 +00:00
Future<void> shareText(String text) async {
2021-01-13 10:16:31 +00:00
return Share.share(text);
2020-05-25 15:07:22 +00:00
}
Future<List<File>> convertIncomingSharedMediaToFile(
2022-06-11 08:23:52 +00:00
List<SharedMediaFile> sharedMedia,
int collectionID,
) async {
2022-08-29 14:43:31 +00:00
final List<File> localFiles = [];
for (var media in sharedMedia) {
if (!(media.type == SharedMediaType.IMAGE ||
media.type == SharedMediaType.VIDEO)) {
_logger.warning(
2022-06-11 08:23:52 +00:00
"ignore unsupported file type ${media.type.toString()} path: ${media.path}",
);
continue;
}
2022-08-29 14:43:31 +00:00
final enteFile = File();
// fileName: img_x.jpg
enteFile.title = basename(media.path);
var ioFile = dartio.File(media.path);
ioFile = ioFile.renameSync(
2022-07-05 20:05:51 +00:00
Configuration.instance.getSharedMediaDirectory() + "/" + enteFile.title,
2022-06-11 08:23:52 +00:00
);
enteFile.localID = kSharedMediaIdentifier + enteFile.title;
enteFile.collectionID = collectionID;
enteFile.fileType =
media.type == SharedMediaType.IMAGE ? FileType.image : FileType.video;
if (enteFile.fileType == FileType.image) {
final exifTime = await getCreationTimeFromEXIF(ioFile);
if (exifTime != null) {
enteFile.creationTime = exifTime.microsecondsSinceEpoch;
}
} else if (enteFile.fileType == FileType.video) {
enteFile.duration = media.duration ~/ 1000 ?? 0;
}
if (enteFile.creationTime == null || enteFile.creationTime == 0) {
final parsedDateTime =
parseDateFromFileName(basenameWithoutExtension(media.path));
if (parsedDateTime != null) {
enteFile.creationTime = parsedDateTime.microsecondsSinceEpoch;
} else {
enteFile.creationTime = DateTime.now().microsecondsSinceEpoch;
}
}
enteFile.modificationTime = enteFile.creationTime;
localFiles.add(enteFile);
}
return localFiles;
}
DateTime parseDateFromFileName(String fileName) {
if (fileName.startsWith('IMG-') || fileName.startsWith('VID-')) {
// Whatsapp media files
return DateTime.tryParse(fileName.split('-')[1]);
} else if (fileName.startsWith("Screenshot_")) {
// Screenshots on droid
return DateTime.tryParse(
2022-06-11 08:23:52 +00:00
(fileName).replaceAll('Screenshot_', '').replaceAll('-', 'T'),
);
} else {
2022-06-11 08:23:52 +00:00
return DateTime.tryParse(
(fileName)
.replaceAll("IMG_", "")
.replaceAll("VID_", "")
.replaceAll("DCIM_", "")
.replaceAll("_", " "),
);
}
}