public async Task <ActionResult <ResponseModel> > Delete(string pluginId) { ResponseModel responseData = new ResponseModel(); var pluginConfigModel = PluginConfigModelFactory.Create(); // 效验是否存在于 已卸载插件列表 if (!pluginConfigModel.UninstalledPlugins.Contains(pluginId)) { responseData.code = -1; responseData.message = "删除失败: 此插件不存在, 或未卸载"; return(await Task.FromResult(responseData)); } try { // 1.删除物理文件 string pluginPath = Path.Combine(PluginPathProvider.PluginsRootPath(), pluginId); var directory = new DirectoryInfo(pluginPath); directory.Delete(true); // 2.从 pluginConfigModel.UninstalledPlugins 移除 pluginConfigModel.UninstalledPlugins.Remove(pluginId); // 3.保存到 plugin.config.json PluginConfigModelFactory.Save(pluginConfigModel); responseData.code = 1; responseData.message = "删除成功"; } catch (Exception ex) { responseData.code = -2; 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)); }