Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] Sanitize Android video gallery titles (#11396)

neeraj-pilota847a5a12c4c
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart b/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart
index 61702b87c8..9feca546d1 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart
@@ -32,6 +32,7 @@ import "package:photos/ui/tools/editor/video_editor/video_editor_player_control.
import "package:photos/ui/tools/editor/video_rotate_page.dart";
import "package:photos/ui/tools/editor/video_trim_page.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
+import "package:photos/utils/gallery_save_title.dart";
import "package:video_editor/video_editor.dart";
class VideoEditorPage extends StatefulWidget {
@@ -554,6 +555,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
"_edited_" +
DateTime.now().microsecondsSinceEpoch.toString() +
".mp4";
+ final galleryTitle = await getMediaStoreCompatibleTitle(fileName);
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
@@ -562,7 +564,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
try {
final AssetEntity newAsset = await (PhotoManager.editor.saveVideo(
result,
- title: fileName,
+ title: galleryTitle,
));
result.deleteSync();
diff --git a/mobile/apps/photos/lib/utils/file_download_util.dart b/mobile/apps/photos/lib/utils/file_download_util.dart
index 8299672e50..9a6a9d1e0a 100644
--- a/mobile/apps/photos/lib/utils/file_download_util.dart
+++ b/mobile/apps/photos/lib/utils/file_download_util.dart
@@ -27,6 +27,7 @@ import "package:photos/services/sync/local_sync_service.dart";
import "package:photos/utils/apple_photos_errors.dart";
import "package:photos/utils/file_key.dart";
import "package:photos/utils/file_util.dart";
+import "package:photos/utils/gallery_save_title.dart";
final _logger = Logger("file_download_util");
@@ -362,6 +363,7 @@ Future<void> downloadToGallery(
throw DownloadFailedError("Unable to fetch file for gallery download");
}
final galleryTitle = _getGallerySaveTitle(file, fileToSave.path);
+ final mediaStoreTitle = await getMediaStoreCompatibleTitle(galleryTitle);
// We use a lock to prevent synchronisation to occur while it is downloading
// as this introduces wrong entry in FilesDB due to race condition
// This is a fix for https://github.com/ente/ente/issues/4296
@@ -372,12 +374,12 @@ Future<void> downloadToGallery(
if (type == FileType.image) {
savedAsset = await PhotoManager.editor.saveImageWithPath(
fileToSave.path,
- title: galleryTitle,
+ title: mediaStoreTitle,
);
} else if (type == FileType.video) {
savedAsset = await PhotoManager.editor.saveVideo(
fileToSave,
- title: galleryTitle,
+ title: mediaStoreTitle,
);
} else if (type == FileType.livePhoto) {
final File? liveVideoFile = await getFileFromServer(
@@ -389,7 +391,11 @@ Future<void> downloadToGallery(
throw AssertionError("Live video can not be null");
}
if (downloadLivePhotoOnDroid) {
- await _saveLivePhotoOnDroid(fileToSave, liveVideoFile, file);
+ await _saveLivePhotoOnDroid(
+ fileToSave,
+ liveVideoFile,
+ mediaStoreTitle,
+ );
} else {
savedAsset = await PhotoManager.editor.darwin.saveLivePhoto(
imageFile: fileToSave,
@@ -440,10 +446,9 @@ Future<void> downloadToGallery(
Future<void> _saveLivePhotoOnDroid(
File image,
File video,
- EnteFile enteFile,
+ String imageTitle,
) async {
debugPrint("Downloading LivePhoto on Droid");
- final imageTitle = _getGallerySaveTitle(enteFile, image.path);
AssetEntity? savedAsset =
await (PhotoManager.editor.saveImageWithPath(
image.path,
@@ -458,9 +463,10 @@ Future<void> _saveLivePhotoOnDroid(
"remoteDownload",
);
await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
- final videoTitle =
- file_path.basenameWithoutExtension(imageTitle) +
- file_path.extension(video.path);
+ final videoTitle = await getMediaStoreCompatibleTitle(
+ file_path.basenameWithoutExtension(imageTitle) +
+ file_path.extension(video.path),
+ );
savedAsset = (await (PhotoManager.editor.saveVideo(video, title: videoTitle))
.catchError((err) {
_logger.warning('Failed to save video $videoTitle of live photo');
diff --git a/mobile/apps/photos/lib/utils/gallery_save_title.dart b/mobile/apps/photos/lib/utils/gallery_save_title.dart
new file mode 100644
index 0000000000..de0f97631d
--- /dev/null
+++ b/mobile/apps/photos/lib/utils/gallery_save_title.dart
@@ -0,0 +1,22 @@
+import "package:photos/core/constants.dart";
+import "package:photos/utils/device_info.dart";
+
+Future<String> getMediaStoreCompatibleTitle(String title) async {
+ final sanitizedTitle = _sanitizePreAndroid11MediaStoreTitle(title);
+ if (sanitizedTitle == title) {
+ return title;
+ }
+ if (await isAndroidSDKVersionLowerThan(android11SDKINT)) {
+ return sanitizedTitle;
+ }
+ return title;
+}
+
+String _sanitizePreAndroid11MediaStoreTitle(String title) {
+ final fragmentIndex = title.lastIndexOf("#");
+ if (fragmentIndex < 0 || title.lastIndexOf(".") <= fragmentIndex) {
+ return title;
+ }
+ // The legacy MIME guesser treats # as a URL fragment delimiter.
+ return title.replaceAll("#", "_");
+}