示例#1
0
        public IActionResult UpdatePluginStatus(PluginInfoModel pluginInfoModel)
        {
            var plugin = _pluginAccountant.GetAvailablePlugins().FirstOrDefault(x => x.SystemName == pluginInfoModel.SystemName);

            if (plugin == null)
            {
                return(NotFound());
            }
            if (pluginInfoModel.Installed && !plugin.Installed)
            {
                _pluginAccountant.InstallPlugin(plugin);
            }
            else if (!pluginInfoModel.Installed && plugin.Installed)
            {
                _pluginAccountant.UninstallPlugin(plugin);
            }

            if (pluginInfoModel.Active && !plugin.ActiveStoreIds.Contains(CurrentStore.Id))
            {
                _pluginAccountant.ActivatePlugin(plugin);
            }
            else if (!pluginInfoModel.Active && plugin.ActiveStoreIds.Contains(CurrentStore.Id))
            {
                _pluginAccountant.DeactivatePlugin(plugin);
            }
            return(R.Success.Result);
        }
        public IHttpActionResult Install(PluginInfoModel model)
        {
            //first find the plugin
            var pluginInfo = _pluginFinderService.FindPlugin(model.SystemName);

            if (pluginInfo == null)
            {
                //was it a correct plugin?
                VerboseReporter.ReportError("The plugin doesn't exist", "plugin");
                return(RespondFailure());
            }

            //install the plugin
            _pluginInstallerService.Install(pluginInfo);

            VerboseReporter.ReportSuccess("The plugin has been installed", "plugin");
            return(RespondSuccess());
        }
        public static IList <PluginInfoModel> CreateAll()
        {
            IList <PluginInfoModel> pluginInfoModels = new List <PluginInfoModel>();
            IList <string>          pluginDirs       = PluginPathProvider.AllPluginDir();

            foreach (var dir in pluginDirs)
            {
                // 从 dir 中解析出 pluginId
                // 约定: 插件文件夹名=PluginID=插件主.dll
                string          pluginId = PluginPathProvider.GetPluginFolderNameByDir(dir);
                PluginInfoModel model    = Create(pluginId);
                pluginInfoModels.Add(model);
            }
            // 去除为 null: 目标插件信息不存在,或者格式错误的
            pluginInfoModels = pluginInfoModels.Where(m => m != null).ToList();

            return(pluginInfoModels);
        }
示例#4
0
        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="pluginDir"></param>
        /// <returns></returns>
        public static PluginInfoModel ReadPluginDir(string pluginDir)
        {
            PluginInfoModel pluginInfoModel    = new PluginInfoModel();
            string          pluginInfoFilePath = Path.Combine(pluginDir, InfoJson);

            if (!File.Exists(pluginInfoFilePath))
            {
                return(null);
            }
            try
            {
                string pluginInfoJsonStr = File.ReadAllText(pluginInfoFilePath, Encoding.UTF8);
                JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
                jsonSerializerOptions.PropertyNameCaseInsensitive = true;
                pluginInfoModel = JsonSerializer.Deserialize <PluginInfoModel>(pluginInfoJsonStr, jsonSerializerOptions);
            }
            catch (Exception ex)
            {
                pluginInfoModel = null;
            }

            return(pluginInfoModel);
        }
示例#6
0
        /// <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));
        }