示例#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
        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
        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
 public void AssignRunner(DownloadJob downloadJob)
 {
     _downloadJobRunners.Add(downloadJob.Id, new DownloadJobRunner(_credentials, new StorageSettings {BasePath = "D:\\DDD\\"}));
 }
示例#8
0
 private void AddDownloadJob(DownloadJob downloadJob)
 {
     downloadJobs.Add(downloadJob);
     _runner.AssignRunner(downloadJob);
 }