Пример #1
0
        private async Task TakeOffline(IRemoteClient client, Deployment deployment)
        {
            if (string.IsNullOrEmpty(deployment.OfflineFile))
            {
                return;
            }

            IRemoteFolder folder = await GetFolder(client, deployment.RemotePath);

            IRemoteFile offlineFile = folder?.Files.FirstOrDefault(x => x.Name.StartsWith(deployment.OfflineFile, StringComparison.OrdinalIgnoreCase));

            if (offlineFile == null)
            {
                return;
            }

            if (offlineFile.Name.Equals(deployment.OfflineFile, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            await client.MoveFile(offlineFile.Path, $"{folder.Path}{client.PathSeparator}{deployment.OfflineFile}");

            _remoteFolderCache.Remove(folder.Path);
        }
Пример #2
0
        private async Task <IRemoteFolder> GetLastUploadFolder(IRemoteClient client, Server server, Deployment deployment)
        {
            string uploadPath = server.Paths.Deploy;

            if (!await client.FolderExists(uploadPath))
            {
                return(null);
            }

            IRemoteFolder folder = await client.GetFolder(uploadPath);

            if (folder == null)
            {
                return(null);
            }

            foreach (string dateFolderPath in folder.SubFolders.Reverse().Select(x => x.Path))
            {
                IRemoteFolder dateFolder = await GetFolder(client, dateFolderPath);

                IRemoteFolder uploadFolder = dateFolder.SubFolders.Reverse()
                                             .FirstOrDefault(x => x.Name.StartsWith(deployment.Name, StringComparison.OrdinalIgnoreCase));
                if (uploadFolder != null)
                {
                    return(uploadFolder);
                }
            }

            return(null);
        }
Пример #3
0
        public async Task <IActionResult> Server(ServerPathRequest request)
        {
            string name = request.Name;
            string path = request.Path;

            ModelState.Clear();

            Server server = await _serverService.GetServer(name);

            if (server == null)
            {
                return(RedirectToIndex());
            }

            IRemoteFolder folder = await _remoteService.GetFolder(name, path);

            if (folder == null)
            {
                return(RedirectToIndex());
            }

            return(View("Server", new ServerViewModel
            {
                BackupPath = server.Paths.Backup,
                CanDeleteChildren = await _remoteService.CanDeleteFromFolder(name, path),
                Deployments = await GetDeploymentList(server),
                DeployPath = server.Paths.Deploy,
                Folder = folder,
                Name = name,
                Path = folder.Path
            }));
        }
Пример #4
0
        public async Task MoveFolder(string from, string to)
        {
            from = GetPath(from);
            to   = GetPath(to);

            IRemoteFolder folder = await GetFolder(from);

            if (folder == null)
            {
                return;
            }

            await CreateFolder(to);

            foreach (IRemoteFolder subFolder in folder.SubFolders)
            {
                string path = Path.Combine(to, subFolder.Name);
                await MoveFolder(subFolder.Path, path);
            }

            foreach (IRemoteFile file in folder.Files)
            {
                string path = Path.Combine(to, file.Name);
                File.Move(file.Path, path);
            }

            Directory.Delete(from);
        }
Пример #5
0
        private async Task ClearRemoteFolder(IRemoteClient client, string path, IReadOnlyCollection <string> skipPaths)
        {
            if (skipPaths != null && skipPaths.Contains(path))
            {
                return;
            }

            IRemoteFolder folder = await GetFolder(client, path);

            if (folder == null)
            {
                return;
            }

            foreach (IRemoteFolder subFolder in folder.SubFolders)
            {
                if (skipPaths != null && skipPaths.Contains(subFolder.Path))
                {
                    continue;
                }

                await DeleteFolder(client, subFolder.Path);
            }

            foreach (IRemoteFile file in folder.Files)
            {
                if (skipPaths != null && skipPaths.Contains(file.RelativePath))
                {
                    continue;
                }

                await client.DeleteFile(file.Path);
            }
        }
Пример #6
0
        public async Task <ActionResult <FileApiResponse> > FolderGet(string path)
        {
            IRemoteFolder folder = await _fileSystem.GetFolder(path);

            if (folder == null)
            {
                return(NotFound());
            }
            return(Ok(MapFolder(folder)));
        }
Пример #7
0
        private async Task <bool> CanDeleteFromFolder(IRemoteClient client, Server server, string path)
        {
            IRemoteFolder folder = await GetFolder(client, path);

            if (folder == null)
            {
                return(false);
            }

            return(folder.Ancestors
                   .Any(x => x.Path.Equals(server.Paths.Deploy) || x.Path.Equals(server.Paths.Backup)));
        }
Пример #8
0
        public async Task <ActionResult <FileApiResponse> > FolderDelete(string path)
        {
            IRemoteFolder folder = await _fileSystem.GetFolder(path);

            if (folder == null)
            {
                return(NotFound());
            }

            await _fileSystem.DeleteFolder(path);

            return(NoContent());
        }
Пример #9
0
 private static FolderApiResponse MapFolder(IRemoteFolder folder)
 {
     return(new FolderApiResponse
     {
         Files = folder.Files.Select(x => new FileApiResponse
         {
             LastModified = x.LastModified,
             Name = x.Name
         }),
         Folders = folder.SubFolders.Select(x => new SubFolderApiResponse
         {
             Name = x.Name
         }),
         Parent = !string.IsNullOrEmpty(folder.RelativePath) ? folder.Parent?.RelativePath : null,
         Path = folder.RelativePath.Replace('\\', '/')
     });
 }
Пример #10
0
        public async Task <string> GetLastBackup(int deploymentId)
        {
            Deployment deployment = await _deploymentRepository.GetDeployment(deploymentId);

            if (deployment == null)
            {
                return(null);
            }

            Server server = await _serverRepository.GetServer(deployment.Server);

            IRemoteClient client = GetClient(server);

            IRemoteFolder folder = await GetLastBackupFolder(client, server, deployment);

            return(folder?.Path);
        }
Пример #11
0
        public async Task <IActionResult> Delete(ServerPathRequest request)
        {
            string name = request.Name;
            string path = request.Path;

            IRemoteFolder folder = await _remoteService.GetFolder(name, path);

            if (folder == null)
            {
                request.Path = null;
                return(RedirectToServer(request));
            }

            await _remoteService.DeleteFolder(name, path);

            request.Path = folder.Parent?.Path;
            return(RedirectToServer(request));
        }
Пример #12
0
        public async Task RollbackDeployment(int deploymentId)
        {
            Deployment deployment = await _deploymentRepository.GetDeployment(deploymentId);

            if (deployment == null)
            {
                return;
            }

            Server server = await _serverRepository.GetServer(deployment.Server);

            IReadOnlyCollection <Deployment> deployments = await _deploymentRepository.GetServerDeployments(server.Name);

            IRemoteClient client = GetClient(server);

            IRemoteFolder from = await GetLastBackupFolder(client, server, deployment);

            if (from == null)
            {
                return;
            }

            await TakeOffline(client, deployment);

            IReadOnlyCollection <string> skipPaths = GetPreservedPaths(client, deployments)
                                                     .Except(new[] { deployment.RemotePath })
                                                     .ToArray();

            await ClearRemoteFolder(client, deployment.RemotePath, skipPaths);
            await MoveRemoteFolder(client, from.Path, deployment.RemotePath);

            await PutOnline(client, deployment);

            IRemoteFolder fromParent = await GetFolder(client, from.Parent.Path);

            if (fromParent.SubFolders.Count == 1)
            {
                await DeleteFolder(client, fromParent.Path);
            }
            else
            {
                await DeleteFolder(client, from.Path);
            }
        }
Пример #13
0
        private async Task PutOnline(IRemoteClient client, Deployment deployment)
        {
            if (string.IsNullOrEmpty(deployment.OfflineFile))
            {
                return;
            }

            IRemoteFolder folder = await GetFolder(client, deployment.RemotePath);

            IRemoteFile offlineFile = folder?.Files.FirstOrDefault(x => x.Name.Equals(deployment.OfflineFile));

            if (offlineFile == null)
            {
                return;
            }

            await client.MoveFile(offlineFile.Path, $"{offlineFile.Path}.x");

            _remoteFolderCache.Remove(folder.Path);
        }
Пример #14
0
        public async Task <bool> IsOffline(int deploymentId)
        {
            Deployment deployment = await _deploymentRepository.GetDeployment(deploymentId);

            if (deployment == null || string.IsNullOrEmpty(deployment.OfflineFile))
            {
                return(false);
            }

            IRemoteClient client = await GetClient(deployment);

            IRemoteFolder folder = await GetFolder(client, deployment.RemotePath);

            if (folder == null)
            {
                return(false);
            }

            return(folder.Files.Any(x => x.Name.Equals(deployment.OfflineFile, StringComparison.OrdinalIgnoreCase)));
        }
Пример #15
0
        private async Task <IRemoteFolder> GetFolder(IRemoteClient client, string path)
        {
            path = path ?? "";
            if (_remoteFolderCache.ContainsKey(path))
            {
                return(_remoteFolderCache[path]);
            }

            try
            {
                IRemoteFolder folder = await client.GetFolder(path);

                _remoteFolderCache.Add(path, folder);
                return(folder);
            }
            catch (FtpCommandException)
            {
                return(null);
            }
        }
Пример #16
0
        public async Task CopyFolder(string from, string to, IReadOnlyCollection <string> skipPaths)
        {
            from = GetPath(from);
            to   = GetPath(to);

            IRemoteFolder folder = await GetFolder(from);

            if (folder == null)
            {
                return;
            }

            if (skipPaths.Any(x => GetPath(x).Equals(folder.Path, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }

            await CreateFolder(to);

            foreach (IRemoteFolder subFolder in folder.SubFolders)
            {
                string path = Path.Combine(to, subFolder.Name);
                await CopyFolder(subFolder.Path, path, skipPaths);
            }

            foreach (IRemoteFile file in folder.Files)
            {
                if (skipPaths.Any(x => GetPath(x).Equals(file.Path, StringComparison.OrdinalIgnoreCase)))
                {
                    continue;
                }

                string path = Path.Combine(to, file.Name);
                File.Copy(file.Path, path, true);
            }
        }
Пример #17
0
        public async Task <bool> FolderExists(string path)
        {
            IRemoteFolder folder = await GetFolder(path);

            return(folder != null);
        }