localFile function

Future<File> localFile(
  1. String name, [
  2. String? doc
])

Implementation

Future<File> localFile(String name, [String? doc]) async {
  // allow nested relative paths (e.g. per inspection) and create dirs if needed
  final basePath = await localPath;
  var p0 = File('$basePath/$name');
  // keep legacy behaviour only for flat names; otherwise preserve folders
  final hasFolder = name.contains('/');

  if (doc != null) {
    await p0.parent.create(recursive: true);
    return p0;
  }

  if (await p0.exists()) return p0;
  if (hasFolder) {
    // if the file has no extension, prefer a .jpg to keep it recognizable
    final baseName = p0.uri.pathSegments.last;
    if (!baseName.contains('.')) {
      final withExt = File('$basePath/$name.jpg');
      return withExt;
    }
    await p0.parent.create(recursive: true);
    return p0;
  }

  final sanitized =
      name.replaceAll(RegExp(r'[^\w]+'), '_'); // legacy naming fallback
  final p1 = File('$basePath/$sanitized.img');
  if (await p1.exists() || useOldImgEncoding) {
    await p1.parent.create(recursive: true);
    return p1;
  }
  final p2 = File('$basePath/$sanitized.maybe.jpg');
  await p2.parent.create(recursive: true);
  return p2;
}