createCachePath static method

Future<String> createCachePath([
  1. String? cacheDir
])

Creates and returns the path to the cache directory.

If cacheDir is provided and not empty, it will be appended to the root cache path. The method ensures the directory exists, creating it recursively if necessary.

Returns a Future that completes with the cache directory path.

Implementation

static Future<String> createCachePath([String? cacheDir]) async {
  String rootPath = _cacheRootPath;
  // Initialize the root path if it is empty.
  if (rootPath.isEmpty) {
    rootPath = (await getApplicationCacheDirectory()).path;
    _cacheRootPath = rootPath;
  }
  // Append 'videos' to the root path.
  rootPath = '$rootPath/videos';
  // Append the custom cache directory if provided.
  if (cacheDir != null && cacheDir.isNotEmpty) {
    rootPath = '$rootPath/$cacheDir';
  }
  // Create the directory if it does not exist.
  if (Directory(rootPath).existsSync()) return rootPath;
  Directory(rootPath).createSync(recursive: true);
  return rootPath;
}