Example #1
0
        public bool IsValid(string path, string sha256)
        {
            if (string.IsNullOrWhiteSpace(sha256))
            {
                return(false);
            }

            if (!_fileSystem.FileExists(path))
            {
                _logger.Debug("No existing download was found");
                return(false);
            }

            try
            {
                _checksumService.ValidateHash(path, sha256);
                _logger.Debug($"Installer is already downloaded: {path}");

                return(true);
            }
            catch (ChecksumVerificationException)
            {
                _logger.Warn("Checksum verification failed. ignoring cache.");
            }

            return(false);
        }
Example #2
0
        private async Task <string> TransferFile(string source, string destinationFolder, string sha256)
        {
            _hub.Publish(new FileTransferStartedEvent(source, destinationFolder));
            _logger.Debug($"Transferring file from {source} to {destinationFolder}");
            var client   = GetClient(source);
            var fileName = await client.GetFileName(source);

            var destinationPath = Path.Combine(destinationFolder, fileName);

            if (_transferCacheService.IsValid(destinationPath, sha256))
            {
                _logger.Info("Skipping download. Using already downloaded file.");
            }
            else
            {
                var progressCallback = new Action <ProgressUpdatedEvent>(p => _hub.Publish(p));

                Console.WriteLine();
                _logger.Info($"Downloading installer from {source}");
                await client.TransferFile(source, destinationPath, progressCallback);

                _logger.Debug($"Installer downloaded to {destinationPath}");

                if (sha256 == null)
                {
                    _logger.Debug("No hash provided. skipping checksum validation");
                }
                else
                {
                    _checksumService.ValidateHash(destinationPath, sha256);
                }
            }

            _hub.Publish(new FileTransferCompletedEvent(source, destinationFolder));

            return(destinationPath);
        }