Пример #1
0
        private void AddCore(DownloadLink link)
        {
            var tokeSource   = new CancellationTokenSource();
            var tasks        = link.Downloader.EmitTasks(link.Request, tokeSource.Token);
            var downloadTask = new DownloadTask(tasks, tokeSource.Token);
            var box          = new DownloadBox(downloadTask, link)
            {
                TokenSource = tokeSource
            };

            box.Canceled           += Box_Canceled;
            downloadTask.MovedNext += (a, b) =>
            {
                MovedNext?.Invoke(this, a, b);
            };
            downloadTask.Seeked += (a, b) =>
            {
                Seeked?.Invoke(this, a, b);
            };
            downloadTask.Done += async w =>
            {
                await RemoveAsync(box.Address);

                Done?.Invoke(this, box);
            };
            downloadMap.Add(link.Request.Entity.ComicUrl, box);
            downloadManager.Add(downloadTask);
            Added?.Invoke(this, box);
        }
Пример #2
0
        public bool GetDownloadHandler(IWebBrowser browser, string mimeType, string fileName, long contentLength, ref IDownloadHandler handler)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.FileName = fileName;
            string strExt = Path.GetExtension(fileName);

            dialog.Filter             = string.Format("{0} 文件(*{0})|*{0}", strExt);
            dialog.RestoreDirectory   = true;
            dialog.OverwritePrompt    = true;
            dialog.CheckFileExists    = false;
            dialog.CheckPathExists    = true;
            dialog.AutoUpgradeEnabled = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string strFilePath = dialog.FileName;
                if (!string.IsNullOrEmpty(strFilePath))
                {
                    //string strFilePath = Path.Combine(strFolderPath, fileName);

                    var downloadItem = new DownloadItem()
                    {
                        DownloadID     = Guid.NewGuid(),
                        SourceFileName = fileName,
                        ContentLength  = contentLength,
                        MineType       = mimeType,
                        SavePath       = strFilePath,
                        SaveFileName   = Path.GetFileName(strFilePath),
                        StartTime      = DateTime.Now
                    };
                    downloadManager.Add(downloadItem);

                    DownloadHandler downloadHandler = new DownloadHandler(downloadItem);
                    downloadHandler.OnDownloadComplete += downloadHandler_OnDownloadComplete;
                    downloadHandler.OnDownloadNotify   += downloadHandler_OnDownloadNotify;

                    handler = downloadHandler;

                    (downloadManager as FormDownloadManger).Show();
                    (downloadManager as FormDownloadManger).BringToFront();

                    return(true);
                }
            }

            return(false);
        }
        public override async Task HandleAsync(EventBase @event, CancellationToken cancellationToken)
        {
            if (@event is StartProcessingEvent startProcessingCommand)
            {
                using var scope = _serviceScopeFactory.CreateScope();

                var userIdentifier = startProcessingCommand.UserIdentifier;

                var stoppingAction = new StoppingAction();
                _downloadManager.Add(userIdentifier, stoppingAction);

                var startedNotification = CreateNotification(userIdentifier, ProcessingStatus.Running);
                _messageSender.Send(startedNotification, Constants.PhotoMapApi);

                if (userIdentifier is YandexDiskUserIdentifier)
                {
                    var yandexDiskDownloadService = scope.ServiceProvider.GetService <IYandexDiskDownloadService>();

                    try
                    {
                        await foreach (var file in yandexDiskDownloadService.DownloadFilesAsync(userIdentifier,
                                                                                                startProcessingCommand.Token, cancellationToken, stoppingAction))
                        {
                            var processedDownloadedFile = await _imageProcessingService.ProcessImageAsync(file);

                            var imageProcessedEvent = CreateResultsCommand(startProcessingCommand.UserIdentifier, processedDownloadedFile);
                            _messageSender.Send(imageProcessedEvent, Constants.PhotoMapApi);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e.Message);

                        var stoppedNotification =
                            CreateNotification(userIdentifier, ProcessingStatus.NotRunning, true, e.Message);
                        _messageSender.Send(stoppedNotification, Constants.PhotoMapApi);
                    }
                    finally
                    {
                        _downloadManager.Remove(userIdentifier);

                        var finishedNotification = CreateNotification(userIdentifier, ProcessingStatus.NotRunning);
                        _messageSender.Send(finishedNotification, Constants.PhotoMapApi);

                        _logger.LogInformation("Processing finished");
                    }
                }
                else if (userIdentifier is DropboxUserIdentifier)
                {
                    var dropboxDownloadService = scope.ServiceProvider.GetService <IDropboxDownloadService>();

                    try
                    {
                        await foreach (var file in dropboxDownloadService.DownloadAsync(userIdentifier,
                                                                                        startProcessingCommand.Token, stoppingAction, cancellationToken))
                        {
                            var processedDownloadedFile = await _imageProcessingService.ProcessImageAsync(file);

                            var imageProcessedEvent = CreateResultsCommand(startProcessingCommand.UserIdentifier, processedDownloadedFile);
                            _messageSender.Send(imageProcessedEvent, Constants.PhotoMapApi);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e.Message);

                        var stoppedNotification =
                            CreateNotification(userIdentifier, ProcessingStatus.NotRunning, true, e.Message);
                        _messageSender.Send(stoppedNotification, Constants.PhotoMapApi);
                    }
                    finally
                    {
                        _downloadManager.Remove(userIdentifier);

                        var finishedNotification = CreateNotification(userIdentifier, ProcessingStatus.NotRunning);
                        _messageSender.Send(finishedNotification, Constants.PhotoMapApi);

                        _logger.LogInformation("Processing finished.");
                    }
                }
            }
        }