98 lines
2.7 KiB
Dart
98 lines
2.7 KiB
Dart
// lib/features/igel/domain/igel_image.dart
|
||
class IgelImage {
|
||
final int id;
|
||
final String url;
|
||
final String? thumbUrl;
|
||
final String? originalName;
|
||
final String? mime;
|
||
final int? sizeBytes;
|
||
|
||
/// Upload-Zeit auf dem Server (DB: created_at)
|
||
final DateTime? createdAt;
|
||
|
||
/// Aufnahmezeit laut EXIF (DB: taken_at) – bevorzugt für die Anzeige
|
||
final DateTime? takenAt;
|
||
|
||
const IgelImage({
|
||
required this.id,
|
||
required this.url,
|
||
this.thumbUrl,
|
||
this.originalName,
|
||
this.mime,
|
||
this.sizeBytes,
|
||
this.createdAt,
|
||
this.takenAt,
|
||
});
|
||
|
||
static DateTime? _parseDate(dynamic v) {
|
||
if (v == null) return null;
|
||
if (v is DateTime) return v;
|
||
if (v is String && v.isNotEmpty) {
|
||
// akzeptiere "YYYY-MM-DD HH:MM:SS" (MySQL) und ISO-8601
|
||
final s1 = DateTime.tryParse(v);
|
||
if (s1 != null) return s1;
|
||
// MySQL „YYYY-MM-DD HH:MM:SS“ -> „YYYY-MM-DDTHH:MM:SS“
|
||
final s2 = DateTime.tryParse(v.replaceFirst(' ', 'T'));
|
||
if (s2 != null) return s2;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
factory IgelImage.fromMap(Map<String, dynamic> m) {
|
||
return IgelImage(
|
||
id: (m['id'] as num).toInt(),
|
||
url: (m['url'] ?? '') as String,
|
||
thumbUrl: (m['thumb_url'] ?? m['thumbUrl']) as String?,
|
||
originalName: m['original_name'] as String?,
|
||
mime: m['mime'] as String?,
|
||
sizeBytes: (m['size_bytes'] as num?)?.toInt(),
|
||
createdAt: _parseDate(m['created_at']),
|
||
takenAt: _parseDate(m['taken_at']),
|
||
);
|
||
}
|
||
|
||
Map<String, dynamic> toMap() => {
|
||
'id': id,
|
||
'url': url,
|
||
if (thumbUrl != null) 'thumb_url': thumbUrl,
|
||
if (originalName != null) 'original_name': originalName,
|
||
if (mime != null) 'mime': mime,
|
||
if (sizeBytes != null) 'size_bytes': sizeBytes,
|
||
if (createdAt != null) 'created_at': createdAt!.toIso8601String(),
|
||
if (takenAt != null) 'taken_at': takenAt!.toIso8601String(),
|
||
};
|
||
|
||
IgelImage copyWith({
|
||
int? id,
|
||
String? url,
|
||
String? thumbUrl,
|
||
String? originalName,
|
||
String? mime,
|
||
int? sizeBytes,
|
||
DateTime? createdAt,
|
||
DateTime? takenAt,
|
||
}) {
|
||
return IgelImage(
|
||
id: id ?? this.id,
|
||
url: url ?? this.url,
|
||
thumbUrl: thumbUrl ?? this.thumbUrl,
|
||
originalName: originalName ?? this.originalName,
|
||
mime: mime ?? this.mime,
|
||
sizeBytes: sizeBytes ?? this.sizeBytes,
|
||
createdAt: createdAt ?? this.createdAt,
|
||
takenAt: takenAt ?? this.takenAt,
|
||
);
|
||
}
|
||
|
||
bool get isVideo {
|
||
final m = mime?.toLowerCase();
|
||
return m != null && m.startsWith('video/');
|
||
}
|
||
|
||
bool get isImage {
|
||
final m = mime?.toLowerCase();
|
||
// Wenn MIME fehlt, lieber wie bisher als Bild behandeln.
|
||
return m == null || m.startsWith('image/');
|
||
}
|
||
}
|