예제 #1
0
 protected FtpItemInfo(SerializationInfo info, StreamingContext context)
 {
     this.Context = FtpContext.GetDeserializationContext((FtpContext) info.GetValue("Context", typeof(FtpContext)));
     this.AbsoluteUri = (Uri) info.GetValue("AbsoluteUri", typeof(Uri));
     this.FName = info.GetString("Name");
     SerializationInfoEnumerator enumerator = info.GetEnumerator();
     while (enumerator.MoveNext())
     {
         SerializationEntry current = enumerator.Current;
         if (current.Name == "LastWriteTime")
         {
             this.FLastWriteTime = new DateTime?((DateTime) current.Value);
             break;
         }
     }
     this.Initialize();
 }
예제 #2
0
 internal FtpItemInfo(FtpContext context, Uri absoluteUri, IVirtualFolder parent)
 {
     if (absoluteUri == null)
     {
         throw new ArgumentNullException("absoluteUri");
     }
     if (absoluteUri.Scheme != Uri.UriSchemeFtp)
     {
         throw new ArgumentException("Ftp uri scheme expected");
     }
     if (!absoluteUri.IsAbsoluteUri)
     {
         throw new ArgumentException("Relative uri is not supported");
     }
     this.Context = context;
     this.AbsoluteUri = absoluteUri;
     string[] segments = this.AbsoluteUri.Segments;
     if (segments.Length > 1)
     {
         this.FName = context.DecodeString(PathHelper.ExcludeTrailingDirectorySeparator(Uri.UnescapeDataString(segments[segments.Length - 1])));
     }
     else
     {
         UriBuilder builder = new UriBuilder(this.AbsoluteUri) {
             UserName = string.Empty,
             Password = string.Empty
         };
         if (this.AbsoluteUri.IsDefaultPort)
         {
             builder.Port = -1;
         }
         this.FName = PathHelper.ExcludeTrailingDirectorySeparator(builder.ToString());
     }
     this.FParent = parent;
     this.SetCapability(FtpItemCapability.HasParent, (this.FParent != null) || (segments.Length < 2));
     this.Initialize();
 }
예제 #3
0
 internal FtpItemInfo(FtpContext context, Uri baseUri, string name, DateTime lastWriteTime, IVirtualFolder parent)
 {
     if (baseUri == null)
     {
         throw new ArgumentNullException("baseUri");
     }
     if (baseUri.Scheme != Uri.UriSchemeFtp)
     {
         throw new ArgumentException("Ftp uri scheme expected");
     }
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     this.Context = context;
     this.FLastWriteTime = new DateTime?(lastWriteTime);
     this.FName = name;
     this.AbsoluteUri = new Uri(baseUri, Path.Combine(baseUri.AbsolutePath, context.EncodeString(this.FName)));
     this.FParent = parent;
     this.SetCapability(FtpItemCapability.HasParent, this.FParent != null);
     this.Initialize();
 }
예제 #4
0
 internal static FtpContext GetDeserializationContext(FtpContext context)
 {
     WeakReference reference;
     if (DeserializationContexts == null)
     {
         DeserializationContexts = new Dictionary<Guid, WeakReference>();
         DeserializationContexts.Add(context.ContextId, new WeakReference(context));
         return context;
     }
     if (DeserializationContexts.TryGetValue(context.ContextId, out reference) && reference.IsAlive)
     {
         return (FtpContext) reference.Target;
     }
     DeserializationContexts[context.ContextId] = new WeakReference(context);
     return context;
 }
예제 #5
0
        public static IVirtualItem FromUri(FtpContext context, Uri absoluteUri, VirtualItemType type, IVirtualFolder parent)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (absoluteUri == null)
            {
                throw new ArgumentNullException("absoluteUri");
            }
            if (!absoluteUri.IsAbsoluteUri)
            {
                throw new ArgumentException("This operation is not supported for a relative URI.");
            }
            if (type == VirtualItemType.Unknown)
            {
                if (absoluteUri.AbsolutePath.EndsWith("/", StringComparison.Ordinal))
                {
                    type = VirtualItemType.Folder;
                }
                else
                {
                    try
                    {
                        context.GetFileSize(absoluteUri);
                        type = VirtualItemType.File;
                    }
                    catch
                    {
                        try
                        {
                            context.PrintWorkingDirectory(absoluteUri);
                            type = VirtualItemType.Folder;
                        }
                        catch
                        {
                            throw new ArgumentException(string.Format("Cannot determine file type from uri ({0})", absoluteUri));
                        }
                    }
                }
            }
            switch (type)
            {
                case VirtualItemType.Folder:
                    return new FtpFolder(context, absoluteUri, parent);

                case VirtualItemType.File:
                    try
                    {
                        Uri baseUri = FtpItemInfo.GetParent(absoluteUri);
                        IVirtualItem item = null;
                        foreach (string str in context.ListDirectoryDetails(absoluteUri))
                        {
                            item = ParseListString(context, baseUri, str, parent);
                            break;
                        }
                        if (item != null)
                        {
                            return item;
                        }
                    }
                    catch (WebException)
                    {
                    }
                    return new FtpFile(context, absoluteUri, parent);
            }
            throw new InvalidEnumArgumentException("type", (int) type, typeof(VirtualItemType));
        }
예제 #6
0
 protected FtpItem(FtpContext context, Uri baseUri, string name, DateTime lastWriteTime, IVirtualFolder parent)
 {
     this.ItemInfo = new FtpItemInfo(context, baseUri, name, lastWriteTime, parent);
 }
예제 #7
0
 protected FtpItem(FtpContext context, Uri absoluteUri, IVirtualFolder parent)
 {
     this.ItemInfo = new FtpItemInfo(context, absoluteUri, parent);
 }
예제 #8
0
        internal static IVirtualItem ParseListString(FtpContext context, Uri baseUri, string line, IVirtualFolder parent)
        {
            Match match;
            int num;
            if (context.ListPattern == null)
            {
                match = ListPatternUnix.Match(line);
                if (match.Success)
                {
                    context.ListPattern = ListPatternUnix;
                }
                else
                {
                    match = ListPatternDos.Match(line);
                    if (match.Success)
                    {
                        context.ListPattern = ListPatternDos;
                    }
                }
            }
            else
            {
                match = context.ListPattern.Match(line);
            }
            if (!match.Success)
            {
                return null;
            }
            if (match.Groups["month"].Success)
            {
                num = Convert.ToInt32(match.Groups["month"].Value);
            }
            else
            {
                string str = match.Groups["monthname"].Value;
                if (string.IsNullOrEmpty(str))
                {
                    return null;
                }
                num = 1;
                foreach (string str2 in CultureInfo.InvariantCulture.DateTimeFormat.AbbreviatedMonthNames)
                {
                    if (str2.Equals(str))
                    {
                        break;
                    }
                    num++;
                }
            }
            int day = Convert.ToInt32(match.Groups["day"].Value);
            int year = 0;
            if (match.Groups["year"].Success)
            {
                year = Convert.ToInt32(match.Groups["year"].Value);
            }
            if (year == 0)
            {
                year = DateTime.Now.Year;
            }
            else if (year < 100)
            {
                year = 0x7d0 + year;
            }
            DateTime lastWriteTime = new DateTime(year, num, day);
            if (match.Groups["hour"].Success)
            {
                int hours = Convert.ToInt32(match.Groups["hour"].Value);
                if (match.Groups["ampm"].Success && match.Groups["ampm"].Value.Equals("pm", StringComparison.OrdinalIgnoreCase))
                {
                    hours += 12;
                }
                lastWriteTime = lastWriteTime.Add(new TimeSpan(hours, Convert.ToInt32(match.Groups["min"].Value), 0));
            }
            string name = match.Groups["name"].Value;
            switch (name)
            {
                case ".":
                case "..":
                    return null;
            }
            switch (match.Groups["type"].Value.ToLower())
            {
                case "d":
                    return new FtpFolder(context, baseUri, name, lastWriteTime, parent);

                case "l":
                    return new FtpLink(context, baseUri, name, match.Groups["linktarget"].Value, lastWriteTime, parent);
            }
            return new FtpFile(context, baseUri, name, new long?(Convert.ToInt64(match.Groups["size"].Value)), lastWriteTime, parent);
        }
예제 #9
0
        public static IVirtualItem FromUrlFile(Stream urlFileContent, IVirtualFolder parent)
        {
            FtpContext context = new FtpContext();
            Uri absoluteUri = null;
            string userName = null;
            byte[] source = null;
            using (IniReader reader = new IniReader(new StreamReader(urlFileContent, Encoding.ASCII)))
            {
                while (reader.Read())
                {
                    if (reader.ElementType != IniElementType.KeyValuePair)
                    {
                        continue;
                    }
                    if (reader.CheckCurrentKey("InternetShortcut", "URL"))
                    {
                        absoluteUri = new Uri(reader.CurrentValue);
                    }
                    switch (reader.CheckCurrentKey("FTP", new string[] { "UsePassive", "UseCache", "UsePrefetch", "StoreCredential", "UploadFileNameCasing", "Encoding" }))
                    {
                        case 0:
                            context.UsePassive = Convert.ToBoolean(reader.CurrentValue);
                            break;

                        case 1:
                            context.UseCache = Convert.ToBoolean(reader.CurrentValue);
                            break;

                        case 2:
                            context.UsePrefetch = Convert.ToBoolean(reader.CurrentValue);
                            break;

                        case 3:
                            context.StoreCredential = Convert.ToBoolean(reader.CurrentValue);
                            break;

                        case 4:
                            context.UploadFileNameCasing = (CharacterCasing) System.Enum.Parse(typeof(CharacterCasing), reader.CurrentValue);
                            break;

                        case 5:
                            context.ListEncoding = Encoding.GetEncoding(reader.CurrentValue);
                            break;
                    }
                    switch (reader.CheckCurrentKey("Credential", new string[] { "UserName", "Password" }))
                    {
                        case 0:
                            userName = reader.CurrentValue;
                            break;

                        case 1:
                            source = ByteArrayHelper.Parse(reader.CurrentValue);
                            break;
                    }
                }
            }
            if (userName != null)
            {
                context.Credentials = new NetworkCredential(userName, (source == null) ? string.Empty : SimpleEncrypt.DecryptString(source, DES.Create()));
            }
            if ((absoluteUri != null) && (absoluteUri.Scheme == Uri.UriSchemeFtp))
            {
                return FromUri(context, absoluteUri, VirtualItemType.Unknown, parent);
            }
            return null;
        }