async Task <FtpObjectType> IFtpSession.GetObjectTypeAsync(string path, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            FtpObjectType objectType = FtpObjectType.File;
            FtpListItem   objectInfo = await _ftpClient.GetObjectInfoAsync(path);

            if (objectInfo == null)
            {
                if (await _ftpClient.FileExistsAsync(path))
                {
                    objectType = FtpObjectType.File;
                }
                else
                {
                    if (await _ftpClient.DirectoryExistsAsync(path))
                    {
                        objectType = FtpObjectType.Directory;
                    }
                    else
                    {
                        throw new ArgumentException(string.Format(Resources.PathNotFoundException, path), nameof(path));
                    }
                }
            }
            else
            {
                objectType = objectInfo.Type.ToFtpObjectType();
            }

            return(objectType);
        }
Esempio n. 2
0
        FtpObjectType IFtpSession.GetObjectType(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            FtpObjectType objectType = FtpObjectType.File;
            FtpListItem   objectInfo = _ftpClient.GetObjectInfo(path);

            if (objectInfo == null)
            {
                if (_ftpClient.FileExists(path))
                {
                    objectType = FtpObjectType.File;
                }
                else
                {
                    if (_ftpClient.DirectoryExists(path))
                    {
                        objectType = FtpObjectType.Directory;
                    }
                    else
                    {
                        throw new ArgumentException(string.Format(Resources.PathNotFoundException, path), nameof(path));
                    }
                }
            }
            else
            {
                objectType = objectInfo.Type.ToFtpObjectType();
            }

            return(objectType);
        }
        void IFtpSession.Download(string remotePath, string localPath, bool overwrite, bool recursive)
        {
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }

            FtpObjectType objectType = ((IFtpSession)this).GetObjectType(remotePath);

            if (objectType == FtpObjectType.Directory)
            {
                IEnumerable <Tuple <string, string> > listing = GetRemoteListing(remotePath, localPath, recursive);

                foreach (Tuple <string, string> file in listing)
                {
                    string directoryPath = Path.GetDirectoryName(file.Item1);
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }

                    using (Stream fileStream = File.OpenWrite(file.Item1))
                    {
                        _sftpClient.DownloadFile(file.Item2, fileStream);
                    }
                }
            }
            else
            {
                if (objectType == FtpObjectType.File)
                {
                    if (File.Exists(localPath) && !overwrite)
                    {
                        throw new IOException(Resources.FileExistsException);
                    }

                    using (Stream fileStream = File.OpenWrite(localPath))
                    {
                        _sftpClient.DownloadFile(remotePath, fileStream);
                    }
                }
                else
                {
                    throw new NotImplementedException(Resources.UnsupportedObjectTypeException);
                }
            }
        }
Esempio n. 4
0
        async Task IFtpSession.DownloadAsync(string remotePath, string localPath, bool overwrite, bool recursive, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }

            FtpObjectType objectType = await((IFtpSession)this).GetObjectTypeAsync(remotePath, cancellationToken);

            if (objectType == FtpObjectType.Directory)
            {
                IEnumerable <Tuple <string, string> > listing = await GetRemoteListingAsync(remotePath, localPath, recursive, cancellationToken);

                foreach (Tuple <string, string> pair in listing)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    await _ftpClient.DownloadFileAsync(pair.Item1, pair.Item2, overwrite, FtpVerify.None, cancellationToken, null);
                }
            }
            else
            {
                if (objectType == FtpObjectType.File)
                {
                    if (File.Exists(localPath) && !overwrite)
                    {
                        throw new IOException(Resources.FileExistsException);
                    }

                    await _ftpClient.DownloadFileAsync(localPath, remotePath, overwrite, FtpVerify.None, cancellationToken, null);
                }
                else
                {
                    throw new NotImplementedException(Resources.UnsupportedObjectTypeException);
                }
            }
        }
Esempio n. 5
0
 /// <summary>Creates a new instance of the FtpListParser object and sets the given index locations as sepcified.</summary>
 /// <param name="regex"> </param>
 /// <param name="nameIndex"> </param>
 /// <param name="sizeIndex"> </param>
 /// <param name="modifyIndex"> </param>
 /// <param name="modeIndex"> </param>
 /// <param name="ownerIndex"> </param>
 /// <param name="groupIndex"> </param>
 /// <param name="linkPathIndex"> </param>
 /// <param name="type"> </param>
 private FtpListFormatParser(string regex,
                             int nameIndex,
                             int sizeIndex,
                             int modifyIndex,
                             int modeIndex,
                             int ownerIndex,
                             int groupIndex,
                             int linkPathIndex,
                             FtpObjectType type)
     : this(new Regex(regex),
            nameIndex,
            sizeIndex,
            modifyIndex,
            modeIndex,
            ownerIndex,
            groupIndex,
            linkPathIndex,
            type)
 {
 }
