Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(panorama): update panorama checking mechanism to reduce checks

Prateek Sunal4f14fafa3453
diff --git a/mobile/lib/models/file/extensions/file_props.dart b/mobile/lib/models/file/extensions/file_props.dart
index 6f68f916b9..9be9bd4c11 100644
--- a/mobile/lib/models/file/extensions/file_props.dart
+++ b/mobile/lib/models/file/extensions/file_props.dart
@@ -18,18 +18,26 @@ extension FilePropsExtn on EnteFile {
bool get hasDims => height > 0 && width > 0;
- // return true if the file is a panorama image, null if the dimensions are not available
+ // return true if the file can be a panorama image, null if the dimensions are not available
bool? isPanorama() {
if (fileType != FileType.image) {
return false;
}
+ if (pubMagicMetadata?.mediaType != null) {
+ return (pubMagicMetadata!.mediaType! & 1) == 1;
+ }
+ return null;
+ }
+
+ bool canBePanorama() {
if (hasDims) {
+ if (height < 8000 && width < 8000) return false;
if (height > width) {
return height / width >= 2.0;
}
return width / height >= 2.0;
}
- return null;
+ return false;
}
bool get canEditMetaInfo => isUploaded && isOwner;
diff --git a/mobile/lib/models/file/file.dart b/mobile/lib/models/file/file.dart
index 9df25bb051..b4276878ce 100644
--- a/mobile/lib/models/file/file.dart
+++ b/mobile/lib/models/file/file.dart
@@ -2,6 +2,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
+import "package:motion_photos/src/xmp_extractor.dart";
import 'package:path/path.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/core/configuration.dart';
@@ -13,6 +14,7 @@ import "package:photos/service_locator.dart";
import 'package:photos/utils/date_time_util.dart';
import 'package:photos/utils/exif_util.dart';
import 'package:photos/utils/file_uploader_util.dart';
+import "package:photos/utils/panorama_util.dart";
//Todo: files with no location data have lat and long set to 0.0. This should ideally be null.
class EnteFile {
@@ -179,6 +181,15 @@ class EnteFile {
hasExifTime = true;
creationTime = exifTime.microsecondsSinceEpoch;
}
+ mediaUploadData.isPanorama = checkPanoramaFromEXIF(null, exifData);
+
+ try {
+ final xmpData = XMPExtractor()
+ .extract(mediaUploadData.sourceFile!.readAsBytesSync());
+ mediaUploadData.isPanorama = checkPanoramaFromXMP(xmpData);
+ } catch (_) {}
+
+ mediaUploadData.isPanorama ??= false;
}
if (Platform.isAndroid) {
//Fix for missing location data in lower android versions.
diff --git a/mobile/lib/models/metadata/file_magic.dart b/mobile/lib/models/metadata/file_magic.dart
index d50dc0dd84..6c146fa031 100644
--- a/mobile/lib/models/metadata/file_magic.dart
+++ b/mobile/lib/models/metadata/file_magic.dart
@@ -8,6 +8,7 @@ const captionKey = "caption";
const uploaderNameKey = "uploaderName";
const widthKey = 'w';
const heightKey = 'h';
+const mediaTypeKey = 'mediaType';
const latKey = "lat";
const longKey = "long";
const motionVideoIndexKey = "mvi";
@@ -55,6 +56,11 @@ class PubMagicMetadata {
// should have exact same hash with should match the constant `blackThumbnailBase64`
bool? noThumb;
+ // null -> not computed
+ // 0 -> normal
+ // 1 -> panorama
+ int? mediaType;
+
PubMagicMetadata({
this.editedTime,
this.editedName,
@@ -66,6 +72,7 @@ class PubMagicMetadata {
this.long,
this.mvi,
this.noThumb,
+ this.mediaType,
});
factory PubMagicMetadata.fromEncodedJson(String encodedJson) =>
@@ -87,6 +94,7 @@ class PubMagicMetadata {
long: map[longKey],
mvi: map[motionVideoIndexKey],
noThumb: map[noThumbKey],
+ mediaType: map[mediaTypeKey],
);
}
}
diff --git a/mobile/lib/ui/actions/file/file_actions.dart b/mobile/lib/ui/actions/file/file_actions.dart
index 83ba1fa72a..6a7305a019 100644
--- a/mobile/lib/ui/actions/file/file_actions.dart
+++ b/mobile/lib/ui/actions/file/file_actions.dart
@@ -5,6 +5,7 @@ import "package:modal_bottom_sheet/modal_bottom_sheet.dart";
import "package:photos/generated/l10n.dart";
import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
+import "package:photos/service_locator.dart";
import "package:photos/theme/colors.dart";
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/components/action_sheet_widget.dart";
@@ -13,6 +14,7 @@ import "package:photos/ui/components/models/button_type.dart";
import 'package:photos/ui/viewer/file/file_details_widget.dart';
import "package:photos/utils/delete_file_util.dart";
import "package:photos/utils/dialog_util.dart";
+import "package:photos/utils/panorama_util.dart";
import "package:photos/utils/toast_util.dart";
Future<void> showSingleFileDeleteSheet(
@@ -135,6 +137,9 @@ Future<void> showSingleFileDeleteSheet(
}
Future<void> showDetailsSheet(BuildContext context, EnteFile file) async {
+ if (flagService.internalUser) {
+ guardedCheckPanorama(file).ignore();
+ }
final colorScheme = getEnteColorScheme(context);
return showBarModalBottomSheet(
topControl: const SizedBox.shrink(),
diff --git a/mobile/lib/ui/tools/editor/video_editor_page.dart b/mobile/lib/ui/tools/editor/video_editor_page.dart
index 5cb5afac8a..2fcba8990b 100644
--- a/mobile/lib/ui/tools/editor/video_editor_page.dart
+++ b/mobile/lib/ui/tools/editor/video_editor_page.dart
@@ -200,7 +200,9 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
final config = VideoFFmpegVideoEditorConfig(
_controller!,
- format: VideoExportFormat.mp4,
+ format: VideoExportFormat(
+ path.extension(widget.ioFile.path).substring(1),
+ ),
// commandBuilder: (config, videoPath, outputPath) {
// final List<String> filters = config.getExportFilters();
// filters.add('hflip'); // add horizontal flip
diff --git a/mobile/lib/ui/viewer/file/file_bottom_bar.dart b/mobile/lib/ui/viewer/file/file_bottom_bar.dart
index 9db7275918..9d9b00ebb6 100644
--- a/mobile/lib/ui/viewer/file/file_bottom_bar.dart
+++ b/mobile/lib/ui/viewer/file/file_bottom_bar.dart
@@ -50,7 +50,6 @@ class FileBottomBarState extends State<FileBottomBar> {
bool _isFileSwipeLocked = false;
late final StreamSubscription<FileSwipeLockEvent>
_fileSwipeLockEventSubscription;
- bool isPanorama = false;
int? lastFileGenID;
@override
@@ -73,26 +72,16 @@ class FileBottomBarState extends State<FileBottomBar> {
@override
Widget build(BuildContext context) {
if (flagService.internalUser) {
- isPanorama = widget.file.isPanorama() ?? false;
- _checkPanorama();
+ if (widget.file.canBePanorama()) {
+ lastFileGenID = widget.file.generatedID;
+ if (lastFileGenID != widget.file.generatedID) {
+ guardedCheckPanorama(widget.file).ignore();
+ }
+ }
}
return _getBottomBar();
}
- // _checkPanorama() method is used to check if the file is a panorama image.
- // This handles the case when the the file dims (width and height) are not available.
- Future<void> _checkPanorama() async {
- if (lastFileGenID == widget.file.generatedID) {
- return;
- }
- lastFileGenID = widget.file.generatedID;
- final result = await checkIfPanorama(widget.file);
- if (mounted && isPanorama == !result) {
- isPanorama = result;
- setState(() {});
- }
- }
-
void safeRefresh() {
if (mounted) {
setState(() {});
@@ -175,25 +164,6 @@ class FileBottomBarState extends State<FileBottomBar> {
);
}
- if (isPanorama) {
- children.add(
- Tooltip(
- message: S.of(context).panorama,
- child: Padding(
- padding: const EdgeInsets.only(top: 12, bottom: 12),
- child: IconButton(
- icon: const Icon(
- Icons.threesixty,
- color: Colors.white,
- ),
- onPressed: () async {
- await openPanoramaViewerPage(widget.file);
- },
- ),
- ),
- ),
- );
- }
children.add(
Tooltip(
message: S.of(context).share,
@@ -227,59 +197,92 @@ class FileBottomBarState extends State<FileBottomBar> {
curve: Curves.easeInOut,
child: Align(
alignment: Alignment.bottomCenter,
- child: Container(
- decoration: BoxDecoration(
- gradient: LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [
- Colors.transparent,
- Colors.black.withOpacity(0.6),
- Colors.black.withOpacity(0.72),
- ],
- stops: const [0, 0.8, 1],
- ),
- ),
- child: Padding(
- padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- widget.file.caption?.isNotEmpty ?? false
- ? Padding(
- padding: const EdgeInsets.fromLTRB(
- 16,
- 12,
- 16,
- 0,
- ),
- child: GestureDetector(
- onTap: () async {
- await _displayDetails(widget.file);
- await Future.delayed(
- const Duration(milliseconds: 500),
- ); //Waiting for some time till the caption gets updated in db if the user closes the bottom sheet without pressing 'done'
- safeRefresh();
- },
- child: Text(
- widget.file.caption!,
- overflow: TextOverflow.ellipsis,
- maxLines: 1,
- style: getEnteTextTheme(context)
- .mini
- .copyWith(color: textBaseDark),
- textAlign: TextAlign.center,
- ),
- ),
- )
- : const SizedBox.shrink(),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: children,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ if (widget.file.isPanorama() == true)
+ Align(
+ alignment: Alignment.centerRight,
+ child: Tooltip(
+ message: S.of(context).panorama,
+ child: Padding(
+ padding: const EdgeInsets.only(
+ top: 12,
+ bottom: 12,
+ right: 20,
+ ),
+ child: IconButton(
+ style: IconButton.styleFrom(
+ backgroundColor: const Color(0xFF252525),
+ fixedSize: const Size(44, 44),
+ ),
+ icon: const Icon(
+ Icons.vrpano_outlined,
+ color: Colors.white,
+ size: 26,
+ ),
+ onPressed: () async {
+ await openPanoramaViewerPage(widget.file);
+ },
+ ),
+ ),
),
- ],
+ ),
+ Container(
+ decoration: BoxDecoration(
+ gradient: LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ Colors.transparent,
+ Colors.black.withOpacity(0.6),
+ Colors.black.withOpacity(0.72),
+ ],
+ stops: const [0, 0.8, 1],
+ ),
+ ),
+ child: Padding(
+ padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ widget.file.caption?.isNotEmpty ?? false
+ ? Padding(
+ padding: const EdgeInsets.fromLTRB(
+ 16,
+ 12,
+ 16,
+ 0,
+ ),
+ child: GestureDetector(
+ onTap: () async {
+ await _displayDetails(widget.file);
+ await Future.delayed(
+ const Duration(milliseconds: 500),
+ ); //Waiting for some time till the caption gets updated in db if the user closes the bottom sheet without pressing 'done'
+ safeRefresh();
+ },
+ child: Text(
+ widget.file.caption!,
+ overflow: TextOverflow.ellipsis,
+ maxLines: 1,
+ style: getEnteTextTheme(context)
+ .mini
+ .copyWith(color: textBaseDark),
+ textAlign: TextAlign.center,
+ ),
+ ),
+ )
+ : const SizedBox.shrink(),
+ Row(
+ mainAxisAlignment: MainAxisAlignment.spaceAround,
+ children: children,
+ ),
+ ],
+ ),
+ ),
),
- ),
+ ],
),
),
),
diff --git a/mobile/lib/ui/viewer/file/panorama_viewer_screen.dart b/mobile/lib/ui/viewer/file/panorama_viewer_screen.dart
index fe5c94652f..4b05bbd6f9 100644
--- a/mobile/lib/ui/viewer/file/panorama_viewer_screen.dart
+++ b/mobile/lib/ui/viewer/file/panorama_viewer_screen.dart
@@ -1,9 +1,13 @@
+import "dart:async";
import "dart:io";
import "package:flutter/material.dart";
+import "package:flutter/services.dart";
+import "package:motion_photos/src/xmp_extractor.dart";
import "package:panorama_viewer/panorama_viewer.dart";
+import "package:photos/generated/l10n.dart";
-class PanoramaViewerScreen extends StatelessWidget {
+class PanoramaViewerScreen extends StatefulWidget {
const PanoramaViewerScreen({
super.key,
required this.file,
@@ -11,26 +15,173 @@ class PanoramaViewerScreen extends StatelessWidget {
final File file;
+ @override
+ State<PanoramaViewerScreen> createState() => _PanoramaViewerScreenState();
+}
+
+class _PanoramaViewerScreenState extends State<PanoramaViewerScreen> {
+ double width = 1.0;
+ double height = 1.0;
+ Rect croppedRect = const Rect.fromLTWH(0.0, 0.0, 1.0, 1.0);
+ SensorControl control = SensorControl.none;
+ Timer? timer;
+ bool isVisible = true;
+
+ @override
+ void initState() {
+ initTimer();
+ init();
+ super.initState();
+ }
+
+ void initTimer() {
+ timer = Timer(const Duration(seconds: 5), () {
+ setState(() {
+ isVisible = false;
+ });
+ SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
+ });
+ SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
+ }
+
+ Future<void> init() async {
+ final data = XMPExtractor().extract(widget.file.readAsBytesSync());
+ double? cWidth =
+ double.tryParse(data["GPano:CroppedAreaImageWidthPixels"] ?? "");
+ double? cHeight =
+ double.tryParse(data["GPano:CroppedAreaImageHeightPixels"] ?? "");
+ double? fWidth = double.tryParse(data["GPano:FullPanoWidthPixels"] ?? "");
+ double? fHeight = double.tryParse(data["GPano:FullPanoHeightPixels"] ?? "");
+ double? cLeft = double.tryParse(data["GPano:CroppedAreaLeftPixels"] ?? "");
+ double? cTop = double.tryParse(data["GPano:CroppedAreaTopPixels"] ?? "");
+
+ // handle missing `fullPanoHeight` (e.g. Samsung camera app panorama mode)
+ if (fHeight == null && fWidth != null && cHeight != null) {
+ fHeight = (fWidth / 2).round().toDouble();
+ cTop = ((fHeight - cHeight) / 2).round().toDouble();
+ }
+
+ // handle inconsistent sizing (e.g. rotated image taken with OnePlus EB2103)
+ if (cHeight != null && fWidth != null && fHeight != null) {
+ final croppedOrientation =
+ cWidth! > cHeight ? Orientation.landscape : Orientation.portrait;
+ final fullOrientation =
+ fWidth > fHeight ? Orientation.landscape : Orientation.portrait;
+ var inconsistent = false;
+ if (croppedOrientation != fullOrientation) {
+ // inconsistent orientation
+ inconsistent = true;
+ final tmp = cHeight;
+ cHeight = cWidth;
+ cWidth = tmp;
+ }
+
+ if (cWidth > fWidth) {
+ // inconsistent full/cropped width
+ inconsistent = true;
+ final tmp = fWidth;
+ fWidth = cWidth;
+ cWidth = tmp;
+ }
+
+ if (cHeight > fHeight) {
+ // inconsistent full/cropped height
+ inconsistent = true;
+ final tmp = cHeight;
+ cHeight = fHeight;
+ fHeight = tmp;
+ }
+
+ if (inconsistent) {
+ cLeft = ((fWidth - cWidth) ~/ 2).toDouble();
+ cTop = ((fHeight - cHeight) ~/ 2).toDouble();
+ }
+ }
+
+ Rect? croppedAreaRect;
+ if (cLeft != null && cTop != null && cWidth != null && cHeight != null) {
+ croppedAreaRect = Rect.fromLTWH(
+ cLeft.toDouble(),
+ cTop.toDouble(),
+ cWidth.toDouble(),
+ cHeight.toDouble(),
+ );
+ }
+
+ if (croppedAreaRect == null || fWidth == null || fHeight == null) return;
+ width = fWidth.toDouble();
+ height = fHeight.toDouble();
+ croppedRect = croppedAreaRect;
+
+ setState(() {});
+ }
+
@override
Widget build(BuildContext context) {
return Scaffold(
- appBar: AppBar(
- elevation: 0, // Remove shadow
- leading: IconButton(
- icon: const Icon(
- Icons.arrow_back,
- size: 18,
+ body: Stack(
+ children: [
+ PanoramaViewer(
+ onTap: (_, __, ___) {
+ setState(() {
+ if (isVisible) {
+ timer?.cancel();
+ SystemChrome.setEnabledSystemUIMode(
+ SystemUiMode.immersiveSticky,
+ );
+ } else {
+ initTimer();
+ }
+ isVisible = !isVisible;
+ });
+ },
+ croppedArea: croppedRect,
+ croppedFullWidth: width,
+ croppedFullHeight: height,
+ sensorControl: control,
+ child: Image.file(
+ widget.file,
+ ),
),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ),
- body: PanoramaViewer(
- child: Image.file(
- file,
- fit: BoxFit.cover,
- ),
+ Visibility(
+ visible: isVisible,
+ child: Align(
+ alignment: Alignment.bottomRight,
+ child: Tooltip(
+ message: S.of(context).panorama,
+ child: Padding(
+ padding: const EdgeInsets.only(
+ top: 12,
+ bottom: 32,
+ right: 20,
+ ),
+ child: IconButton(
+ style: IconButton.styleFrom(
+ backgroundColor: const Color(0xFF252525),
+ fixedSize: const Size(44, 44),
+ ),
+ icon: Icon(
+ Icons.explore,
+ color: control != SensorControl.none
+ ? Colors.white
+ : Colors.white.withOpacity(0.4),
+ size: 26,
+ ),
+ onPressed: () async {
+ if (control != SensorControl.none) {
+ control = SensorControl.none;
+ } else {
+ control = SensorControl.orientation;
+ }
+
+ setState(() {});
+ },
+ ),
+ ),
+ ),
+ ),
+ ),
+ ],
),
);
}
diff --git a/mobile/lib/utils/exif_util.dart b/mobile/lib/utils/exif_util.dart
index d9bac85e6d..8243b3a92f 100644
--- a/mobile/lib/utils/exif_util.dart
+++ b/mobile/lib/utils/exif_util.dart
@@ -102,6 +102,12 @@ Future<FFProbeProps?> getVideoPropsAsync(File originalFile) async {
}
}
+bool? checkPanoramaFromEXIF(File? file, Map<String, IfdTag>? exifData) {
+ final element = exifData?["EXIF CustomRendered"];
+ if (element?.printable == null) return null;
+ return element?.printable == "6";
+}
+
Future<DateTime?> getCreationTimeFromEXIF(
File? file,
Map<String, IfdTag>? exifData,
diff --git a/mobile/lib/utils/file_uploader.dart b/mobile/lib/utils/file_uploader.dart
index a5750f6b7a..6e81e5acc5 100644
--- a/mobile/lib/utils/file_uploader.dart
+++ b/mobile/lib/utils/file_uploader.dart
@@ -675,6 +675,8 @@ class FileUploader {
(mediaUploadData.width ?? 0) != 0) {
pubMetadata[heightKey] = mediaUploadData.height;
pubMetadata[widthKey] = mediaUploadData.width;
+ pubMetadata[mediaTypeKey] =
+ mediaUploadData.isPanorama == true ? 1 : 0;
}
if (mediaUploadData.motionPhotoStartIndex != null) {
pubMetadata[motionVideoIndexKey] =
diff --git a/mobile/lib/utils/file_uploader_util.dart b/mobile/lib/utils/file_uploader_util.dart
index ef4f042b7f..e443b1ad8c 100644
--- a/mobile/lib/utils/file_uploader_util.dart
+++ b/mobile/lib/utils/file_uploader_util.dart
@@ -44,6 +44,8 @@ class MediaUploadData {
// For iOS, this value will be always null.
final int? motionPhotoStartIndex;
+ bool? isPanorama;
+
MediaUploadData(
this.sourceFile,
this.thumbnail,
@@ -52,6 +54,7 @@ class MediaUploadData {
this.height,
this.width,
this.motionPhotoStartIndex,
+ this.isPanorama,
});
}
diff --git a/mobile/lib/utils/panorama_util.dart b/mobile/lib/utils/panorama_util.dart
index d4fee93669..c002873185 100644
--- a/mobile/lib/utils/panorama_util.dart
+++ b/mobile/lib/utils/panorama_util.dart
@@ -1,7 +1,10 @@
-import "package:flutter/painting.dart";
+import "package:motion_photos/src/xmp_extractor.dart";
import "package:photos/models/file/extensions/file_props.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/file/file_type.dart";
+import "package:photos/models/metadata/file_magic.dart";
+import "package:photos/services/file_magic_service.dart";
+import "package:photos/utils/exif_util.dart";
import "package:photos/utils/file_util.dart";
/// Check if the file is a panorama image.
@@ -9,20 +12,49 @@ Future<bool> checkIfPanorama(EnteFile enteFile) async {
if (enteFile.fileType != FileType.image) {
return false;
}
- if (enteFile.isPanorama() != null) {
- return enteFile.isPanorama()!;
- }
final file = await getFile(enteFile);
if (file == null) {
return false;
}
+ try {
+ final result = XMPExtractor().extract(file.readAsBytesSync());
+ if (result["GPano:ProjectionType"] == "cylindrical" ||
+ result["GPano:ProjectionType"] == "equirectangular") {
+ return true;
+ }
+ } catch (_) {}
+
+ final result = await readExifAsync(file);
+
+ final element = result["EXIF CustomRendered"];
+ return element?.printable == "6";
+}
+
+bool? checkPanoramaFromXMP(Map<String, dynamic> xmpData) {
+ if (xmpData["GPano:ProjectionType"] == "cylindrical" ||
+ xmpData["GPano:ProjectionType"] == "equirectangular") {
+ return true;
+ }
+ return false;
+}
+
+// guardedCheckPanorama() method is used to check if the file is a panorama image.
+Future<void> guardedCheckPanorama(EnteFile file) async {
+ if (file.isPanorama() != null) {
+ return;
+ }
+ final result = await checkIfPanorama(file);
+
+ // Update the metadata if it is not updated
+ if (file.isPanorama() == null && file.canEditMetaInfo) {
+ int? mediaType = file.pubMagicMetadata?.mediaType;
+ mediaType ??= 0;
- final image = await decodeImageFromList(await file.readAsBytes());
- final width = image.width.toDouble();
- final height = image.height.toDouble();
+ mediaType = mediaType | (result ? 1 : 0);
- if (height > width) {
- return height / width >= 2.0;
+ FileMagicService.instance.updatePublicMagicMetadata(
+ [file],
+ {mediaTypeKey: mediaType},
+ ).ignore();
}
- return width / height >= 2.0;
}