import 'dart:convert'; import '../core/failure.dart'; import '../models/server_profile.dart'; class ProfileTransfer { static const maxBytes = 5 * 1024 * 1024; static List decode(List bytes) { if (bytes.length > maxBytes) throw const Failure('invalidImport'); try { final text = utf8.decode(bytes).replaceFirst(RegExp(r'^\uFEFF'), ''); final data = jsonDecode(text); if (data is! Map || data['format'] != 'chj-server-profiles' || data['version'] != 1 || data['servers'] is! List || data['servers'].isEmpty || data['servers'].length > 10000) { throw const Failure('invalidImport'); } return (data['servers'] as List) .map( (e) => ServerProfile.fromJson( Map.from(e), portable: true, ), ) .toList(); } catch (_) { throw const Failure('invalidImport'); } } static List encode(List profiles) => utf8.encode( const JsonEncoder.withIndent(' ').convert({ 'format': 'chj-server-profiles', 'version': 1, 'servers': profiles.map((p) => p.portable()).toList(), }), ); }