Beispiel #1
0
        /// <summary>
        /// Starts downloading a file.
        /// </summary>
        /// <param name="remoteFile">The file to download.</param>
        /// <param name="localFilePath">The complete file path that will be used to store the downloaded file.</param>
        /// <param name="version">The file version to download.</param>
        /// <returns>True if the download was successfully started; otherwise false.</returns>
        public bool Download(string remoteFile, string localFilePath, int?version)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Attempting Download on `{0}`. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}", this,
                                remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]");
            }

            var uriPath = RootPath;

            // Add the version directory if needed
            if (version.HasValue)
            {
                uriPath = PathHelper.CombineDifferentPaths(uriPath, PathHelper.GetVersionString(version.Value));
            }

            // Add the file to get to the path
            uriPath = PathHelper.CombineDifferentPaths(uriPath, remoteFile);

            var uri = new Uri(uriPath);

            // Ensure the directory exists
            var dir = Path.GetDirectoryName(localFilePath);

            if (dir != null)
            {
                if (!Directory.Exists(dir))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Creating directory: {0}", dir);
                    }
                    Directory.CreateDirectory(dir);
                }
            }

            // Get the WebClient to use
            lock (_webClientsSync)
            {
                // Check for a free WebClient
                if (_webClients.Count == 0)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.InfoFormat(
                            "Could not start download on `{0}` since no WebClients are available. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}",
                            this, remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]");
                    }
                    return(false);
                }

                // Grab the free WebClient and start the download
                var wc = _webClients.Pop();
                wc.DownloadFileAsync(uri, localFilePath, new AsyncDownloadInfo(remoteFile, localFilePath));

                if (log.IsInfoEnabled)
                {
                    log.InfoFormat(
                        "Starting download on `{0}` using WebClient {4}. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}", this,
                        remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]", wc);
                }
            }

            return(true);
        }