92 lines
2.3 KiB
Dart
92 lines
2.3 KiB
Dart
import 'package:equatable/equatable.dart';
|
|
|
|
class CustomerFileModel extends Equatable {
|
|
final String? id;
|
|
final String customerId; // Riferimento UUID
|
|
final String name;
|
|
final String storagePath;
|
|
final String extension;
|
|
final DateTime? createdAt;
|
|
final int fileSize;
|
|
|
|
const CustomerFileModel({
|
|
this.id,
|
|
required this.customerId,
|
|
required this.name,
|
|
required this.storagePath,
|
|
required this.extension,
|
|
this.createdAt,
|
|
required this.fileSize,
|
|
});
|
|
|
|
// Trasforma i byte in qualcosa di leggibile (KB, MB, GB)
|
|
String get sizeFormatted {
|
|
if (fileSize <= 0) return "0 B";
|
|
const suffixes = ["B", "KB", "MB", "GB", "TB"];
|
|
var i = (fileSize.toString().length - 1) ~/ 3;
|
|
if (i >= suffixes.length) i = suffixes.length - 1;
|
|
double num = fileSize / (1 << (i * 10));
|
|
return "${num.toStringAsFixed(i == 0 ? 0 : 1)} ${suffixes[i]}";
|
|
}
|
|
|
|
bool get isPdf => extension.toLowerCase().replaceAll('.', '') == 'pdf';
|
|
|
|
CustomerFileModel copyWith({
|
|
String? id,
|
|
String? customerId,
|
|
String? name,
|
|
String? storagePath,
|
|
String? extension,
|
|
DateTime? createdAt,
|
|
int? fileSize,
|
|
}) {
|
|
return CustomerFileModel(
|
|
id: id ?? this.id,
|
|
customerId: customerId ?? this.customerId,
|
|
name: name ?? this.name,
|
|
storagePath: storagePath ?? this.storagePath,
|
|
extension: extension ?? this.extension,
|
|
createdAt: createdAt ?? this.createdAt,
|
|
fileSize: fileSize ?? this.fileSize,
|
|
);
|
|
}
|
|
|
|
factory CustomerFileModel.fromMap(Map<String, dynamic> map) {
|
|
return CustomerFileModel(
|
|
id: map['id'] as String,
|
|
customerId: map['customer_id'],
|
|
name: map['name'],
|
|
storagePath: map['storage_path'],
|
|
extension: map['extension'] ?? '',
|
|
createdAt: map['created_at'] != null
|
|
? DateTime.parse(map['created_at'])
|
|
: null,
|
|
fileSize: map['file_size'] is int
|
|
? map['file_size']
|
|
: int.tryParse(map['file_size']?.toString() ?? '0') ?? 0,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
if (id != null) 'id': id,
|
|
'customer_id': customerId,
|
|
'name': name,
|
|
'storage_path': storagePath,
|
|
'extension': extension,
|
|
'file_size': fileSize,
|
|
};
|
|
}
|
|
|
|
@override
|
|
List<Object?> get props => [
|
|
id,
|
|
customerId,
|
|
name,
|
|
storagePath,
|
|
extension,
|
|
createdAt,
|
|
fileSize,
|
|
];
|
|
}
|