downloadImages method

Future<void> downloadImages({
  1. required String parentDirectory,
  2. required Map<String, String> imageUrls,
  3. required String imageFormat,
  4. required String sectionName,
  5. required List<Map<String, String>> assets,
  6. required double scale,
  7. required bool forceDownload,
})

Fetches images from image URLs

Implementation

Future<void> downloadImages({
  required String parentDirectory,
  required Map<String, String> imageUrls,
  required String imageFormat,
  required String sectionName,
  required List<Map<String, String>> assets,
  required double scale,
  required bool forceDownload,
}) async {
  final subfolderName = formatScale(scale);
  String scaleFolder = scale == 1.0
      ? '$parentDirectory/$sectionName'
      : '$parentDirectory/$sectionName/$subfolderName';

  Directory(scaleFolder).createSync(recursive: true);

  for (var asset in assets) {
    String nodeId = asset['id']!;
    String imageName = asset['name']!.replaceAll(RegExp(r'[^\w.-]'), '_');
    String imageUrl = imageUrls[nodeId] ?? '';

    if (imageUrl.isEmpty) continue;

    String filePath = '$scaleFolder/$imageName.$imageFormat';

    if (!forceDownload && await File(filePath).exists()) {
      logger.log('⚡ Skipped (already exists): $filePath');
      continue;
    }

    try {
      var response = await dio.get(imageUrl,
          options: Options(responseType: ResponseType.bytes));
      await File(filePath).writeAsBytes(response.data);
      logger.log('✅ Downloaded: $filePath');
    } catch (e) {
      logger.log('❌ Error downloading image $imageName: $e');
    }
  }
}