Exemplo n.º 1
0
        public async Task <JsonResult> LoadNccStoreModules()
        {
            ApiResponse resp = new ApiResponse();

            resp.IsSuccess = false;
            resp.Message   = "";
            try
            {
                try
                {
                    var settings = Settings.GetByKey <StoreSettings>();
                    resp.Data = NccRestClient.Get <List <NccModuleViewModel> >(settings.StoreBaseUrl, "NccStore/Api/Modules", new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("nccVersion", NccInfo.Version.ToString(4)),
                        new KeyValuePair <string, string>("key", settings.SecretKey),
                        new KeyValuePair <string, string>("domain", GlobalContext.WebSite.DomainName)
                    });
                    resp.IsSuccess = true;
                    resp.Message   = "Success";
                }
                catch (HttpRequestException e)
                {
                    _logger.LogError($"Request exception: {e.Message}", e);
                }
            }
            catch (Exception) { }
            return(Json(resp));
        }
Exemplo n.º 2
0
        public async Task <JsonResult> DownloadModule(string moduleName, string version)
        {
            ApiResponse resp = new ApiResponse();

            resp.IsSuccess = true;
            resp.Message   = "";
            if (moduleName.Trim() != "")
            {
                try
                {
                    var settings = Settings.GetByKey <StoreSettings>();
                    var data     = NccRestClient.Download(settings.StoreBaseUrl, "NccStore/Api/DownloadModule", new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("moduleName", moduleName),
                        new KeyValuePair <string, string>("version", version),
                        new KeyValuePair <string, string>("key", settings.SecretKey),
                        new KeyValuePair <string, string>("domain", GlobalContext.WebSite.DomainName)
                    });

                    #region Download in temp folder
                    var tempFullFilepath = Path.Combine(_env.ContentRootPath, _modulePath + "\\_temp");
                    try
                    {
                        if (resp.IsSuccess == true)
                        {
                            if (!Directory.Exists(tempFullFilepath))
                            {
                                Directory.CreateDirectory(tempFullFilepath);
                            }

                            using (var stream = new FileStream(_modulePath + "\\_temp\\" + moduleName + ".zip", FileMode.Create, FileAccess.Write))
                            {
                                stream.Write(data, 0, data.Length);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.ToString());
                        resp.IsSuccess = false;
                        resp.Message  += "Module Download Failed.";
                    }
                    #endregion

                    #region Check & Create module folder
                    var finalFolderPath = Path.Combine(_env.ContentRootPath, _modulePath + "\\" + moduleName);
                    try
                    {
                        if (resp.IsSuccess == true)
                        {
                            if (!Directory.Exists(finalFolderPath))
                            {
                                Directory.CreateDirectory(finalFolderPath);
                            }
                            else
                            {
                                //Delete is not working perfectly. Bin folder dll file is in use cannot delete.
                                Thread.Sleep(2000);
                                try
                                {
                                    string strCmdText;
                                    strCmdText = "rd /s /q \"" + finalFolderPath + "\"";
                                    System.Diagnostics.Process.Start("CMD.exe", strCmdText);
                                    //Directory.Delete(finalFolderPath, true);
                                    //DeleteDirectory(finalFolderPath);
                                }
                                catch (Exception ex)
                                {
                                    _logger.LogError(ex.ToString());
                                    resp.IsSuccess = false;
                                    resp.Message  += "Previous module folder delete failed.";
                                }
                                Directory.CreateDirectory(finalFolderPath);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.ToString());
                        resp.IsSuccess = false;
                        resp.Message  += "Module folder creation failed.";
                    }
                    #endregion

                    #region Unzip in folder location
                    try
                    {
                        if (resp.IsSuccess == true)
                        {
                            ZipFile.ExtractToDirectory(tempFullFilepath + "\\" + moduleName + ".zip", finalFolderPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.ToString());
                        resp.IsSuccess = false;
                        resp.Message  += "Module Unzip Failed.";
                    }

                    try
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            if (System.IO.File.Exists(tempFullFilepath + "\\" + moduleName + ".zip") == true)
                            {
                                Thread.Sleep(2000);
                                System.IO.File.Delete(tempFullFilepath + "\\" + moduleName + ".zip");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.ToString());
                        resp.IsSuccess = false;
                        resp.Message  += "Downloaded temporary file remove failed.";
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString());
                    resp.IsSuccess = false;
                    resp.Message   = ex.Message;
                }
            }
            if (resp.IsSuccess == true)
            {
                resp.Message = "Module downloaded and restored successfully";
            }
            return(Json(resp));
        }