import 'dart:typed_data'; import 'package:equatable/equatable.dart'; class AttachmentModel extends Equatable { final String? id; final DateTime? createdAt; final String? customerId; final String? operationId; final String name; final String extension; final String? storagePath; final int fileSize; final Uint8List? localBytes; final String companyId; const AttachmentModel({ this.id, this.createdAt, this.customerId, this.operationId, required this.name, required this.extension, this.storagePath, required this.fileSize, this.localBytes, required this.companyId, }); @override List get props => [ id, createdAt, customerId, operationId, name, extension, storagePath, fileSize, localBytes, companyId, ]; bool get isLocal => localBytes != null; bool get isPdf => extension.toLowerCase().replaceAll('.', '') == 'pdf'; 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]}"; } AttachmentModel copyWith({ String? id, DateTime? createdAt, String? customerId, String? operationId, String? name, String? extension, String? storagePath, int? fileSize, Uint8List? localBytes, String? companyId, }) => AttachmentModel( id: id ?? this.id, createdAt: createdAt ?? this.createdAt, customerId: customerId ?? this.customerId, operationId: operationId ?? this.operationId, name: name ?? this.name, extension: extension ?? this.extension, storagePath: storagePath ?? this.storagePath, fileSize: fileSize ?? this.fileSize, localBytes: localBytes ?? this.localBytes, companyId: companyId ?? this.companyId, ); factory AttachmentModel.fromMap(Map map) { return AttachmentModel( id: map['id'] as String, createdAt: map['created_at'] != null ? DateTime.parse(map['created_at']) : null, customerId: map['customer_id'] as String?, operationId: map['operation_id'] as String?, name: map['name'] as String, extension: map['extension'] as String, storagePath: map['storage_path'] as String?, fileSize: map['file_size'] is int ? map['file_size'] : int.tryParse(map['file_size']?.toString() ?? '0') ?? 0, companyId: map['company_id'] as String, ); } Map toMap() { return { if (id != null) 'id': id, 'name': name, 'extension': extension, 'storage_path': storagePath, 'customer_id': customerId, 'operation_id': operationId, 'file_size': fileSize, 'company_id': companyId, }; } }