Esempio n. 1
0
        private async Task downloadAll(ImageDownloadQueue queue, IDownloader downloader, CancellationToken token)
        {
            while (true)
            {
                if (_abort)
                {
                    return;
                }

                var task = queue.PopTaskFor(downloader);
                if (task == null)
                {
                    return;
                }

                if (isAlreadyDownloaded(task))
                {
                    Console.WriteLine("[Skip] {0} {1}", task.QualityGroup.Name ?? string.Empty, task.Dir.Subdir);
                    Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count);
                    ProgressChanged?.Invoke();
                }
                else
                {
                    Interlocked.Add(ref _countInDownloadedDirs, task.FilesDownloaded.Count);
                    ProgressChanged?.Invoke();

                    bool success = await downloader.Download(task, token);

                    if (success)
                    {
                        ImageDownloadProgressReader.WriteExistingSignatures(task);
                    }
                    else
                    {
                        if (queue.PushFailedTaskBack(downloader, task))
                        {
                            Console.WriteLine("Other download source available for {0}", task.Dir.Subdir);
                        }
                        else
                        {
                            Console.WriteLine("No other download source available for {0}", task.Dir.Subdir);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private void download(ImageDownloadProgress task, Action <ImageDownloadProgress> downloader)
        {
            if (isAlreadyDownloaded(task))
            {
                Console.WriteLine("[Skip] {0}", task.Dir.Subdirectory);

                Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count);
                ProgressChanged?.Invoke();
            }
            else
            {
                Interlocked.Add(ref _countInDownloadedDirs, task.FilesDownloaded.Count);
                ProgressChanged?.Invoke();

                downloader(task);
                ImageDownloadProgressReader.WriteExistingSignatures(task);
            }
        }
Esempio n. 3
0
        public FormUpdate(
            Installer installer,
            ImageDownloader imageDownloader,
            ImageDownloadProgressReader imageDownloadProgressReader,
            NewsService newsService,
            PriceDownloader priceDownloader,
            PriceRepository priceRepository,
            ImageRepository imageRepository,
            CardRepository cardRepository,
            ImageLoader imageLoader)
            : this()
        {
            _installer                   = installer;
            _imageDownloader             = imageDownloader;
            _imageDownloadProgressReader = imageDownloadProgressReader;
            _newsService                 = newsService;
            _priceDownloader             = priceDownloader;
            _priceRepository             = priceRepository;
            _imageRepository             = imageRepository;
            _cardRepository              = cardRepository;
            _imageLoader                 = imageLoader;

            _buttonApp.Click           += appClick;
            _buttonImgLq.Click         += imgLqClick;
            _buttonImgMq.Click         += imgMqClick;
            _buttonImgArt.Click        += imgArtClick;
            _buttonMtgjson.Click       += mtgjsonClick;
            _buttonPrices.Click        += pricesClick;
            _buttonEditConfig.Click    += editConfigClick;
            _buttonNotifications.Click += notificationsClick;

            Closing       += closing;
            Closed        += closed;
            Load          += load;
            DoubleBuffered = true;

            _imageDownloader.ProgressChanged += downloadImageProgressChanged;
            _priceDownloader.SidAdded        += downloadPricesProgressChanged;
            _priceDownloader.PriceAdded      += downloadPricesProgressChanged;

            ColorSchemeController.SystemColorsChanging += systemColorsChanged;

            scale();
        }
Esempio n. 4
0
        public FormUpdate(
            Installer installer,
            ImageDownloader imageDownloader,
            ImageDownloadProgressReader imageDownloadProgressReader,
            NewsService newsService,
            ImageRepository imageRepository,
            CardRepository cardRepository,
            ImageLoader imageLoader,
            IApplication app)
            : this()
        {
            _installer                   = installer;
            _imageDownloader             = imageDownloader;
            _imageDownloadProgressReader = imageDownloadProgressReader;
            _newsService                 = newsService;
            _imageRepository             = imageRepository;
            _cardRepository              = cardRepository;
            _imageLoader                 = imageLoader;
            _app = app;

            _buttonApp.Pressed           += appClick;
            _buttonImgLq.Pressed         += imgLqClick;
            _buttonImgMq.Pressed         += imgMqClick;
            _buttonImgArt.Pressed        += imgArtClick;
            _buttonMtgjson.Pressed       += mtgjsonClick;
            _buttonPrices.Pressed        += pricesClick;
            _buttonEditConfig.Pressed    += editConfigClick;
            _buttonNotifications.Pressed += notificationsClick;

            Closing       += closing;
            Closed        += closed;
            Load          += load;
            DoubleBuffered = true;

            _imageDownloader.ProgressChanged           += downloadImageProgressChanged;
            ColorSchemeController.SystemColorsChanging += systemColorsChanged;

            RegisterDragControl(_labelTitle);

            scale();
        }
Esempio n. 5
0
        private static bool isAlreadyDownloaded(ImageDownloadProgress progress)
        {
            string targetSubdirectory = progress.TargetSubdirectory;

            Directory.CreateDirectory(targetSubdirectory);

            if (progress.FilesOnline == null)
            {
                return(false);
            }

            bool alreadyDownloaded = true;

            var existingFiles = new HashSet <string>(
                Directory.GetFiles(targetSubdirectory, "*.*", SearchOption.AllDirectories),
                Str.Comparer);

            var existingSignatures = new Dictionary <string, FileSignature>(Str.Comparer);

            foreach (var fileOnline in progress.FilesOnline.Values)
            {
                string filePath = Path.Combine(targetSubdirectory, fileOnline.Path);

                if (!existingFiles.Contains(filePath))
                {
                    alreadyDownloaded = false;
                    continue;
                }

                var existingSignature =
                    progress.FilesCorrupted.TryGet(fileOnline.Path) ??
                    progress.FilesDownloaded.TryGet(fileOnline.Path) ??
                    Signer.CreateSignature(filePath, useAbsolutePath: true).AsRelativeTo(targetSubdirectory);

                if (existingSignature.Md5Hash != fileOnline.Md5Hash)
                {
                    alreadyDownloaded = false;
                    Console.WriteLine("Deleting modified or corrupted file {0}", filePath);

                    lock (ImageLoader.SyncIo)
                    {
                        try
                        {
                            File.Delete(filePath);
                        }
                        catch (IOException ex)
                        {
                            Console.WriteLine($"Failed to remove {filePath}. {ex.Message}");
                        }
                    }
                }
                else
                {
                    existingSignatures.Add(existingSignature.Path, existingSignature);
                }
            }

            foreach (string file in existingFiles)
            {
                var relativePath = file.Substring(targetSubdirectory.Length + 1);
                if (!progress.FilesOnline.ContainsKey(relativePath) && !Str.Equals(relativePath, Signer.SignaturesFile))
                {
                    Console.WriteLine("Deleting {0}", file);
                    File.Delete(file);
                }
            }

            if (alreadyDownloaded)
            {
                ImageDownloadProgressReader.WriteExistingSignatures(progress, existingSignatures.Values);
            }

            return(alreadyDownloaded);
        }
Esempio n. 6
0
        private static bool isAlreadyDownloaded(ImageDownloadProgress progress)
        {
            FsPath targetSubdirectory = progress.TargetSubdirectory;

            targetSubdirectory.CreateDirectory();

            if (progress.FilesOnline == null)
            {
                return(false);
            }

            bool alreadyDownloaded = true;

            var existingFiles = new HashSet <FsPath>(
                targetSubdirectory.EnumerateFiles("*", SearchOption.AllDirectories));

            var existingSignatures = new Dictionary <FsPath, FileSignature>();

            foreach (var fileOnline in progress.FilesOnline.Values)
            {
                FsPath filePath = targetSubdirectory.Join(fileOnline.Path);
                if (!existingFiles.Contains(filePath))
                {
                    alreadyDownloaded = false;
                    continue;
                }

                FileSignature tempQualifier     = Signer.CreateSignature(filePath, useAbsolutePath: true);
                var           existingSignature =
                    progress.FilesCorrupted.TryGet(fileOnline.Path) ??
                    progress.FilesDownloaded.TryGet(fileOnline.Path) ??
                    new FileSignature
                {
                    Path    = tempQualifier.Path.RelativeTo(targetSubdirectory).Intern(true),
                    Md5Hash = tempQualifier.Md5Hash
                };

                if (existingSignature.Md5Hash != fileOnline.Md5Hash)
                {
                    alreadyDownloaded = false;
                    Console.WriteLine("Deleting modified or corrupted file {0}", filePath);

                    lock (ImageLoader.SyncIo)
                    {
                        try
                        {
                            filePath.DeleteFile();
                        }
                        catch (IOException ex)
                        {
                            Console.WriteLine($"Failed to remove {filePath}. {ex.Message}");
                        }
                    }
                }
                else
                {
                    existingSignatures.Add(existingSignature.Path, existingSignature);
                }
            }

            foreach (FsPath file in existingFiles)
            {
                var relativePath = file.RelativeTo(targetSubdirectory);
                if (!progress.FilesOnline.ContainsKey(relativePath) && relativePath != Signer.SignaturesFile)
                {
                    Console.WriteLine("Deleting {0}", file);
                    file.DeleteFile();
                }
            }

            if (alreadyDownloaded)
            {
                ImageDownloadProgressReader.WriteExistingSignatures(progress, existingSignatures.Values);
            }

            return(alreadyDownloaded);
        }