private async Task DetectAndUpdatePlugins(FtpClient ftpClient) { var cachePlugins = await mvcPluginSetup.GetPluginsAsync(all : true); var ftpPlugins = await ftpClient.GetFileDetailsAsync(); foreach (var o in cachePlugins.Select(o => o.ZipFileName).Concat(ftpPlugins.Select(o => o.name)).Distinct()) { var cache = cachePlugins.FirstOrDefault(x => x.ZipFileName == o); // cache var(name, size, modified) = ftpPlugins.FirstOrDefault(x => x.name == o); // current if (!string.IsNullOrEmpty(name) && (size != cache?.Size || modified != cache?.Modified)) { try { // 下载并更新插件 using (var stream = await ftpClient.DownloadAsync(name)) { var pluginPackage = serviceScope.ServiceProvider.GetRequiredService <PluginPackage>(); var pluginManager = serviceScope.ServiceProvider.GetRequiredService <IPluginManager>(); // 服务器 ftp 若存在相同名称的插件,会频繁更新\卸载 // TODO 添加维护版本信息 await pluginPackage.InitializeAsync(stream); pluginPackage.Configuration.Size = size; pluginPackage.Configuration.Modified = modified; pluginPackage.Configuration.ZipFileName = name; await pluginManager.AddPluginsAsync(pluginPackage); } logger.LogInformation(new EventId(200, "下载并更新插件"), $"name={name},size={size},modified={modified}"); } catch (Exception ex) { logger.LogError(new EventId(400, "下载并更新插件"), ex, ex.Message); } } } }
public async Task <List <PluginModel> > GetAllPluginsAsync() => await mvcModuleSetup.GetPluginsAsync();