decode static method

Future<Comics> decode(
  1. Uint8List bytes, {
  2. String? extractTo,
})

Decodes .comics bytes: the contract reader validates the archive (entry names, data.json schema) first; with extractTo every entry is written below that directory. Throws FreedomeException (ValidationException / UnsupportedFormatException) for a file the contract refuses.

Implementation

static Future<Comics> decode(Uint8List bytes, {String? extractTo}) async {
  final doc = (await fc.ComicsCodec.decode(bytes)).value;
  if (doc.variant != 'data') {
    throw const fc.UnsupportedFormatException(
      'legacy image-only .comics: open it in the player (no data.json to edit)',
    );
  }
  final archive = ZipDecoder().decodeBytes(bytes);
  Comics? comics;
  for (final f in archive) {
    if (!f.isFile) continue;
    if (f.name == 'data.json') {
      comics = Comics.fromJson(
        jsonDecode(utf8.decode(f.content)) as Map<String, dynamic>,
      );
    }
    if (extractTo != null && isSafeEntryName(f.name)) {
      final out = File(p.join(extractTo, f.name));
      await out.parent.create(recursive: true);
      await out.writeAsBytes(f.content);
    }
  }
  return comics!;
}