deleteModelFile static method

Future<void> deleteModelFile(
  1. String filename
)

Safely deletes a model file

Implementation

static Future<void> deleteModelFile(String filename) async {
  try {
    // A FileSource install is referenced in place under its (possibly
    // namespaced) filename -> its original path; delete THAT, not the
    // managed-dir path the filename would otherwise map to (which is empty
    // for an externally-referenced companion, so uninstall would leave the
    // real file behind).
    final external = await ServiceRegistry.instance.protectedFilesRegistry
        .getExternalPath(filename);
    final filePath = external ?? await getModelFilePath(filename);
    final file = File(filePath);

    if (await file.exists()) {
      await file.delete();
      gemmaLog('Deleted model file: $filename');
    }
  } catch (e) {
    gemmaLog('Failed to delete model file $filename: $e');
    throw ModelStorageException(
      'Failed to delete model file: $filename',
      e,
      'delete',
    );
  }
}