Ejemplo n.º 1
0
        private void CopyAllToSftp(string source, string target, bool overwrite, DateTime?lastModificationLimit, SftpClientAdapter client, string[] excludedFiles)
        {
            string targetPath = target;

            if (!client.Exists(targetPath))
            {
                client.CreateDirectory(targetPath);
            }

            DirectoryInfo sourceDir = new DirectoryInfo(source);

            foreach (FileInfo file in sourceDir.GetFiles())
            {
                if (excludedFiles != null && excludedFiles.Contains(file.Name))
                {
                    continue;
                }

                string targetFilePath = FileSystemUtils.ConcatenatePaths(targetPath, file.Name);
                if (((!client.Exists(targetFilePath)) || overwrite) && ((!lastModificationLimit.HasValue) || (lastModificationLimit.Value < file.LastWriteTime)))
                {
                    using FileStream sourceStream = file.OpenRead();
                    client.UploadFile(sourceStream, targetFilePath, true);
                }
            }
            foreach (DirectoryInfo directory in sourceDir.GetDirectories())
            {
                CopyAllToSftp(directory.FullName, FileSystemUtils.ConcatenatePaths(target, directory.Name), overwrite, lastModificationLimit, client, FileSystemUtils.GetExcludedFilesForSubdirectory(excludedFiles, directory.Name));
            }
        }
        public ICollection <JobFileContent> SynchronizeFiles()
        {
            ConnectionInfo connection = ConnectionPool.GetConnectionForUser(_credentials);

            try
            {
                var    client     = new SftpClientAdapter((ExtendedSftpClient)connection.Connection);
                string sourcePath = FileSystemUtils.ConcatenatePaths(SyncFileInfo.SourceDirectory, SyncFileInfo.RelativePath);

                if (client.Exists(sourcePath))
                {
                    using Stream sourceStream = client.OpenRead(sourcePath);
                    string synchronizedContent = FileSystemUtils.ReadStreamContentFromSpecifiedOffset(sourceStream, Offset);
                    if (!string.IsNullOrEmpty(SyncFileInfo.DestinationDirectory))
                    {
                        string destinationPath = Path.Combine(SyncFileInfo.DestinationDirectory, SyncFileInfo.RelativePath);
                        if (Offset == 0)
                        {
                            File.Delete(destinationPath);
                        }
                        FileSystemUtils.WriteStringToLocalFile(synchronizedContent, destinationPath);
                    }

                    JobFileContent[] result = new JobFileContent[1]
                    {
                        new JobFileContent {
                            RelativePath = SyncFileInfo.RelativePath,
                            Content      = synchronizedContent,
                            Offset       = Offset
                        }
                    };

                    if (SyncFileInfo.SynchronizationType == FileSynchronizationType.IncrementalAppend)
                    {
                        Offset = sourceStream.Position;
                    }
                    return(result);
                }
            }
            finally
            {
                ConnectionPool.ReturnConnection(connection);
            }
            return(default);
Ejemplo n.º 3
0
        private void DeleteRemoteDirectory(string remotePath, SftpClientAdapter client)
        {
            _logger.LogDebug($"Starting delete remote directory {remotePath}");
            if (client.Exists(remotePath))
            {
                foreach (SftpFile file in client.ListDirectory(remotePath))
                {
                    if (file.Name == "." || file.Name == "..")
                    {
                        continue;
                    }

                    if (file.IsSymbolicLink)
                    {
                        _logger.LogDebug($"Deleting symlink {file.Name}");
                        client.Delete(file.FullName);
                    }
                    else
                    {
                        if (file.IsDirectory)
                        {
                            _logger.LogDebug($"Deleting subdirectory {file.Name}");
                            DeleteRemoteDirectory(file.FullName, client);
                        }
                        else
                        {
                            _logger.LogDebug($"Deleting file {file.Name}");
                            client.DeleteFile(file.FullName);
                        }
                    }
                }

                _logger.LogDebug($"Deleting root directory {remotePath}");
                client.DeleteDirectory(remotePath);
            }
        }