1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-26 10:50:29 +02:00

feat(Android): guard against missing EXIF info (#2965)

This commit is contained in:
Fynn Petersen-Frey 2023-06-26 17:27:32 +02:00 committed by GitHub
parent 454fb106d2
commit 99f85fb359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 16 deletions

View File

@ -16,7 +16,9 @@ import 'package:immich_mobile/shared/providers/db.provider.dart';
import 'package:immich_mobile/shared/services/api.service.dart'; import 'package:immich_mobile/shared/services/api.service.dart';
import 'package:immich_mobile/utils/files_helper.dart'; import 'package:immich_mobile/utils/files_helper.dart';
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:logging/logging.dart';
import 'package:openapi/api.dart'; import 'package:openapi/api.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:photo_manager/photo_manager.dart'; import 'package:photo_manager/photo_manager.dart';
import 'package:http_parser/http_parser.dart'; import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
@ -33,6 +35,7 @@ class BackupService {
final httpClient = http.Client(); final httpClient = http.Client();
final ApiService _apiService; final ApiService _apiService;
final Isar _db; final Isar _db;
final Logger _log = Logger("BackupService");
BackupService(this._apiService, this._db); BackupService(this._apiService, this._db);
@ -203,6 +206,14 @@ class BackupService {
Function(CurrentUploadAsset) setCurrentUploadAssetCb, Function(CurrentUploadAsset) setCurrentUploadAssetCb,
Function(ErrorUploadAsset) errorCb, Function(ErrorUploadAsset) errorCb,
) async { ) async {
if (Platform.isAndroid &&
!(await Permission.accessMediaLocation.status).isGranted) {
// double check that permission is granted here, to guard against
// uploading corrupt assets without EXIF information
_log.warning("Media location permission is not granted. "
"Cannot access original assets for backup.");
return false;
}
final String deviceId = Store.get(StoreKey.deviceId); final String deviceId = Store.get(StoreKey.deviceId);
final String savedEndpoint = Store.get(StoreKey.serverEndpoint); final String savedEndpoint = Store.get(StoreKey.serverEndpoint);
File? file; File? file;

View File

@ -16,19 +16,20 @@ class GalleryPermissionNotifier extends StateNotifier<PermissionStatus> {
/// Requests the gallery permission /// Requests the gallery permission
Future<PermissionStatus> requestGalleryPermission() async { Future<PermissionStatus> requestGalleryPermission() async {
PermissionStatus result;
// Android 32 and below uses Permission.storage // Android 32 and below uses Permission.storage
if (Platform.isAndroid) { if (Platform.isAndroid) {
final androidInfo = await DeviceInfoPlugin().androidInfo; final androidInfo = await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt <= 32) { if (androidInfo.version.sdkInt <= 32) {
// Android 32 and below need storage // Android 32 and below need storage
final permission = await Permission.storage.request(); final permission = await Permission.storage.request();
state = permission; result = permission;
return permission;
} else { } else {
// Android 33 need photo & video // Android 33 need photo & video
final photos = await Permission.photos.request(); final photos = await Permission.photos.request();
if (!photos.isGranted) { if (!photos.isGranted) {
// Don't ask twice for the same permission // Don't ask twice for the same permission
state = photos;
return photos; return photos;
} }
final videos = await Permission.videos.request(); final videos = await Permission.videos.request();
@ -45,28 +46,32 @@ class GalleryPermissionNotifier extends StateNotifier<PermissionStatus> {
status = PermissionStatus.denied; status = PermissionStatus.denied;
} }
state = status; result = status;
return status; }
if (result == PermissionStatus.granted &&
androidInfo.version.sdkInt >= 29) {
result = await Permission.accessMediaLocation.request();
} }
} else { } else {
// iOS can use photos // iOS can use photos
final photos = await Permission.photos.request(); final photos = await Permission.photos.request();
state = photos; result = photos;
return photos;
} }
state = result;
return result;
} }
/// Checks the current state of the gallery permissions without /// Checks the current state of the gallery permissions without
/// requesting them again /// requesting them again
Future<PermissionStatus> getGalleryPermissionStatus() async { Future<PermissionStatus> getGalleryPermissionStatus() async {
PermissionStatus result;
// Android 32 and below uses Permission.storage // Android 32 and below uses Permission.storage
if (Platform.isAndroid) { if (Platform.isAndroid) {
final androidInfo = await DeviceInfoPlugin().androidInfo; final androidInfo = await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt <= 32) { if (androidInfo.version.sdkInt <= 32) {
// Android 32 and below need storage // Android 32 and below need storage
final permission = await Permission.storage.status; final permission = await Permission.storage.status;
state = permission; result = permission;
return permission;
} else { } else {
// Android 33 needs photo & video // Android 33 needs photo & video
final photos = await Permission.photos.status; final photos = await Permission.photos.status;
@ -84,18 +89,23 @@ class GalleryPermissionNotifier extends StateNotifier<PermissionStatus> {
status = PermissionStatus.denied; status = PermissionStatus.denied;
} }
state = status; result = status;
return status; }
if (state == PermissionStatus.granted &&
androidInfo.version.sdkInt >= 29) {
result = await Permission.accessMediaLocation.status;
} }
} else { } else {
// iOS can use photos // iOS can use photos
final photos = await Permission.photos.status; final photos = await Permission.photos.status;
state = photos; result = photos;
return photos;
} }
state = result;
return result;
} }
} }
final galleryPermissionNotifier final galleryPermissionNotifier =
= StateNotifierProvider<GalleryPermissionNotifier, PermissionStatus> StateNotifierProvider<GalleryPermissionNotifier, PermissionStatus>(
((ref) => GalleryPermissionNotifier()); (ref) => GalleryPermissionNotifier(),
);