Esempio n. 1
0
        public SftpChannel(DirectoryInfo workingDirectory, ChannelSettings channelSettings)
        {
            _channelSettings = channelSettings ?? throw new ArgumentNullException(nameof(channelSettings));

            _workingDirectory = workingDirectory ?? throw new ArgumentNullException(nameof(channelSettings));

            _channelDirection = ChannelDirectionFactory.Get(channelSettings);

            _source = string.IsNullOrEmpty(channelSettings.Path) ? throw new ArgumentException(nameof(channelSettings.Path))  :  channelSettings.Path;

            _target = _workingDirectory;

            _connectionInfo = channelSettings.AsConnectionInfo();
        }
Esempio n. 2
0
        public static ChannelSettings CreateIfNotExists(this ChannelSettings target)
        {
            var info = target.AsConnectionInfo();

            using (SftpClient client = new SftpClient(info))
            {
                client.Connect();
                if (!client.Exists(target.Path))
                {
                    client.CreateDirectory(target.Path);
                }
            }
            return(target);
        }
Esempio n. 3
0
        public static ChannelSettings DeleteOneFile(this ChannelSettings target, FileInfo fileInfo)
        {
            var info = target.AsConnectionInfo();

            using (SftpClient client = new SftpClient(info))
            {
                client.Connect();

                if (client.Exists(target.Path))
                {
                    client.DeleteFile(Path.Combine(target.Path, fileInfo.Name));
                }
            }
            return(target);
        }
Esempio n. 4
0
        public static ChannelSettings DeleteWithContentIfExists(this ChannelSettings target)
        {
            var info = target.AsConnectionInfo();

            using (SftpClient client = new SftpClient(info))
            {
                client.Connect();

                if (client.Exists(target.Path))
                {
                    DeleteRecoursively(target.Path, client);
                }
            }
            return(target);
        }
Esempio n. 5
0
        public static void CopyContentTo(this DirectoryInfo directory, ChannelSettings target)
        {
            var info  = target.AsConnectionInfo();
            var files = directory.GetFiles();

            using (SftpClient client = new SftpClient(info))
            {
                client.Connect();
                foreach (var file in files)
                {
                    string tempFile         = $"{Path.GetFileNameWithoutExtension(file.Name)}{Constants.FileExtensions.FileOps}";
                    string tempFileName     = Path.Combine(target.Path, tempFile);
                    string originalFileName = Path.Combine(target.Path, file.Name);
                    using (var stream = file.OpenRead())
                    {
                        client.UploadFile(stream, tempFileName, true);
                    }
                    client.RenameFile(tempFileName, originalFileName);
                }
            }
        }