コード例 #1
0
        private async Task<DownloadInfo> StartNewDownload(DownloadJob job)
        {
            var cookies = await _httpClient.GetCookiesAsync(_credentials);
            var webFileInfo = await _httpClient.GetFileInfoAsync(job.ExFileId.Value, cookies);

            var range = new Range
            {
                From = Position.From(job.BytesDownloaded),
                To = Position.From((int)webFileInfo.FileSize - 1),
            };

            var stream = await _httpClient.DownloadFileAsync(job.ExFileId.Value, cookies, range);
            
            var cts = new CancellationTokenSource();

            RegisterTokenSource(job.Id, cts);

            var ct = cts.Token;
            var downloadInfo = DownloadInfo.Default;

            Action<int> UIAction = pr => { };
            
            try
            {
                var storage = _storageAllocator.GetInstance(webFileInfo.FileName, startNew: job.Status == JobStatus.New);

                ValidateFile(storage, job);

                job.MarkAsStarted();

                await _downloader.Download(storage, UIAction, downloadInfo.UpdateBytesRead, ct, stream);

                job.MarkAsFinished();

                var intergrityValidator = new IntegrityValidator(storage);

                var isValid = await intergrityValidator.ValidateHash(job.ExpectedHash);

                if (!isValid)
                {
                    throw new Exception("File hash is wrong");
                }

                return downloadInfo.AsFinished();
            }
            catch (OperationCanceledException)
            {
                return downloadInfo;
            }
            finally
            {
                RemoveUsedToken(job.Id);
            }
        }
コード例 #2
0
 public Task<DownloadInfo> Run(DownloadJob job)
 {
     switch (job.Status)
     {
         case JobStatus.New:
             return StartNewDownload(job);
         case JobStatus.InProgress:
             return StartNewDownload(job);
         case JobStatus.Finished:
             return GetFinishedObject(job);
         default:
             throw new ArgumentOutOfRangeException();
     }
 }
コード例 #3
0
ファイル: Runner.cs プロジェクト: klym1/ex.ua-client
        public async Task Run(DownloadJob downloadJob)
        {
            DownloadJobRunner runner;

            if (_downloadJobRunners.TryGetValue(downloadJob.Id, out runner))
            {
                var downloadInfo = await runner.Run(downloadJob);

                downloadJob.UpdateStatus(downloadInfo, DateTime.UtcNow);
            }
            else
            {
                throw new Exception("Runner not registered");
            } 
        }
コード例 #4
0
ファイル: SyncJobManager.cs プロジェクト: klym1/ex.ua-client
        public Task StartOrResume(Guid downloadjobGuid)
        {
            var existingJob = downloadJobs.FirstOrDefault(it => it.Id == downloadjobGuid);

            if (existingJob != null)
            {
                return _runner.Run(existingJob);
            }

            var md5Hash = Converters.ToMd5HexByteArray("63406451651e240e8b0e1b586ff34f89");

            var downloadJob = new DownloadJob(new ExFileId(178595480), md5Hash, downloadjobGuid);
            AddDownloadJob(downloadJob);
                
            return _runner.Run(downloadJob);
        }
コード例 #5
0
 private Task<DownloadInfo> GetFinishedObject(DownloadJob downloadJob)
 {
     return Task.FromResult(new DownloadInfo {BytesRead = downloadJob.BytesDownloaded}.AsFinished());
 }
コード例 #6
0
        private void ValidateFile(Storage storage, DownloadJob job)
        {
            if (job.Status == JobStatus.InProgress || job.Status == JobStatus.Finished)
            {
                if (!storage.FileExists())
                {
                    throw new FileNotFoundException("File not found", storage.FileName);
                }

                var actualFileSize = storage.GetFileSize();

                if (actualFileSize != job.BytesDownloaded)
                {
                    throw new Exception("Actual bytes number is wrong");
                }
            }
        }
コード例 #7
0
ファイル: Runner.cs プロジェクト: klym1/ex.ua-client
 public void AssignRunner(DownloadJob downloadJob)
 {
     _downloadJobRunners.Add(downloadJob.Id, new DownloadJobRunner(_credentials, new StorageSettings {BasePath = "D:\\DDD\\"}));
 }
コード例 #8
0
ファイル: SyncJobManager.cs プロジェクト: klym1/ex.ua-client
 private void AddDownloadJob(DownloadJob downloadJob)
 {
     downloadJobs.Add(downloadJob);
     _runner.AssignRunner(downloadJob);
 }