Beispiel #1
0
        public PreferenceFilesDownloader(string SaveDirectory, List <PreferenceFile> PreferenceFiles, ref PreferenceTrackingHandler PreferenceTracker)
        {
            this.SaveDirectory     = SaveDirectory;
            this.PreferenceFiles   = PreferenceFiles;
            this.PreferenceTracker = PreferenceTracker;

            TotalBytes = GetTotalBytes();
        }
        /// <summary>
        /// Files preference manager allows you to check the integrity of files, download missing or injured ones, as well as delete unnecessary ones and track all this
        /// </summary>
        /// <param name="SaveDirectory">Root path for downloading and checking files</param>
        /// <param name="AddressToFiles">URL address to json file</param>
        /// <param name="ValidityMode">File check mode</param>
        /// <param name="PreferenceTracker"></param>
        public FilesPreferenceManager(string SaveDirectory, Uri AddressToFiles, FileValidityMode ValidityMode, PreferenceTrackingHandler PreferenceTracker)
        {
            this.SaveDirectory     = SaveDirectory;
            this.AddressToFiles    = AddressToFiles;
            this.ValidityMode      = ValidityMode;
            this.PreferenceTracker = PreferenceTracker;

            Directory.CreateDirectory(SaveDirectory);
        }
        static void Main(string[] args)
        {
            // The path to the folder for downloading and checking files.
            string SavePath = @"C:\Sample folder";

            // Link to json file, it is generated using PreferenceGenerator.php
            Uri AddressToJson = new Uri("http://test1.ru/PreferenceFiles.json");

            // There are two modes for checking files. (Hashing/Sizing)
            // The first is more accurate, but the second is much faster.
            FileValidityMode ValidityMode = FileValidityMode.Hashing; //or FileValidityMode.Sizing;

            PreferenceTrackingHandler PreferenceTracker = new PreferenceTrackingHandler();

            PreferenceTracker.DownloadEngineStatusChanged += (s, e) => // When the download state changes.
            {
                Console.WriteLine(string.Format($"Status: {e.DownloadStatus}"));
            };

            PreferenceTracker.DownloadEngineFileChanged += (s, e) => // When the next file starts downloading.
            {
                Console.WriteLine(string.Format($"Download now: {Path.Combine(e.Directory, e.Name)}"));
            };

            PreferenceTracker.DownloadEngineProgressChanged += (s, e) => // When new bytes are received.
            {
                Console.WriteLine(string.Format($"Downloaded: {e.DownloadedSize} / {e.Size} | Percentage: {e.Percentage}"));
            };

            // Declaring the main instance of the manager class and initializing the download engine.
            FilesPreferenceManager PreferenceManager = new FilesPreferenceManager(SavePath, AddressToJson, ValidityMode, PreferenceTracker);

            PreferenceManager.InitializeDownloadEngine();

            Console.ReadKey();
        }
        /// <summary>
        /// This method allows you to check the integrity of the files, the list of which it received
        /// </summary>
        /// <param name="SaveDirectory">File check root path</param>
        /// <param name="PreferenceFiles">List of preference files</param>
        /// <param name="ValidityMode">File check mode</param>
        /// <returns>List of files edited as a result of the method</returns>
        public List <PreferenceFile> ValidateFiles(string SaveDirectory, List <PreferenceFile> PreferenceFiles, FileValidityMode ValidityMode, ref PreferenceTrackingHandler PreferenceTracker)
        {
            ///Tracking change to download status
            PreferenceTracker.DownloadEngineStatusArgs.DownloadStatus = DownloadStatusFlags.Validating;
            PreferenceTracker.DownloadEngineStatusChange(PreferenceTracker.DownloadEngineStatusArgs);

            List <PreferenceFile> PreferenceInjuredFiles = new List <PreferenceFile>();

            foreach (PreferenceFile ValidityFile in PreferenceFiles)
            {
                if (!string.IsNullOrEmpty(ValidityFile.Directory))
                {
                    Directory.CreateDirectory(Path.Combine(SaveDirectory, ValidityFile.Directory));
                }

                if (!File.Exists(Path.Combine(SaveDirectory, ValidityFile.Directory, ValidityFile.Name)))
                {
                    PreferenceInjuredFiles.Add(ValidityFile);
                    continue;
                }


                if (ValidityMode == FileValidityMode.Sizing)
                {
                    if (new FileInfo(Path.Combine(SaveDirectory, ValidityFile.Directory, ValidityFile.Name)).Length != ValidityFile.Size)
                    {
                        PreferenceInjuredFiles.Add(ValidityFile);
                    }
                }
                else if (ValidityMode == FileValidityMode.Hashing)
                {
                    MD5        Crypto = MD5.Create();
                    FileStream Stream = File.OpenRead(Path.Combine(SaveDirectory, ValidityFile.Directory, ValidityFile.Name));

                    byte[] Hashcode = Crypto.ComputeHash(Stream);
                    string Hash     = (BitConverter.ToString(Hashcode).Replace("-", string.Empty)).ToLower();

                    if (Hash != ValidityFile.Hash)
                    {
                        PreferenceInjuredFiles.Add(ValidityFile);
                    }
                }
            }
            return(PreferenceInjuredFiles);
        }