diff --git a/mobile/lib/shared/models/asset.dart b/mobile/lib/shared/models/asset.dart index bd0b797cab..1e6ef07089 100644 --- a/mobile/lib/shared/models/asset.dart +++ b/mobile/lib/shared/models/asset.dart @@ -19,7 +19,8 @@ class Asset { fileCreatedAt = DateTime.parse(remote.fileCreatedAt).toUtc(), fileModifiedAt = DateTime.parse(remote.fileModifiedAt).toUtc(), updatedAt = DateTime.parse(remote.updatedAt).toUtc(), - durationInSeconds = remote.duration.toDuration().inSeconds, + // use -1 as fallback duration (to not mix it up with non-video assets correctly having duration=0) + durationInSeconds = remote.duration.toDuration()?.inSeconds ?? -1, fileName = p.basename(remote.originalPath), height = remote.exifInfo?.exifImageHeight?.toInt(), width = remote.exifInfo?.exifImageWidth?.toInt(), diff --git a/mobile/lib/utils/builtin_extensions.dart b/mobile/lib/utils/builtin_extensions.dart index 1bd3c9dc1b..da02bf8e70 100644 --- a/mobile/lib/utils/builtin_extensions.dart +++ b/mobile/lib/utils/builtin_extensions.dart @@ -1,8 +1,13 @@ extension DurationExtension on String { - Duration toDuration() { - final parts = - split(':').map((e) => double.parse(e).toInt()).toList(growable: false); - return Duration(hours: parts[0], minutes: parts[1], seconds: parts[2]); + Duration? toDuration() { + try { + final parts = split(':') + .map((e) => double.parse(e).toInt()) + .toList(growable: false); + return Duration(hours: parts[0], minutes: parts[1], seconds: parts[2]); + } catch (e) { + return null; + } } double toDouble() { diff --git a/mobile/test/builtin_extensions_text.dart b/mobile/test/builtin_extensions_text.dart new file mode 100644 index 0000000000..4f5e7aa75d --- /dev/null +++ b/mobile/test/builtin_extensions_text.dart @@ -0,0 +1,18 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:immich_mobile/utils/builtin_extensions.dart'; + +void main() { + group('Test toDuration', () { + test('ok', () { + expect( + "1:02:33".toDuration(), + const Duration(hours: 1, minutes: 2, seconds: 33), + ); + }); + test('malformed', () { + expect("".toDuration(), null); + expect("1:2".toDuration(), null); + expect("a:b:c".toDuration(), null); + }); + }); +}