ensureImageExtension static method

String ensureImageExtension(
  1. String filePath,
  2. String? mimeType
)

根据文件内容识别出的格式,返回可直接用于保存的图片路径。

Implementation

static String ensureImageExtension(String filePath, String? mimeType) {
  if (mimeType == null || mimeType.isEmpty) return filePath;

  final normalized = mimeType.toLowerCase();
  final lowerPath = filePath.toLowerCase();
  final hasExpectedExtension = normalized == 'jpeg'
      ? lowerPath.endsWith('.jpg') || lowerPath.endsWith('.jpeg')
      : lowerPath.endsWith('.$normalized');
  if (hasExpectedExtension) return filePath;
  return '$filePath.$normalized';
}