Click here to Skip to main content
15,894,017 members
Articles / Multimedia / GDI+

MyDownloader: A Multi-thread C# Segmented Download Manager

Rate me:
Please Sign up or sign in to vote.
4.95/5 (418 votes)
12 Feb 2008CPOL10 min read 2.2M   124.8K   1.3K  
Sample application that manages multiple segmented downloads and supports HTTP, FTP and YouTube video downloads
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace MyDownloader.Core
{
    [Serializable]
    public class ResourceLocation
    {
        #region Fields
        
        private string url;
        private bool authenticate;
        private string login;
        private string password;
        private Type protocolProviderType;
        private IProtocolProvider provider;

        #endregion

        #region Constructor
        public ResourceLocation()
        {
        }

        public static ResourceLocation FromURL(string url)
        {
            ResourceLocation rl = new ResourceLocation();
            rl.URL = url;
            return rl;
        }

        public static ResourceLocation[] FromURLArray(string[] urls)
        {
            List<ResourceLocation> result = new List<ResourceLocation>();

            for (int i = 0; i < urls.Length; i++)
            {
                if (IsURL(urls[i]))
                {
                    result.Add(ResourceLocation.FromURL(urls[i]));
                }
            }

            return result.ToArray();
        }

        public static ResourceLocation FromURL(
            string url,
            bool authenticate,
            string login,
            string password)
        {
            ResourceLocation rl = new ResourceLocation();
            rl.URL = url;
            rl.Authenticate = authenticate;
            rl.Login = login;
            rl.Password = password;
            return rl;
        } 
        #endregion

        #region Properties

        public string URL
        {
            get { return url; }
            set 
            { 
                url = value;
                BindProtocolProviderType();
            }
        }

        public bool Authenticate
        {
            get { return authenticate; }
            set { authenticate = value; }
        }

        public string Login
        {
            get { return login; }
            set { login = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string ProtocolProviderType
        {
            get
            {
                if (protocolProviderType == null)
                {
                    return null;
                }
                return protocolProviderType.AssemblyQualifiedName;
            }
            set
            {
                if (value == null)
                {
                    BindProtocolProviderType();
                }
                else
                {
                    protocolProviderType = Type.GetType(value);
                }
            }
        }

        #endregion

        #region Methods

        public IProtocolProvider GetProtocolProvider(Downloader downloader)
        {
            return BindProtocolProviderInstance(downloader);
        }

        public void BindProtocolProviderType()
        {
            provider = null;

            if (! String.IsNullOrEmpty(this.URL))
            {
                protocolProviderType = ProtocolProviderFactory.GetProviderType(this.URL);                
            }            
        }

        public IProtocolProvider BindProtocolProviderInstance(Downloader downloader)
        {
            if (protocolProviderType == null)
            {
                BindProtocolProviderType();
            }

            if (provider == null)
            {
                provider = ProtocolProviderFactory.CreateProvider(protocolProviderType, downloader);
            }

            return provider;
        }

        public ResourceLocation Clone()
        {
            return (ResourceLocation)this.MemberwiseClone();
        }

        public override string ToString()
        {
            return this.URL;
        }

        public static bool IsURL(string url)
        {
            Match m = Regex.Match(url, @"(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*");
            if (m.ToString() != string.Empty)
            {
                return true;
            }
            return false;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions