Ejemplo n.º 1
0
        public void CheckThatTheDownloaderIsCalledForEachEnumeration()
        {
            var collection =
                new DownloadCollection(
                    string.Empty,
                    new List<IEnumerable<string>>
                        {
                            new List<string> { "A", "B" },
                            new List<string> { "D", "E", "F" },
                            new List<string> { "G", "H", "I" }
                        },
                        "{0}{1}{2}");

            var mockDownloader = new Mock<IDownloader>();
            mockDownloader.Setup(d => d.Download(collection, It.IsAny<string>())).Returns(new Task<bool>(() => true));
            var manager = new DownloadManager(new List<DownloadCollection> { collection })
                {
                    Downloader = new FakeDownloader() //mockDownloader.Object
                };

            manager.StartDownloading();
            var bob = false;
            while(!bob)
            {
            }
            //mockDownloader.Verify(d => d.Download(collection, It.IsAny<string>()), Times.Exactly(18));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize setup with the xml file.
        /// </summary>
        /// <param name="xmlFile">The xml file to load</param>
        /// <returns>Pass or Fail</returns>
        public bool Initalize(string xmlFile)
        {
            if (!File.Exists(xmlFile))
            {
                ServiceManager.Services.LogService.WriteLine("\"{0}\" does not exist.", ConsoleColor.Red, xmlFile);
                return(false);
            }

            FileInfo info = new FileInfo(xmlFile);

            if (!string.Equals(info.Extension, ".xml", StringComparison.OrdinalIgnoreCase))
            {
                ServiceManager.Services.LogService.WriteLine("\"{0}\" doesn't seem to be an xml file.", ConsoleColor.Red, xmlFile);
                return(false);
            }

            // Initialize Data.
            Setup result = this.Deserialize(xmlFile);

            this.Downloads = result.Downloads;
            this.Scripts   = result.Scripts;

            // Write Details.
            ServiceManager.Services.LogService.WriteSubHeader("Details");
            ServiceManager.Services.LogService.WriteLine("\"{0}\" File(s) to download.", Downloads == null ? 0 : Downloads.Count);
            ServiceManager.Services.LogService.WriteLine("\"{0}\" Script(s) to execute.", Scripts.Count);

            return(true);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Fetches the associated downloads by product id.
 /// </summary>
 /// <param name="productId">The product id.</param>
 /// <returns></returns>
 public DownloadCollection FetchAssociatedDownloadsByProductId(int productId)
 {
     IDataReader reader = SPs.FetchAssociatedDownloadsByProductId(productId).GetReader();
       DownloadCollection artifactCollection = new DownloadCollection();
       artifactCollection.LoadAndCloseReader(reader);
       return artifactCollection;
 }
Ejemplo n.º 4
0
        public void DownloadCollectionEnumeratesToCorrectValues()
        {
            var collection =
                new DownloadCollection(
                    string.Empty,
                    new List<IEnumerable<string>>
                        {
                            new List<string> { "A", "B" },
                            new List<string> { "D", "E", "F" },
                            new List<string> { "G", "H", "I" }
                        },
                        "{0}#{1}.{2}");

            var items = collection.ToArray();
            Assert.AreEqual(18, items.Count(), "The collection returned was not the right length");
            Assert.AreEqual("A#D.G", items[0]);
            Assert.AreEqual("A#D.H", items[1]);
            Assert.AreEqual("A#D.I", items[2]);
            Assert.AreEqual("A#E.G", items[3]);
            Assert.AreEqual("A#E.H", items[4]);
            Assert.AreEqual("A#E.I", items[5]);
            Assert.AreEqual("A#F.G", items[6]);
            Assert.AreEqual("A#F.H", items[7]);
            Assert.AreEqual("A#F.I", items[8]);
            Assert.AreEqual("B#D.G", items[9]);
            Assert.AreEqual("B#D.H", items[10]);
            Assert.AreEqual("B#D.I", items[11]);
            Assert.AreEqual("B#E.G", items[12]);
            Assert.AreEqual("B#E.H", items[13]);
            Assert.AreEqual("B#E.I", items[14]);
            Assert.AreEqual("B#F.G", items[15]);
            Assert.AreEqual("B#F.H", items[16]);
            Assert.AreEqual("B#F.I", items[17]);
        }
Ejemplo n.º 5
0
 public DownloadCollection FetchAll()
 {
     DownloadCollection coll = new DownloadCollection();
     Query qry = new Query(Download.Schema);
     coll.LoadAndCloseReader(qry.ExecuteReader());
     return coll;
 }
 public DownloadCollection FetchPurchasedDownloadsByUserId(Guid userId)
 {
     IDataReader reader = SPs.FetchPurchasedDownloadsByUserId(userId).GetReader();
       DownloadCollection downloadCollection = new DownloadCollection();
       downloadCollection.LoadAndCloseReader(reader);
       return downloadCollection;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// The main program method
        /// </summary>
        /// <param name="args">
        /// The arguments
        /// </param>
        public static void Main(string[] args)
        {
            var collection =
                new DownloadCollection(
                    string.Empty,
                    new List<IEnumerable<string>>
                        {
                            new List<string> { "A", "B" },
                            new List<string> { "D", "E", "F" },
                            new List<string> { "G", "H", "I" }
                        },
                        "{0}{1}{2}");
            var collection2 = new DownloadCollection(
                string.Empty,
                new List<IEnumerable<string>>
                    {
                        new List<string> { "1", "2" },
                        new List<string> { "3", "4", "5" },
                        new List<string> { "6", "7", "8" }
                    },
                "{0}{1}{2}") { Pause = 0 };

            var manager = new DownloadManager(new List<DownloadCollection> { collection, collection2 })
            {
                Downloader = new FakeDownloader(),
                LoggingAction = a => Console.WriteLine(a)
            };

            manager.StartDownloading();
            Console.WriteLine("Done");
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        public BrowserForm()
        {
            // See Program.cs for WebCore initialization.

            InitializeComponent();

            _downloads = new DownloadCollection();
        }
Ejemplo n.º 9
0
        public void PowershellScript_ResultFail()
        {
            Script script = new Script();

            script.Name = "Powershell test script.";

            // The powershell script.
            script.Executable = new Executable()
            {
                Path = Utilities.GetIncludedFile("Scripts/PowershellTest_ScriptFail.ps1")
            };

            // Exit Codes
            ExitCodeCollection exitCollection = new ExitCodeCollection();

            exitCollection.Add(new ExitCode()
            {
                Value = 0, Message = "Success", IsSuccess = true
            });
            exitCollection.Add(new ExitCode()
            {
                Value = 1, Message = "Failure"
            });

            // Add all elements into the single script file.
            script.ExitCodes = exitCollection;

            // Add the single script above into a collection of scripts.
            ScriptCollection scriptCollection = new ScriptCollection();

            scriptCollection.Add(script);

            // No Downloads.
            DownloadCollection downloadCollection = new DownloadCollection();

            // Add the 2 main elements, the scripts to run and the downloads to download.
            Setup setup = new Setup();

            setup.Scripts   = scriptCollection;
            setup.Downloads = downloadCollection;


            int exitCode = -1;

            if (setup.Initalize())
            {
                exitCode = setup.Execute();
            }

            // The exit code should be 1, since the script did not match IsSuccess ExitCode.
            Assert.IsTrue(exitCode == 1);
        }
Ejemplo n.º 10
0
        private static DownloadCollection DBMapping(DBDownloadCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            DownloadCollection collection = new DownloadCollection();
            foreach (DBDownload dbItem in dbCollection)
            {
                Download item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }
Ejemplo n.º 11
0
        public ActionResult Details(int id)
        {
            var downloads = new DownloadCollection();

            using (var database = new DatabaseEntities())
            {
                var item = database.ItemById(id);
                if (item == null)
                {
                    return(HttpNotFound());
                }
                downloads.PrimaryItem = new ItemViewModel(item);
                AddDependencies(downloads, item);
            }
            return(Json(downloads, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 12
0
 private void AddDependencies(DownloadCollection collection, Item item)
 {
     collection.Items.Add(new ItemViewModel(item));
     foreach (var blob in item.Blobs)
     {
         collection.Downloads.Add(new Download
         {
             DestinationPath = blob.DestinationPath,
             DownloadUrl     = blob.DownloadUrl
         });
     }
     foreach (var dependency in item.Dependencies)
     {
         AddDependencies(collection, dependency.Item);
     }
 }
Ejemplo n.º 13
0
        private static DownloadCollection DBMapping(DBDownloadCollection dbCollection)
        {
            if (dbCollection == null)
            {
                return(null);
            }

            DownloadCollection collection = new DownloadCollection();

            foreach (DBDownload dbItem in dbCollection)
            {
                Download item = DBMapping(dbItem);
                collection.Add(item);
            }

            return(collection);
        }
Ejemplo n.º 14
0
        private static async Task <DownloadCollection> GetDownloadables(string url, string extension)
        {
            var downloadResults = new DownloadCollection();
            var uri             = new Uri(url);
            var httpHandler     = new HttpClientHandler()
            {
                MaxConnectionsPerServer = int.MaxValue
            };

            using (var wc = new HttpClient(httpHandler))
            {
                Console.WriteLine("Scraping: " + url);
                var data = await wc.GetByteArrayAsync(url);

                if (data != null && data.Length > 0)
                {
                    var source = Encoding.UTF8.GetString(data, 0, data.Length - 1);
                    source = WebUtility.HtmlDecode(source);
                    var resultat = new HtmlDocument();
                    resultat.LoadHtml(source);

                    //Parse results
                    var resultLinks = resultat.DocumentNode.Descendants().Where
                                          (x => (x.Name == "a" && x.Attributes["href"] != null && (extension == null || x.InnerText.ToLower().EndsWith("/") || x.InnerText.ToLower().EndsWith(extension.ToLower())))).ToList();
                    foreach (var results in resultLinks)
                    {
                        var name = results.InnerText.Trim();
                        if (name.EndsWith("/") || name.Contains('.'))
                        {
                            downloadResults.Add(new DownloadResult
                            {
                                Name    = name,
                                Url     = string.Concat(url.ToString(), "/", name),
                                BaseUrl = url
                            });
                        }
                    }
                }
            }//WebClient
            return(downloadResults);
        }
Ejemplo n.º 15
0
        public void DownloadCollectionEnumeratesToCorrectValues()
        {
            var collection =
                new DownloadCollection(
                    string.Empty,
                    new List <IEnumerable <string> >
            {
                new List <string> {
                    "A", "B"
                },
                new List <string> {
                    "D", "E", "F"
                },
                new List <string> {
                    "G", "H", "I"
                }
            },
                    "{0}#{1}.{2}");

            var items = collection.ToArray();

            Assert.AreEqual(18, items.Count(), "The collection returned was not the right length");
            Assert.AreEqual("A#D.G", items[0]);
            Assert.AreEqual("A#D.H", items[1]);
            Assert.AreEqual("A#D.I", items[2]);
            Assert.AreEqual("A#E.G", items[3]);
            Assert.AreEqual("A#E.H", items[4]);
            Assert.AreEqual("A#E.I", items[5]);
            Assert.AreEqual("A#F.G", items[6]);
            Assert.AreEqual("A#F.H", items[7]);
            Assert.AreEqual("A#F.I", items[8]);
            Assert.AreEqual("B#D.G", items[9]);
            Assert.AreEqual("B#D.H", items[10]);
            Assert.AreEqual("B#D.I", items[11]);
            Assert.AreEqual("B#E.G", items[12]);
            Assert.AreEqual("B#E.H", items[13]);
            Assert.AreEqual("B#E.I", items[14]);
            Assert.AreEqual("B#F.G", items[15]);
            Assert.AreEqual("B#F.H", items[16]);
            Assert.AreEqual("B#F.I", items[17]);
        }
Ejemplo n.º 16
0
        public void CheckThatTheDownloaderIsCalledForEachEnumeration()
        {
            var collection =
                new DownloadCollection(
                    string.Empty,
                    new List <IEnumerable <string> >
            {
                new List <string> {
                    "A", "B"
                },
                new List <string> {
                    "D", "E", "F"
                },
                new List <string> {
                    "G", "H", "I"
                }
            },
                    "{0}{1}{2}");

            var mockDownloader = new Mock <IDownloader>();

            mockDownloader.Setup(d => d.Download(collection, It.IsAny <string>())).Returns(new Task <bool>(() => true));
            var manager = new DownloadManager(new List <DownloadCollection> {
                collection
            })
            {
                Downloader = new FakeDownloader()     //mockDownloader.Object
            };

            manager.StartDownloading();
            var bob = false;

            while (!bob)
            {
            }
            //mockDownloader.Verify(d => d.Download(collection, It.IsAny<string>()), Times.Exactly(18));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Generates a template xml to base custom scripts off of.
        /// </summary>
        /// <param name="savePath">The location the template will be saved.</param>
        public void GenerateXmlTemplate(string savePath)
        {
            ServiceManager.Services.LogService.WriteHeader("Generating Xml Template...");

            #region Script 1
            Script script = new Script();
            script.Name        = "Script 1";
            script.Description = "Does nothing";

            Executable executable = new Executable();
            executable.Path = "C:/Temp/nothing.ps1";

            ArgumentCollection argCollection = new ArgumentCollection();
            argCollection.Add(new Argument()
            {
                Key = "-i", Value = "C:/Temp/something.bin"
            });
            argCollection.Add(new Argument()
            {
                Key = "-x", Value = ""
            });

            ExitCodeCollection exitCollection = new ExitCodeCollection();
            exitCollection.Add(new ExitCode()
            {
                Value = 0, Message = "Files deleted", IsSuccess = true
            });
            exitCollection.Add(new ExitCode()
            {
                Value = 1, Message = "Files failed to delete"
            });
            exitCollection.Add(new ExitCode()
            {
                Value = 2, Message = "Couldn't find any files"
            });

            // Add all elements into the single script file.
            script.Arguments  = argCollection;
            script.ExitCodes  = exitCollection;
            script.Executable = executable;
            #endregion

            #region Script 2
            Script script2 = new Script();
            script2.Name        = "Script 2";
            script2.Description = "Does nothing";

            Executable executable2 = new Executable();
            executable2.Path = "C:/Temp/Downloads/Extracted/SomethingThatWasInAZip.exe";

            ArgumentCollection argCollection2 = new ArgumentCollection();
            argCollection2.Add(new Argument()
            {
                Key = "-install"
            });
            argCollection2.Add(new Argument()
            {
                Key = "-silent"
            });

            ExitCodeCollection exitCollection2 = new ExitCodeCollection();
            exitCollection2.Add(new ExitCode()
            {
                Value = 0, Message = "Script 2 has installed", IsSuccess = true
            });
            exitCollection2.Add(new ExitCode()
            {
                Value = 1, Message = "Failed to install."
            });
            exitCollection2.Add(new ExitCode()
            {
                Value = 2, Message = "Installed but limited."
            });

            // Add all elements into the single script file.
            script2.Arguments  = argCollection;
            script2.ExitCodes  = exitCollection;
            script2.Executable = executable;

            // Add the single script above into a collection of scripts.
            ScriptCollection scriptCollection = new ScriptCollection();
            scriptCollection.Add(script);
            scriptCollection.Add(script2);
            #endregion

            ServiceManager.Services.LogService.WriteLine("Generating Download Collection...");
            DownloadCollection downloadCollection = new DownloadCollection();
            downloadCollection.TimeOut     = 60;
            downloadCollection.RefreshRate = 10;
            downloadCollection.Add(new Download()
            {
                Name = "Nothing Powershell Script", Description = "This script does nothing", DownloadUrl = "www.blank.com/nothing.ps1", DestinationPath = "C:/Temp/Downloads/nothing.ps1"
            });
            downloadCollection.Add(new Download()
            {
                Name = "Test Zip File", Description = "This zip has nothing", DownloadUrl = "www.blank.com/nothing.zip", DestinationPath = "C:/Temp/Downloads", ExtractionPath = "C:/Temp/Downloads/Extracted"
            });

            // Add the 2 main elements, the scripts to run and the downloads to download.
            Setup setup = new Setup();
            setup.Scripts   = scriptCollection;
            setup.Downloads = downloadCollection;

            ServiceManager.Services.LogService.WriteLine("Saving file to: \"{0}\"", savePath);
            setup.Serialize(savePath);
        }
Ejemplo n.º 18
0
        public void PowershellScript_Both()
        {
            #region Script 1
            Script script1 = new Script();
            script1.Name       = "Powershell test script 1.";
            script1.Executable = new Executable()
            {
                Path = Utilities.GetIncludedFile("Scripts/PowershellTest_ScriptPass.ps1")
            };

            // Exit Codes
            ExitCodeCollection exitCollection1 = new ExitCodeCollection();
            exitCollection1.Add(new ExitCode()
            {
                Value = 0, Message = "Success", IsSuccess = true
            });
            exitCollection1.Add(new ExitCode()
            {
                Value = 1, Message = "Failure"
            });

            // Add all elements into the script1
            script1.ExitCodes = exitCollection1;
            #endregion

            #region Script 2
            Script script2 = new Script();
            script2.Name       = "Powershell test script 2.";
            script2.Executable = new Executable()
            {
                Path = Utilities.GetIncludedFile("Scripts/PowershellTest_ScriptFail.ps1")
            };

            // Exit Codes
            ExitCodeCollection exitCollection2 = new ExitCodeCollection();
            exitCollection2.Add(new ExitCode()
            {
                Value = 0, Message = "Success", IsSuccess = true
            });
            exitCollection2.Add(new ExitCode()
            {
                Value = 1, Message = "Failure"
            });

            // Add all elements into the script1
            script2.ExitCodes = exitCollection2;
            #endregion

            // Add the single script above into a collection of scripts.
            ScriptCollection scriptCollection = new ScriptCollection();
            scriptCollection.Add(script1);
            scriptCollection.Add(script2);

            // No Downloads.
            DownloadCollection downloadCollection = new DownloadCollection();

            // Add the 2 main elements, the scripts to run and the downloads to download.
            Setup setup = new Setup();
            setup.Scripts   = scriptCollection;
            setup.Downloads = downloadCollection;


            int exitCode = -1;
            if (setup.Initalize())
            {
                exitCode = setup.Execute();
            }

            // Exit code should be 1 as 1 of the 2 scripts didn't have the expected result.
            Assert.IsTrue(exitCode == 1);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Neue Write()
        /// 03.07.2009 Lars
        /// 04.07.2009 Lars (!HasInformation und SectorsMap)
        /// 09.07.2009 Lars (Falls StealthNet abstürzt, wird die downloads.xml nicht mehr "gekillt")
        /// </summary>
        public static void Write(string downloadsFilePath, DownloadCollection downloads)
        {
            if (downloadsFilePath == null)
                throw new ArgumentNullException("downloadsFilePath");
            if (downloadsFilePath == string.Empty)
                throw new ArgumentException();
            if (downloads == null)
                throw new ArgumentNullException("downloads");

            if (!m_IsReady)
                return;
            try
            {
                try
                {
                    if (File.Exists(downloadsFilePath))
                    {
                        string backupFilePath = string.Format("{0}.bak", downloadsFilePath);
                        if (File.Exists(backupFilePath))
                        {
                            string backupBackupFilePath = string.Format("{0}.bak", backupFilePath);
                            if (File.Exists(backupBackupFilePath))
                                File.Delete(backupBackupFilePath);
                            File.Move(backupFilePath, backupBackupFilePath);
                        }
                        File.Move(downloadsFilePath, backupFilePath);
                    }
                }
                catch (Exception ex)
                {
                    m_Logger.Log(ex, "An exception was thrown while archiving the downloads.xml!");
                }
                XmlWriterSettings downloadsXmlWriterSettings = new XmlWriterSettings();
                downloadsXmlWriterSettings.CloseOutput = true;
                downloadsXmlWriterSettings.Indent = true;
                MemoryStream memoryStream = new MemoryStream();
                XmlWriter downloadsXmlWriter = XmlWriter.Create(memoryStream, downloadsXmlWriterSettings);
                downloadsXmlWriter.WriteStartDocument();
                downloadsXmlWriter.WriteStartElement("downloads");
                foreach (Download download in downloads.Values)
                {
                    string fileHashString = download.FileHashString;
                    long fileSize = download.FileSize;
                    string fileName = download.FileName;
                    DateTime? lastSeen = download.LastSeen;
                    DateTime? lastReception = download.LastReception;
                    string subFolder = download.SubFolder;
                    byte[] sectorsMap = download.SectorsMap;
                    if (fileHashString == null || fileHashString.Length != 128 || fileSize < 0 ||
                        fileName == null || fileName == string.Empty || (download.HasInformation && sectorsMap == null))
                    {
                        m_Logger.Log("A download could not be saved because of invalid information about it!\n\nFile Hash: {0}\nFile Size: {1}\nFile Name: {2}\nSectors Map: {3}", fileHashString, fileSize, fileName, sectorsMap);
                        continue;
                    }
                    downloadsXmlWriter.WriteStartElement("download");
                    downloadsXmlWriter.WriteAttributeString("hash", fileHashString);
                    if (!download.HasInformation)
                        downloadsXmlWriter.WriteAttributeString("hasinformation", "none");
                    downloadsXmlWriter.WriteStartElement("filesize");
                    downloadsXmlWriter.WriteValue(fileSize);
                    downloadsXmlWriter.WriteEndElement();
                    if (download.HasInformation)
                    {
                        downloadsXmlWriter.WriteStartElement("sectorsmap");
                        downloadsXmlWriter.WriteValue(Convert.ToBase64String(sectorsMap));
                        downloadsXmlWriter.WriteEndElement();
                    }
                    downloadsXmlWriter.WriteStartElement("filename");
                    downloadsXmlWriter.WriteValue(fileName);
                    downloadsXmlWriter.WriteEndElement();
                    if (lastSeen != null && lastSeen.HasValue)
                    {
                        downloadsXmlWriter.WriteStartElement("lastseen");
                        downloadsXmlWriter.WriteValue(lastSeen.Value.ToString());
                        downloadsXmlWriter.WriteEndElement();
                    }
                    if (lastReception != null && lastReception.HasValue)
                    {
                        downloadsXmlWriter.WriteStartElement("lastreception");
                        downloadsXmlWriter.WriteValue(lastReception.Value.ToString());
                        downloadsXmlWriter.WriteEndElement();
                    }
                    if (subFolder != null && subFolder != string.Empty)
                    {
                        downloadsXmlWriter.WriteStartElement("subfolder");
                        downloadsXmlWriter.WriteValue(subFolder);
                        downloadsXmlWriter.WriteEndElement();
                    }
                    downloadsXmlWriter.WriteEndElement();
                }
                downloadsXmlWriter.WriteEndElement();
                downloadsXmlWriter.WriteEndDocument();
                downloadsXmlWriter.Flush();
                FileStream fileStream = new FileStream(downloadsFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                byte[] buffer = memoryStream.ToArray();
                fileStream.Write(buffer, 0, buffer.Length);
                downloadsXmlWriter.Close();
                fileStream.Flush();
                fileStream.Close();
            }
            catch (Exception ex)
            {
                m_Logger.Log(ex, "An exception was thrown while writing the downloads.xml!");
            }
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Starts the download
 /// </summary>
 /// <param name="collection">
 /// The collection.
 /// </param>
 /// <param name="file">
 /// The name of the file to download.
 /// </param>
 /// <returns>
 /// Whether to continue or move to the next enumeration
 /// </returns>
 public async Task<bool> Download(DownloadCollection collection, string file)
 {
     await Task.Factory.StartNew(() => Thread.Sleep(5000));
     return true;
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Starts the download
        /// </summary>
        /// <param name="collection">
        /// The collection.
        /// </param>
        /// <param name="file">
        /// The name of the file to download.
        /// </param>
        /// <returns>
        /// Whether to continue or move to the next enumeration
        /// </returns>
        public async Task<bool> Download(DownloadCollection collection, string file)
        {
            bool returnValue = false;
            string remoteFile = Path.Combine(collection.RemotePath, file);
            string localFile = Path.Combine(collection.LocalPath, file);
            var request = (HttpWebRequest)WebRequest.Create(remoteFile);

            request.Credentials = new NetworkCredential(collection.UserName, collection.Password);

            Stream reader = null;
            FileStream fileStream = null;
            try
            {
                int startPosition = FileLength(localFile);

                request.AddRange(startPosition);
                var response = await request.GetResponseAsync() as HttpWebResponse;

                if (response != null && (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.PartialContent))
                {
                    Console.WriteLine("Downloading {0} ({1} bytes)", file, response.ContentLength);
                    reader = response.GetResponseStream();

                    fileStream = new FileStream(localFile, FileMode.Append, FileAccess.Write, FileShare.None);

                    if (reader != null)
                    {
                        // It will store the current number of bytes we retrieved from the server
                        int bytesSize;

                        // A buffer for storing and writing the data retrieved from the server
                        var downBuffer = new byte[2048];
                        int offset = 0;
                        Console.WriteLine();
                        int totalSize = (int)fileStream.Length + (int)response.ContentLength;

                        // Loop through the buffer until the buffer is empty
                        while ((bytesSize = reader.Read(downBuffer, offset, downBuffer.Length)) > 0)
                        {
                            fileStream.Write(downBuffer, 0, bytesSize);
                            offset = 0;
                        }

                        Console.WriteLine();
                        Console.WriteLine("Success: {0}", remoteFile);
                        returnValue = true;
                    }
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine("File error: {0}", remoteFile);
                Console.Write(ex.Message);
                returnValue = true;
            }
            catch (WebException ex)
            {
                Console.WriteLine("Skipping file: {0}", file);
                returnValue = ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed: {0}", remoteFile);
                Console.Write(ex.Message);
                returnValue = false;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }

            return returnValue;
        }
Ejemplo n.º 22
0
 public DownloadCollection FetchByID(object DownloadId)
 {
     DownloadCollection coll = new DownloadCollection().Where("DownloadId", DownloadId).Load();
     return coll;
 }
Ejemplo n.º 23
0
 public DownloadCollection FetchByQuery(Query qry)
 {
     DownloadCollection coll = new DownloadCollection();
     coll.LoadAndCloseReader(qry.ExecuteReader());
     return coll;
 }
Ejemplo n.º 24
0
        /// <summary>
        /// Process a <see cref="DownloadCollection"/>  collection.
        /// </summary>
        /// <param name="collection">
        /// The collection to process
        /// </param>
        /// <param name="semaphore">The semaphore to limit the concurrent tasks</param>
        private void ProcessCollection(DownloadCollection collection, SemaphoreSlim semaphore)
        {
            foreach (var downloadItem in collection)
            {
                string item = downloadItem;
                Task.Factory.StartNew(
                    async () =>
                    {
                        semaphore.Wait();
                        Debug.WriteLine("Debugging");
                        InvokeResponse("Downloading {0}", item);
                        await Downloader.Download(collection, item);
                        InvokeResponse("Releasing {0}", item);
                        Interlocked.Decrement(ref this._counter);
                        semaphore.Release();
                    });

                if (collection.Pause > 0)
                {
                    InvokeResponse("Waiting {0} seconds after {1}", collection.Pause, item);
                    Thread.Sleep(collection.Pause * 1000);
                }
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Neue Write()
        /// 03.07.2009 Lars
        /// 04.07.2009 Lars (!HasInformation und SectorsMap)
        /// 09.07.2009 Lars (Falls StealthNet abstürzt, wird die downloads.xml nicht mehr "gekillt")
        /// </summary>
        public static void Write(string downloadsFilePath, DownloadCollection downloads)
        {
            if (downloadsFilePath == null)
            {
                throw new ArgumentNullException("downloadsFilePath");
            }
            if (downloadsFilePath == string.Empty)
            {
                throw new ArgumentException();
            }
            if (downloads == null)
            {
                throw new ArgumentNullException("downloads");
            }

            if (!m_IsReady)
            {
                return;
            }
            try
            {
                try
                {
                    if (File.Exists(downloadsFilePath))
                    {
                        string backupFilePath = string.Format("{0}.bak", downloadsFilePath);
                        if (File.Exists(backupFilePath))
                        {
                            string backupBackupFilePath = string.Format("{0}.bak", backupFilePath);
                            if (File.Exists(backupBackupFilePath))
                            {
                                File.Delete(backupBackupFilePath);
                            }
                            File.Move(backupFilePath, backupBackupFilePath);
                        }
                        File.Move(downloadsFilePath, backupFilePath);
                    }
                }
                catch (Exception ex)
                {
                    m_Logger.Log(ex, "An exception was thrown while archiving the downloads.xml!");
                }
                XmlWriterSettings downloadsXmlWriterSettings = new XmlWriterSettings();
                downloadsXmlWriterSettings.CloseOutput = true;
                downloadsXmlWriterSettings.Indent      = true;
                MemoryStream memoryStream       = new MemoryStream();
                XmlWriter    downloadsXmlWriter = XmlWriter.Create(memoryStream, downloadsXmlWriterSettings);
                downloadsXmlWriter.WriteStartDocument();
                downloadsXmlWriter.WriteStartElement("downloads");
                foreach (Download download in downloads.Values)
                {
                    string   fileHashString = download.FileHashString;
                    long     fileSize       = download.FileSize;
                    string   fileName       = download.FileName;
                    DateTime?lastSeen       = download.LastSeen;
                    DateTime?lastReception  = download.LastReception;
                    string   subFolder      = download.SubFolder;
                    byte[]   sectorsMap     = download.SectorsMap;
                    if (fileHashString == null || fileHashString.Length != 128 || fileSize < 0 ||
                        fileName == null || fileName == string.Empty || (download.HasInformation && sectorsMap == null))
                    {
                        m_Logger.Log("A download could not be saved because of invalid information about it!\n\nFile Hash: {0}\nFile Size: {1}\nFile Name: {2}\nSectors Map: {3}", fileHashString, fileSize, fileName, sectorsMap);
                        continue;
                    }
                    downloadsXmlWriter.WriteStartElement("download");
                    downloadsXmlWriter.WriteAttributeString("hash", fileHashString);
                    if (!download.HasInformation)
                    {
                        downloadsXmlWriter.WriteAttributeString("hasinformation", "none");
                    }
                    downloadsXmlWriter.WriteStartElement("filesize");
                    downloadsXmlWriter.WriteValue(fileSize);
                    downloadsXmlWriter.WriteEndElement();
                    if (download.HasInformation)
                    {
                        downloadsXmlWriter.WriteStartElement("sectorsmap");
                        downloadsXmlWriter.WriteValue(Convert.ToBase64String(sectorsMap));
                        downloadsXmlWriter.WriteEndElement();
                    }
                    downloadsXmlWriter.WriteStartElement("filename");
                    downloadsXmlWriter.WriteValue(fileName);
                    downloadsXmlWriter.WriteEndElement();
                    if (lastSeen != null && lastSeen.HasValue)
                    {
                        downloadsXmlWriter.WriteStartElement("lastseen");
                        downloadsXmlWriter.WriteValue(lastSeen.Value.ToString());
                        downloadsXmlWriter.WriteEndElement();
                    }
                    if (lastReception != null && lastReception.HasValue)
                    {
                        downloadsXmlWriter.WriteStartElement("lastreception");
                        downloadsXmlWriter.WriteValue(lastReception.Value.ToString());
                        downloadsXmlWriter.WriteEndElement();
                    }
                    if (subFolder != null && subFolder != string.Empty)
                    {
                        downloadsXmlWriter.WriteStartElement("subfolder");
                        downloadsXmlWriter.WriteValue(subFolder);
                        downloadsXmlWriter.WriteEndElement();
                    }
                    downloadsXmlWriter.WriteEndElement();
                }
                downloadsXmlWriter.WriteEndElement();
                downloadsXmlWriter.WriteEndDocument();
                downloadsXmlWriter.Flush();
                FileStream fileStream = new FileStream(downloadsFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                byte[]     buffer     = memoryStream.ToArray();
                fileStream.Write(buffer, 0, buffer.Length);
                downloadsXmlWriter.Close();
                fileStream.Flush();
                fileStream.Close();
            }
            catch (Exception ex)
            {
                m_Logger.Log(ex, "An exception was thrown while writing the downloads.xml!");
            }
        }