Esempio n. 6
0
 /// <summary>Creates a new instance of the FtpListParser object and sets the given index locations as specified.</summary>
 /// <param name="re"> </param>
 /// <param name="nameIndex"> </param>
 /// <param name="sizeIndex"> </param>
 /// <param name="modifyIndex"> </param>
 /// <param name="modeIndex"> </param>
 /// <param name="ownerIndex"> </param>
 /// <param name="groupIndex"> </param>
 /// <param name="linkPathIndex"> </param>
 /// <param name="type"> </param>
 private FtpListFormatParser(Regex re,
                             int nameIndex,
                             int sizeIndex,
                             int modifyIndex,
                             int modeIndex,
                             int ownerIndex,
                             int groupIndex,
                             int linkPathIndex,
                             FtpObjectType type)
 {
     this._regex         = re;
     this._nameIndex     = nameIndex;
     this._sizeIndex     = sizeIndex;
     this._modifyIndex   = modifyIndex;
     this._modeIndex     = modeIndex;
     this._ownerIndex    = ownerIndex;
     this._groupIndex    = groupIndex;
     this._linkPathIndex = linkPathIndex;
     this._objectType    = type;
 }
Esempio n. 7
0
        void IFtpSession.Download(string remotePath, string localPath, FtpLocalExists overwrite, bool recursive)
        {
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }

            FtpObjectType objectType = ((IFtpSession)this).GetObjectType(remotePath);

            if (objectType == FtpObjectType.Directory)
            {
                IEnumerable <Tuple <string, string> > listing = GetRemoteListing(remotePath, localPath, recursive);

                foreach (Tuple <string, string> pair in listing)
                {
                    _ftpClient.DownloadFile(pair.Item1, pair.Item2, overwrite);
                }
            }
            else
            {
                if (objectType == FtpObjectType.File)
                {
                    if (File.Exists(localPath) && overwrite != FtpLocalExists.Overwrite)
                    {
                        throw new IOException("File Not Exist");
                    }
                    else
                    {
                        _ftpClient.DownloadFileAsync(localPath, remotePath, overwrite);
                    }
                }
                else
                {
                    throw new NotImplementedException("UnsupportedObjectTypeException");
                }
            }
        }
        async Task IFtpSession.DownloadAsync(string remotePath, string localPath, bool overwrite, bool recursive, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }

            FtpObjectType objectType = ((IFtpSession)this).GetObjectType(remotePath);

            if (objectType == FtpObjectType.Directory)
            {
                IEnumerable <Tuple <string, string> > listing = await GetRemoteListingAsync(remotePath, localPath, recursive, cancellationToken);

                foreach (Tuple <string, string> file in listing)
                {
                    if (File.Exists(file.Item1) && !overwrite)
                    {
                        continue;
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    string directoryPath = Path.GetDirectoryName(file.Item1);
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }

                    using (Stream fileStream = File.OpenWrite(file.Item1))
                    {
                        await Task.Factory.FromAsync(_sftpClient.BeginDownloadFile(file.Item2, fileStream), _sftpClient.EndDownloadFile);
                    }
                }
            }
            else
            {
                if (objectType == FtpObjectType.File)
                {
                    if (File.Exists(localPath))
                    {
                        if (overwrite)
                        {
                            File.Delete(localPath);
                        }
                        else
                        {
                            throw new IOException(Resources.FileExistsException);
                        }
                    }

                    using (Stream fileStream = File.OpenWrite(localPath))
                    {
                        await Task.Factory.FromAsync(_sftpClient.BeginDownloadFile(remotePath, fileStream), _sftpClient.EndDownloadFile);
                    }
                }
                else
                {
                    throw new NotImplementedException(Resources.UnsupportedObjectTypeException);
                }
            }
        }
Esempio n. 9
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            PropertyDescriptor ftpSessionProperty = context.DataContext.GetProperties()[WithFtpSession.FtpSessionPropertyName];
            IFtpSession        ftpSession         = ftpSessionProperty?.GetValue(context.DataContext) as IFtpSession;

            if (ftpSession == null)
            {
                throw new InvalidOperationException("FTPSessionNotFoundException");
            }

            string remotePath = RemotePath.Get(context);
            string localPath  = LocalPath.Get(context);

            FtpObjectType objectType = await ftpSession.GetObjectTypeAsync(remotePath, cancellationToken);

            if (objectType == FtpObjectType.Directory)
            {
                if (string.IsNullOrWhiteSpace(Path.GetExtension(localPath)))
                {
                    if (!Directory.Exists(localPath))
                    {
                        if (Create)
                        {
                            Directory.CreateDirectory(localPath);
                        }
                        else
                        {
                            throw new ArgumentException("PathNotFoundException", localPath);
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("IncompatiblePathsException");
                }
            }
            else
            {
                if (objectType == FtpObjectType.File)
                {
                    if (string.IsNullOrWhiteSpace(Path.GetExtension(localPath)))
                    {
                        localPath = Path.Combine(localPath, Path.GetFileName(remotePath));
                    }

                    string directoryPath = Path.GetDirectoryName(localPath);

                    if (!Directory.Exists(directoryPath))
                    {
                        if (Create)
                        {
                            Directory.CreateDirectory(directoryPath);
                        }
                        else
                        {
                            throw new InvalidOperationException("PathNotFoundException");
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException("UnsupportedObjectTypeException");
                }
            }

            if (Overwrite)
            {
                await ftpSession.DownloadAsync(remotePath, localPath, FtpLocalExists.Overwrite, Recursive, cancellationToken);
            }
            else
            {
                await ftpSession.DownloadAsync(remotePath, localPath, FtpLocalExists.Append, Recursive, cancellationToken);
            }

            return((asyncCodeActivityContext) =>
            {
            });
        }
Esempio n. 10
0
 /// <summary>
 /// Creates a new instance of the FtpListParser object and sets
 /// the given index locations as specified.
 /// </summary>
 /// <param name="re"></param>
 /// <param name="nameIndex"></param>
 /// <param name="sizeIndex"></param>
 /// <param name="modifyIndex"></param>
 /// <param name="modeIndex"></param>
 /// <param name="ownerIndex"></param>
 /// <param name="groupIndex"></param>
 /// <param name="linkPathIndex"></param>
 /// <param name="type"></param>
 public FtpListFormatParser(Regex re, int nameIndex, int sizeIndex, int modifyIndex, int modeIndex, int ownerIndex, int groupIndex, int linkPathIndex, FtpObjectType type)
 {
     this.Regex         = re;
     this.NameIndex     = nameIndex;
     this.SizeIndex     = sizeIndex;
     this.ModifyIndex   = modifyIndex;
     this.ModeIndex     = modeIndex;
     this.OwnerIndex    = ownerIndex;
     this.GroupIndex    = groupIndex;
     this.LinkPathIndex = linkPathIndex;
     this.ObjectType    = type;
 }
 /// <summary>
 /// Creates a new instance of the FtpListParser object and sets
 /// the given index locations as sepcified.
 /// </summary>
 /// <param name="regex"></param>
 /// <param name="nameIndex"></param>
 /// <param name="sizeIndex"></param>
 /// <param name="modifyIndex"></param>
 /// <param name="modeIndex"></param>
 /// <param name="ownerIndex"></param>
 /// <param name="groupIndex"></param>
 /// <param name="type"></param>
 public FtpListFormatParser(string regex, int nameIndex, int sizeIndex, int modifyIndex, int modeIndex, int ownerIndex, int groupIndex, int linkPathIndex, FtpObjectType type)
     : this(new Regex(regex), nameIndex, sizeIndex, modifyIndex, modeIndex, ownerIndex, groupIndex, linkPathIndex, type) {
 }
 /// <summary>
 /// Creates a new instance of the FtpListParser object and sets
 /// the given index locations as specified.
 /// </summary>
 /// <param name="re"></param>
 /// <param name="nameIndex"></param>
 /// <param name="sizeIndex"></param>
 /// <param name="modifyIndex"></param>
 /// <param name="modeIndex"></param>
 /// <param name="ownerIndex"></param>
 /// <param name="groupIndex"></param>
 /// <param name="linkPathIndex"></param>
 /// <param name="type"></param>
 public FtpListFormatParser(Regex re, int nameIndex, int sizeIndex, int modifyIndex, int modeIndex, int ownerIndex, int groupIndex, int linkPathIndex, FtpObjectType type) {
     this.Regex = re;
     this.NameIndex = nameIndex;
     this.SizeIndex = sizeIndex;
     this.ModifyIndex = modifyIndex;
     this.ModeIndex = modeIndex;
     this.OwnerIndex = ownerIndex;
     this.GroupIndex = groupIndex;
     this.LinkPathIndex = linkPathIndex;
     this.ObjectType = type;
 }
        private void ParseMachineListing(string listing)
        {
            // more information: http://tools.ietf.org/html/rfc3659#page-23

            var matches = new List<string>();
            var regularExpression = new Regex(@"(.+?)=(.*?);|  ?(.+?)$");
            Match match;

            if (Regex.Match(listing,
                            "^[0-9]+")
                     .Success)
            {
                // this is probably info messages, don't try to parse it
                return;
            }

            if ((match = regularExpression.Match(listing)).Success)
            {
                do
                {
                    matches.Clear();

                    for (var i = 1; i < match.Groups.Count; i++)
                    {
                        var group = match.Groups[i];
                        if (group.Success)
                        {
                            matches.Add(group.Value);
                        }
                    }

                    var key = matches[0];
                    if (matches.Count != 2)
                    {
                        if (matches.Count == 1
                            && this.Name == null)
                        {
                            // filename
                            this.Name = key;
                        }
                        continue;
                    }

                    // key=value pair
                    var value = matches[1];
                    switch (key.Trim()
                               .ToLower())
                    {
                        case "type":
                            switch (this.Type)
                            {
                                case FtpObjectType.Unknown:
                                    var lower = value.ToLower();
                                    if (lower == "file")
                                    {
                                        this.Type = FtpObjectType.File;
                                    }
                                    else if (lower.Contains("os.unix=slink"))
                                    {
                                        this.Type = FtpObjectType.Link;
                                        this.LinkPath = value.Substring(value.LastIndexOf(':'));
                                    }
                                    else if (lower == "dir"
                                             || lower == "cdir"
                                             || lower == "pdir")
                                    {
                                        this.Type = FtpObjectType.Directory;
                                    }
                                    break;
                            }
                            break;
                        case "size":
                            if (this.Size == -1)
                            {
                                this.Size = long.Parse(value);
                            }
                            break;
                        case "modify":
                            if (this.Modify == DateTime.MinValue)
                            {
                                DateTime tmodify;
                                var formats = new[]
                                {
                                    "yyyyMMddHHmmss", "yyyyMMddHHmmss.fff"
                                };
                                if (DateTime.TryParseExact(value,
                                                           formats,
                                                           CultureInfo.InvariantCulture,
                                                           DateTimeStyles.AssumeLocal,
                                                           out tmodify))
                                {
                                    this.Modify = tmodify;
                                }
                            }
                            break;
                        case "unix.mode":
                            if (this.Mode == "0000")
                            {
                                this.Mode = value;
                            }
                            break;
                        case "unix.owner":
                            if (this.Owner == null)
                            {
                                this.Owner = value;
                            }
                            break;
                        case "unix.group":
                            if (this.Group == null)
                            {
                                this.Group = value;
                            }
                            break;
                    }
                } while ((match = match.NextMatch()).Success);
            }
        }
        /// <summary>Parses DOS and UNIX LIST style listings</summary>
        /// <param name="listing"> </param>
        private void ParseListListing(string listing)
        {
            foreach (var p in FtpListFormatParser.Parsers.Value)
            {
                if (!p.Parse(listing))
                {
                    continue;
                }

                this.Type = p.ObjectType;
                this.Name = p.Name;
                this.Size = p.Size;
                this.Modify = p.Modify;
                this.Mode = p.Mode;
                this.Owner = p.Owner;
                this.LinkPath = p.LinkPath;
                this.Group = p.Group;

                break;
            }
        }
 /// <summary>Creates a new instance of the FtpListParser object and sets the given index locations as specified.</summary>
 /// <param name="re"> </param>
 /// <param name="nameIndex"> </param>
 /// <param name="sizeIndex"> </param>
 /// <param name="modifyIndex"> </param>
 /// <param name="modeIndex"> </param>
 /// <param name="ownerIndex"> </param>
 /// <param name="groupIndex"> </param>
 /// <param name="linkPathIndex"> </param>
 /// <param name="type"> </param>
 private FtpListFormatParser(Regex re,
     int nameIndex,
     int sizeIndex,
     int modifyIndex,
     int modeIndex,
     int ownerIndex,
     int groupIndex,
     int linkPathIndex,
     FtpObjectType type)
 {
     this._regex = re;
     this._nameIndex = nameIndex;
     this._sizeIndex = sizeIndex;
     this._modifyIndex = modifyIndex;
     this._modeIndex = modeIndex;
     this._ownerIndex = ownerIndex;
     this._groupIndex = groupIndex;
     this._linkPathIndex = linkPathIndex;
     this._objectType = type;
 }