コード例 #1
0
        /// <summary>
        ///     The background downloader worker completed event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args.</param>
        private void BackgroundDownloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _downloadComplete = true;

            _backgroundDownloader.DoWork             -= BackgroundDownloader_DoWork;
            _backgroundDownloader.ProgressChanged    -= BackgroundDownloader_ProgressChanged;
            _backgroundDownloader.RunWorkerCompleted -= BackgroundDownloader_RunWorkerCompleted;

            DownloadsCompleted?.Invoke(this, new EventArgs());
        }
コード例 #2
0
ファイル: DownloadsService2.cs プロジェクト: RomanGL/VKSaver
        private async void HandleDownloadAsync(DownloadOperation operation, bool start = true)
        {
            var tokenSource = new CancellationTokenSource();
            var callback    = new Progress <DownloadOperation>(OnDownloadProgressChanged);

            _downloads.Add(operation);
            _cts.Add(operation.Guid, tokenSource);

            OnDownloadProgressChanged(operation);

            try
            {
                if (start)
                {
                    await operation.StartAsync().AsTask(tokenSource.Token, callback);
                }
                else
                {
                    await operation.AttachAsync().AsTask(tokenSource.Token, callback);
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                DownloadError?.Invoke(this, new TransferOperationErrorEventArgs(
                                          operation.Guid,
                                          GetOperationNameFromFileName(operation.ResultFile.Name),
                                          GetContentTypeFromExtension(operation.ResultFile.FileType),
                                          ex));
            }

            OnDownloadProgressChanged(operation);
            if (operation.Progress.Status == BackgroundTransferStatus.Canceled ||
                operation.Progress.Status == BackgroundTransferStatus.Error)
            {
                await operation.ResultFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
            }

            _cts.Remove(operation.Guid);
            _downloads.Remove(operation);

            if (_downloads.Count == 0)
            {
                DownloadsCompleted?.Invoke(this, EventArgs.Empty);
            }
        }
コード例 #3
0
        public async Task QueueAndDownloadAsync(Sound s, IProgress <double> progress)
        {
            string downloadUrl = await _onlineSoundDataProvider.GetDownloadLinkAsync(s);

            if (string.IsNullOrWhiteSpace(downloadUrl))
            {
                return;
            }

            progress.Report(1);

            string localImagePath = await _fileDownloader.ImageDownloadAndSaveAsync(
                s.ImagePath,
                s.Id);

            StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                $"{SoundsDirectory}\\{s.Id + s.FileExtension}",
                CreationCollisionOption.ReplaceExisting);

            BackgroundDownloadService.Instance.StartDownload(
                destinationFile,
                downloadUrl,
                progress);

            var newSoundInfo = new Sound
            {
                Id                    = s.Id,
                ImagePath             = localImagePath,
                Name                  = s.Name,
                FilePath              = destinationFile.Path,
                Attribution           = s.Attribution,
                FileExtension         = s.FileExtension,
                ScreensaverImagePaths = s.ScreensaverImagePaths,
                IsPremium             = s.IsPremium,
                IapId                 = s.IapId,
                ColourHex             = s.ColourHex
            };

            await _soundDataProvider.AddLocalSoundAsync(newSoundInfo);

            DownloadsCompleted?.Invoke(this, EventArgs.Empty);
        }
コード例 #4
0
        private async void HandleDownloadAsync(DownloadOperation operation, bool start = true)
        {
            var tokenSource = new CancellationTokenSource();
            var callback    = new Progress <DownloadOperation>(OnDownloadProgressChanged);

            _downloads.Add(operation);
            _cts[operation.Guid] = tokenSource;

            OnDownloadProgressChanged(operation);

            try
            {
                if (start)
                {
                    await operation.StartAsync().AsTask(tokenSource.Token, callback);
                }
                else
                {
                    await operation.AttachAsync().AsTask(tokenSource.Token, callback);
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                _logService.LogException(ex);
                DownloadError?.Invoke(this, new TransferOperationErrorEventArgs(
                                          operation.Guid,
                                          GetOperationNameFromFile(operation.ResultFile),
                                          GetContentTypeFromExtension(operation.ResultFile.FileType),
                                          ex));
            }

            string fileName = operation.ResultFile.Name.Split(new char[] { '.' })[0];

            try
            {
                if (operation.Progress.Status == BackgroundTransferStatus.Completed)
                {
                    var type = GetContentTypeFromExtension(operation.ResultFile.FileType);
                    if (type == FileContentType.Music)
                    {
                        VKSaverAudio metadata = null;
                        _musicDownloads.TryGetValue(fileName, out metadata);

                        // TODO!
                        await _musicCacheService.PostprocessAudioAsync((StorageFile)operation.ResultFile, metadata);

                        //await _musicCacheService.ConvertAudioToVKSaverFormat((StorageFile)operation.ResultFile, metadata);
                    }
                }
            }
            catch (Exception ex)
            {
                _logService.LogException(ex);
            }

            OnDownloadProgressChanged(operation);

            _cts.Remove(operation.Guid);
            _downloads.Remove(operation);
            _musicDownloads.Remove(fileName);

            if (_downloads.Count == 0)
            {
                DownloadsCompleted?.Invoke(this, EventArgs.Empty);
                await GetMetadataFileAsync(true);
            }
        }