mirror of
https://github.com/immich-app/immich.git
synced 2025-03-01 15:11:21 +01:00
fix(mobile): do not crash on malformed asset duration (#1921)
* fix(mobile): do not crash on malformed asset duration * add unit test
This commit is contained in:
parent
f52e076cb3
commit
9ac087c59c
3 changed files with 29 additions and 5 deletions
|
@ -19,7 +19,8 @@ class Asset {
|
||||||
fileCreatedAt = DateTime.parse(remote.fileCreatedAt).toUtc(),
|
fileCreatedAt = DateTime.parse(remote.fileCreatedAt).toUtc(),
|
||||||
fileModifiedAt = DateTime.parse(remote.fileModifiedAt).toUtc(),
|
fileModifiedAt = DateTime.parse(remote.fileModifiedAt).toUtc(),
|
||||||
updatedAt = DateTime.parse(remote.updatedAt).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),
|
fileName = p.basename(remote.originalPath),
|
||||||
height = remote.exifInfo?.exifImageHeight?.toInt(),
|
height = remote.exifInfo?.exifImageHeight?.toInt(),
|
||||||
width = remote.exifInfo?.exifImageWidth?.toInt(),
|
width = remote.exifInfo?.exifImageWidth?.toInt(),
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
extension DurationExtension on String {
|
extension DurationExtension on String {
|
||||||
Duration toDuration() {
|
Duration? toDuration() {
|
||||||
final parts =
|
try {
|
||||||
split(':').map((e) => double.parse(e).toInt()).toList(growable: false);
|
final parts = split(':')
|
||||||
return Duration(hours: parts[0], minutes: parts[1], seconds: parts[2]);
|
.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() {
|
double toDouble() {
|
||||||
|
|
18
mobile/test/builtin_extensions_text.dart
Normal file
18
mobile/test/builtin_extensions_text.dart
Normal file
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue