Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FtpFileSystemInfo"/> class.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="type">The type.</param>
        protected internal FtpFileSystemInfo(Uri uri, FtpFileSystemInfoType type)
        {
            if (uri == null)
                throw new ArgumentNullException("uri");

            if (uri.Scheme != Uri.UriSchemeFtp)
                throw new ArgumentException("The URI isn't a valid FTP Uri", "uri");

            this.Uri = uri;
            this.Type = type;
        }
Example #2
0
        /// <summary>
        /// Gets the files or directories from the specified directory.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="type">The type.</param>
        /// <returns>
        /// An enumeration of <see cref="FlagFtp.FtpFileSystemInfo"/> with the type of the specified type argument.
        /// </returns>
        private IEnumerable<FtpFileSystemInfo> GetFileSystemInfos(Uri directory, FtpFileSystemInfoType type)
        {
            if (directory == null)
                throw new ArgumentNullException("directory");

            if (directory.Scheme != Uri.UriSchemeFtp)
                throw new ArgumentException("The directory isn't a valid FTP URI", "directory");

            using (var response = (this.CreateResponse(directory, WebRequestMethods.Ftp.ListDirectoryDetails)))
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(responseStream))
                    {
                        string all = reader.ReadToEnd();

                        var regex = new Regex(@"^(?<FileOrDirectory>[d-])(?<Attributes>[rwxts-]{3}){3}\s+\d{1,}\s+.*?(?<FileSize>\d{1,})\s+(?<Date>\w+\s+\d{1,2}\s+(?:\d{4})?)(?<YearOrTime>\d{1,2}:\d{2})?\s+(?<Name>.+?)\s?$",
                            RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                        MatchCollection matches = regex.Matches(all);

                        var infos = matches.Cast<Match>()
                            .Select(
                                match =>
                                new
                                {
                                    IsDirectory = match.Groups["FileOrDirectory"].Value == "d",
                                    FileLength = long.Parse(match.Groups["FileSize"].Value),
                                    Name = match.Groups["Name"].Value,
                                    FullName = new Uri(new Uri(directory + "/"), match.Groups["Name"].Value)
                                })
                            .Where(info => info.Name != "." && info.Name != "..");

                        switch (type)
                        {
                            case FtpFileSystemInfoType.Directory:
                                return infos.Where(info => info.IsDirectory)
                                    .Select(info => new FtpDirectoryInfo(this.NormalizeUri(info.FullName)))
                                    .Cast<FtpFileSystemInfo>();

                            case FtpFileSystemInfoType.File:
                                return infos.Where(info => !info.IsDirectory)
                                    .Select(info => new FtpFileInfo(this.NormalizeUri(info.FullName), this.GetTimeStamp(info.FullName), info.FileLength))
                                    .Cast<FtpFileSystemInfo>();
                        }
                    }
                }
            }

            throw new InvalidOperationException("Method should not reach this code!");
        }