resolveLetsEncryptDirectory static method

Directory? resolveLetsEncryptDirectory(
  1. Object? directory, {
  2. APIConfig? apiConfig,
  3. bool letsEncrypt = false,
})

Implementation

static Directory? resolveLetsEncryptDirectory(Object? directory,
    {APIConfig? apiConfig, bool letsEncrypt = false}) {
  directory ??= apiConfig?.getPath('letsencrypt', 'directory');

  if (directory != null) {
    Directory? dir;
    if (directory is Directory) {
      dir = directory;
    } else if (directory is String) {
      directory = directory.trim();
      if (directory.isNotEmpty) {
        dir = Directory(directory);
      }
    }

    if (dir != null) {
      if (letsEncrypt && !dir.existsSync()) {
        dir.createSync(recursive: true);
      }
      return dir.existsSync() ? dir.absolute : dir;
    }
  }

  var paths = ['/etc/letsencrypt/live', '/etc/letsencrypt'];

  var userDir = _getUserDirectory();
  if (userDir != null) {
    paths.add(pack_path.join(userDir.path, '.letsencrypt'));
    paths.add(pack_path.join(userDir.path, '.lets-encrypt'));
    paths.add(pack_path.join(userDir.path, 'letsencrypt'));
    paths.add(pack_path.join(userDir.path, 'lets-encrypt'));
  }

  for (var p in paths) {
    var dir = Directory(p);
    if (dir.existsSync() && dir.statSync().canWrite) {
      return dir.absolute;
    }
  }

  if (letsEncrypt && userDir != null) {
    var dir = Directory(pack_path.join(userDir.path, '.letsencrypt'));
    dir.createSync();
    return dir;
  }

  return null;
}