/// <summary> /// 加载插件列表 /// </summary> /// <param name="status">插件状态</param> /// <returns></returns> public async Task <ActionResult <ResponseModel> > List(string status = "all") { ResponseModel responseData = new ResponseModel(); var pluginConfigModel = PluginConfigModelFactory.Create(); // 获取所有插件信息 IList <PluginInfoModel> pluginInfoModels = PluginInfoModelFactory.CreateAll(); IList <PluginInfoResponseModel> responseModels = new List <PluginInfoResponseModel>(); // 添加插件状态 responseModels = PluginInfoModelToResponseModel(pluginInfoModels, pluginConfigModel); #region 筛选插件状态 switch (status.ToLower()) { case "all": break; case "installed": responseModels = responseModels.Where(m => m.Status == PluginStatus.Enabled || m.Status == PluginStatus.Disabled).ToList(); break; case "enabled": responseModels = responseModels.Where(m => m.Status == PluginStatus.Enabled).ToList(); break; case "disabled": responseModels = responseModels.Where(m => m.Status == PluginStatus.Disabled).ToList(); break; case "uninstalled": responseModels = responseModels.Where(m => m.Status == PluginStatus.Uninstalled).ToList(); break; default: break; } #endregion responseData.code = 1; responseData.message = "加载插件列表成功"; responseData.data = responseModels; return(await Task.FromResult(responseData)); }
public async Task <ActionResult <ResponseModel> > Details(string pluginId) { ResponseModel responseData = new ResponseModel(); try { var pluginConfigModel = PluginConfigModelFactory.Create(); var allPluginConfigModels = pluginConfigModel.EnabledPlugins.Concat(pluginConfigModel.DisabledPlugins) .Concat(pluginConfigModel.UninstalledPlugins).ToList(); #region 效验 if (!allPluginConfigModels.Contains(pluginId)) { responseData.code = -1; responseData.message = $"查看详细失败: 不存在 {pluginId} 插件"; return(await Task.FromResult(responseData)); } #endregion PluginInfoModel pluginInfoModel = PluginInfoModelFactory.Create(pluginId); PluginInfoResponseModel pluginInfoResponseModel = PluginInfoModelToResponseModel(new List <PluginInfoModel>() { pluginInfoModel }, pluginConfigModel).FirstOrDefault(); responseData.code = 1; responseData.message = "查看详细成功"; responseData.data = pluginInfoResponseModel; } catch (Exception ex) { responseData.code = -1; responseData.message = "查看详细失败: " + ex.Message; } return(await Task.FromResult(responseData)); }
/// <summary> /// 上传插件 /// </summary> /// <param name="file">注意: 参数名一定为 file, 对应前端传过来时以 file 为名</param> /// <returns></returns> public async Task <ActionResult <ResponseModel> > Upload([FromForm] IFormFile file) { ResponseModel responseData = new ResponseModel(); #region 效验 if (file == null) { responseData.code = -1; responseData.message = "上传的文件不能为空"; return(responseData); } //文件后缀 string fileExtension = Path.GetExtension(file.FileName);//获取文件格式,拓展名 if (fileExtension != ".zip") { responseData.code = -1; responseData.message = "只能上传zip格式文件"; return(responseData); } //判断文件大小 var fileSize = file.Length; if (fileSize > 1024 * 1024 * 5) // 5M { responseData.code = -1; responseData.message = "上传的文件不能大于5MB"; return(responseData); } #endregion try { // 1.先上传到 临时插件上传目录, 用Guid.zip作为保存文件名 string tempZipFilePath = Path.Combine(PluginPathProvider.TempPluginUploadDir(), Guid.NewGuid() + ".zip"); using (var fs = System.IO.File.Create(tempZipFilePath)) { file.CopyTo(fs); //将上传的文件文件流,复制到fs中 fs.Flush(); //清空文件流 } // 2.解压 bool isDecomparessSuccess = Core.Common.ZipHelper.DecomparessFile(tempZipFilePath, tempZipFilePath.Replace(".zip", "")); // 3.删除原压缩包 System.IO.File.Delete(tempZipFilePath); if (!isDecomparessSuccess) { responseData.code = -1; responseData.message = "解压插件压缩包失败"; return(responseData); } // 4.读取其中的info.json, 获取 PluginId 值 PluginInfoModel pluginInfoModel = PluginInfoModelFactory.ReadPluginDir(tempZipFilePath.Replace(".zip", "")); if (pluginInfoModel == null || string.IsNullOrEmpty(pluginInfoModel.PluginId)) { // 记得删除已不再需要的临时插件文件夹 Directory.Delete(tempZipFilePath.Replace(".zip", ""), true); responseData.code = -1; responseData.message = "不合法的插件"; return(responseData); } string pluginId = pluginInfoModel.PluginId; // 5.检索 此 PluginId 是否本地插件已存在 var pluginConfigModel = PluginConfigModelFactory.Create(); // 本地已经存在的 PluginId IList <string> localExistPluginIds = pluginConfigModel.EnabledPlugins.Concat(pluginConfigModel.DisabledPlugins).Concat(pluginConfigModel.UninstalledPlugins).ToList(); if (localExistPluginIds.Contains(pluginId)) { // 记得删除已不再需要的临时插件文件夹 Directory.Delete(tempZipFilePath.Replace(".zip", ""), true); responseData.code = -1; responseData.message = $"本地已有此插件 (PluginId: {pluginId}), 请前往插件列表删除后, 再上传"; return(responseData); } // 6.本地无此插件 -> 移动插件文件夹到 Plugins 下, 并以 PluginId 为插件文件夹名 string pluginsRootPath = PluginPathProvider.PluginsRootPath(); string newPluginDir = Path.Combine(pluginsRootPath, pluginId); Directory.Move(tempZipFilePath.Replace(".zip", ""), newPluginDir); // 7. 加入 PluginConfigModel.UninstalledPlugins pluginConfigModel.UninstalledPlugins.Add(pluginId); PluginConfigModelFactory.Save(pluginConfigModel); responseData.code = 1; responseData.message = $"上传插件成功 (PluginId: {pluginId})"; } catch (Exception ex) { responseData.code = -1; responseData.message = "上传插件失败: " + ex.Message; } return(await Task.FromResult(responseData)); }