Пример #1
0
        public async Task <IActionResult> Import(IFormFile importedPackage)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.Import))
            {
                return(Forbid());
            }

            if (importedPackage != null)
            {
                var tempArchiveName   = Path.GetTempFileName() + Path.GetExtension(importedPackage.FileName);
                var tempArchiveFolder = PathExtensions.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                try
                {
                    using (var stream = new FileStream(tempArchiveName, FileMode.Create))
                    {
                        await importedPackage.CopyToAsync(stream);
                    }

                    if (importedPackage.FileName.EndsWith(".zip"))
                    {
                        ZipFile.ExtractToDirectory(tempArchiveName, tempArchiveFolder);
                    }
                    else if (importedPackage.FileName.EndsWith(".json"))
                    {
                        Directory.CreateDirectory(tempArchiveFolder);
                        System.IO.File.Move(tempArchiveName, Path.Combine(tempArchiveFolder, "Recipe.json"));
                    }
                    else
                    {
                        await _notifier.ErrorAsync(H["Only zip or json files are supported."]);

                        return(RedirectToAction(nameof(Index)));
                    }

                    await _deploymentManager.ImportDeploymentPackageAsync(new PhysicalFileProvider(tempArchiveFolder));

                    await _notifier.SuccessAsync(H["Deployment package imported."]);
                }
                finally
                {
                    if (System.IO.File.Exists(tempArchiveName))
                    {
                        System.IO.File.Delete(tempArchiveName);
                    }

                    if (Directory.Exists(tempArchiveFolder))
                    {
                        Directory.Delete(tempArchiveFolder, true);
                    }
                }
            }
            else
            {
                await _notifier.ErrorAsync(H["Please add a file to import."]);
            }

            return(RedirectToAction(nameof(Index)));
        }
Пример #2
0
        public async Task <IActionResult> Import(ImportViewModel model)
        {
            var remoteClientList = await _remoteClientService.GetRemoteClientListAsync();

            var remoteClient = remoteClientList.RemoteClients.FirstOrDefault(x => x.ClientName == model.ClientName);

            var apiKey = Encoding.UTF8.GetString(_dataProtector.Unprotect(remoteClient.ProtectedApiKey));

            if (remoteClient == null || model.ApiKey != apiKey || model.ClientName != remoteClient.ClientName)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, "The Api Key was not recognized"));
            }

            // Create a temporary filename to save the archive
            var tempArchiveName = Path.GetTempFileName() + ".zip";

            // Create a temporary folder to extract the archive to
            var tempArchiveFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                using (var fs = System.IO.File.Create(tempArchiveName))
                {
                    await model.Content.CopyToAsync(fs);
                }

                ZipFile.ExtractToDirectory(tempArchiveName, tempArchiveFolder);

                await _deploymentManager.ImportDeploymentPackageAsync(new PhysicalFileProvider(tempArchiveFolder));
            }
            finally
            {
                if (System.IO.File.Exists(tempArchiveName))
                {
                    System.IO.File.Delete(tempArchiveName);
                }

                if (Directory.Exists(tempArchiveFolder))
                {
                    Directory.Delete(tempArchiveFolder, true);
                }
            }

            return(Ok());
        }
Пример #3
0
        public async Task <IActionResult> Import(IFormFile importedPackage)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.Import))
            {
                return(Unauthorized());
            }

            var tempArchiveName   = Path.GetTempFileName() + ".zip";
            var tempArchiveFolder = PathExtensions.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                using (var stream = new FileStream(tempArchiveName, FileMode.Create))
                {
                    await importedPackage.CopyToAsync(stream);
                }

                ZipFile.ExtractToDirectory(tempArchiveName, tempArchiveFolder);

                await _deploymentManager.ImportDeploymentPackageAsync(new PhysicalFileProvider(tempArchiveFolder));

                _notifier.Success(H["Deployment package imported"]);
            }
            finally
            {
                if (System.IO.File.Exists(tempArchiveName))
                {
                    System.IO.File.Delete(tempArchiveName);
                }

                if (Directory.Exists(tempArchiveFolder))
                {
                    Directory.Delete(tempArchiveFolder, true);
                }
            }

            return(RedirectToAction("Index"));
        }