chunkFilesBySize method

List<List<FileInfo>> chunkFilesBySize(
  1. List<FileInfo> files
)

Chunk files by size limit

Implementation

List<List<FileInfo>> chunkFilesBySize(List<FileInfo> files) {
  final List<List<FileInfo>> chunks = <List<FileInfo>>[];
  List<FileInfo> currentChunk = <FileInfo>[];
  int currentSize = 0;
  final maxSize = config.targetSizeKB * 1024;

  for (final file in files) {
    final fileSize = file.contentSize;

    // If adding this file would exceed the limit, start a new chunk
    // (unless the current chunk is empty - we need at least one file per chunk)
    if (currentChunk.isNotEmpty && currentSize + fileSize > maxSize) {
      chunks.add(currentChunk);
      currentChunk = <FileInfo>[];
      currentSize = 0;
    }

    currentChunk.add(file);
    currentSize += fileSize;
  }

  // Don't forget the last chunk
  if (currentChunk.isNotEmpty) {
    chunks.add(currentChunk);
  }

  return chunks;
}