Exemplo n.º 1
0
        private async Task DownloadAsyncInternal(CancellationToken ct)
        {
            var headRequest = WebRequest.CreateHttp(Source);

            headRequest.Method            = "HEAD";
            headRequest.AllowAutoRedirect = false;

            var partFileInfo = new FileInfo(PartFilename);
            var tryResume    = (State == DownloadState.Paused) && _lastRequestDate.HasValue;

            if (tryResume)
            {
                headRequest.IfModifiedSince = _lastRequestDate.Value;
            }
            else if (partFileInfo.Exists)
            {
                partFileInfo.Delete();
            }

            State = DownloadState.Downloading;
            var getRequest = WebRequest.CreateHttp(Source);

            // Send HEAD to investigate the headers and prepare proper GET request
            using (var response = (HttpWebResponse)await headRequest.GetResponseAsync())
            {
                if (tryResume && ((response.StatusCode == HttpStatusCode.NotModified) ||
                                  ((response.LastModified != default(DateTime)) &&
                                   (response.LastModified < _lastRequestDate.Value))))
                {
                    if (partFileInfo.Exists)
                    {
                        DownloadedBytes = partFileInfo.Length;
                        getRequest.AddRange("bytes", partFileInfo.Length, TotalBytes);
                    }
                }
                else
                {
                    TotalBytes  = response.ContentLength;
                    CanBePaused =
                        string.Compare(response.Headers["Accept-Ranges"], "bytes", StringComparison.OrdinalIgnoreCase) ==
                        0;
                }
            }

            ct.ThrowIfCancellationRequested();
            // Actual download (GET)
            using (var response = (HttpWebResponse)await getRequest.GetResponseAsync())
            {
                _lastRequestDate = DateTime.UtcNow;
                var responseStream = response.GetResponseStream();
                if (responseStream == null)
                {
                    throw new IOException("Cannot get HTTP response stream");
                }

                // Append new data or create new file by default
                var fileMode = FileMode.Append;

                // If Content-Length doesn't match already known size, start downloading from scratch
                if (response.ContentLength != TotalBytes - DownloadedBytes)
                {
                    DownloadedBytes = 0;
                    TotalBytes      = response.ContentLength;
                    fileMode        = FileMode.Truncate;
                    EtaCalculator.Reset();
                }

                using (var fileStream = new FileStream(PartFilename, fileMode, FileAccess.Write, FileShare.None))
                {
                    var buffer = new byte[64 * 1024];

                    int read;
                    while (
                        (read = await responseStream.ReadAsync(buffer, 0, buffer.Length, ct)) > 0)
                    {
                        await fileStream.WriteAsync(buffer, 0, read, ct);

                        DownloadedBytes += read;
                        EtaCalculator.Update(ProgressValue);
                        OnDownloadProgressChanged();
                    }
                }
                if (File.Exists(TargetFileName))
                {
                    File.Delete(TargetFileName);
                }
                File.Move(PartFilename, TargetFileName);
            }
        }
Exemplo n.º 2
0
        public Task ProcessSelectedDocumentsAsync(IProgress <TaskProgress> prog, CancellationToken token, List <DocumentTypes> types, EtaCalculator pc, List <FileInfo> files)
        {
            var currentFile = 0.0;
            var fileCount   = Convert.ToDouble(files.Count);

            return(Task.Run(() =>
            {
                foreach (var file in files)
                {
                    var criteriaMatches = types.Select(o => new CriteriaMatchModel {
                        DocumentType = o
                    }).ToList();
                    using (var observedImage = CvInvoke.Imread(file.FullName))
                    {
                        Parallel.ForEach(_criteriaImages, (criteriaImage) =>
                        {
                            var criteriaFile = criteriaImage.Info;
                            var criteriaFileSplit = criteriaFile.Name.Split('-');
                            var type = types.First(c => c.DocumentType == criteriaFileSplit[0]);
                            var score = Classify(criteriaImage, observedImage);
                            var existingModel = criteriaMatches.First(c => c.DocumentType == type);
                            existingModel.Score += score;
                            existingModel.PdfFile = file.FullName;
                        });
                    }
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    var matchedCriteria = criteriaMatches.First(c => c.Score == criteriaMatches.Max(p => p.Score));
                    Console.WriteLine($"Score: {matchedCriteria.Score}");
                    if (matchedCriteria.Score >= matchedCriteria.DocumentType.AverageScore)
                    {
                        DocumentSelectionList.First(c => c.DocumentType == matchedCriteria.DocumentType.DocumentType).Matches += 1;
                        System.Windows.Application.Current.Dispatcher.Invoke(() =>
                        {
                            SelectionViewSource.Refresh();
                        });
                        var matchedFileName = file.Name.Substring(0, file.Name.Length - 4).Split('.')[0];
                        var matchedFile = PdfFiles.First(c => c.Contains(matchedFileName));
                        var matchedFileExtension = matchedFile.Substring(matchedFile.Length - 3);
                        if (matchedFileExtension.Equals("pdf", StringComparison.CurrentCultureIgnoreCase))
                        {
                            ExtractPageFromPdf(file, matchedCriteria.DocumentType.DocumentType, NamingModels);
                        }
                        else
                        {
                            CreatePdfFromImage(file, matchedCriteria.DocumentType.DocumentType, NamingModels);
                        }
                    }
                    currentFile++;
                    var rawProgress = (currentFile / fileCount);
                    var progress = rawProgress * 100;
                    var progressFloat = (float)rawProgress;
                    pc.Update(progressFloat);
                    if (pc.ETAIsAvailable)
                    {
                        var timeRemaining = pc.ETR.ToString(@"dd\.hh\:mm\:ss");
                        prog.Report(new TaskProgress
                        {
                            ProgressText = file.Name,
                            ProgressPercentage = progress,
                            ProgressText2 = timeRemaining
                        });
                    }
                    else
                    {
                        prog.Report(new TaskProgress
                        {
                            ProgressText = file.Name,
                            ProgressPercentage = progress,
                            ProgressText2 = "Calculating..."
                        });
                    }
                }
            }));
        }