public static async Task DownloadS3ObjectAsync(string downloadDir, string objectKey) { try { var settings = SettingsManager.GetSettings(); var downloadInfo = new DownloadObjectInfo { DownloadDirectory = downloadDir, ObjectKey = objectKey }; AmazonS3Client client = new AmazonS3Client( settings.AWSAccessKeyID, settings.AWSSecretAccessKey, RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName)); var request = new GetObjectRequest { BucketName = settings.AWSS3Bucket, Key = objectKey }; var response = await client.GetObjectAsync(request); await SaveObjectAsync(response, downloadInfo); } catch (Exception ex) { Debug.WriteLine(ex.Message); DownloadError?.Invoke("Error during download"); throw ex; } }
public static void ProcessArchiveModel() { var model = GetArchiveModel(); if (model != null) { foreach (var m in model) { if (m.ArchiveTopicFilePath != null) { var topicFile = GetArchiveTopicFileName(m.ArchiveId); var topic = GetExistingTopic(topicFile); if (topic != null) { try { var result = ProcessQueue(topic); //if (result == GlacierResult.Completed) // DownloadSuccess?.Invoke($"Glacier archive was downloaded to {topic.GetOutputFile()}"); } catch (Exception ex) { DownloadError?.Invoke("An error occurred processing a Glacier job. " + ex.Message); } } } } } }
/// <summary> /// 下载完成事件的回调函数 /// </summary> private async void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { if (sender != client) { return; } try { string path = StorageTools.Settings.DownloadFolderPath; StorageFile file = await StorageFile.GetFileFromPathAsync(Message.TempFilePath); await file.MoveAsync(await StorageFolder.GetFolderFromPathAsync(StorageTools.Settings.DownloadFolderPath), Message.FileName + Message.Extention, NameCollisionOption.GenerateUniqueName); //播放一个通知 Toasts.ToastManager.ShowDownloadCompleteToastAsync(Strings.AppResources.GetString("DownloadCompleted"), Message.FileName + ": " + Converters.StringConverter.GetPrintSize(_prog_.CurrentValue), file.Path); //触发事件 DownloadComplete?.Invoke(Message); } catch (Exception ex) { //若用户把下载文件夹设置在奇怪的地方,这里会导致无法访问,触发异常 Debug.WriteLine(ex.ToString()); DownloadError?.Invoke(ex); } }
private void HandleError(Exception e, int operationCode) { if (operationCode != CurrentOperationCode || State == DownloadState.Error) { return; } lock (threadLock) { if (operationCode != CurrentOperationCode || State == DownloadState.Error) { return; } if (State == DownloadState.Downloading && retryCount < MaximumRetries) {//自动重试,不烦用户 retryCount++; AutoRefresh(); return; } else { State = DownloadState.Error; DisposeThreads(); } } //上面的else执行后没有return,会执行这里 DownloadError?.Invoke(e); }
private void HandleError(Exception e, int operationCode) { lock (threadLock) { if (operationCode != CurrentOperationCode || State == DownloadState.Error) { return; } if (State == DownloadState.Downloading && retryCount < MaximumRetries) {//自动重试,不烦用户 retryCount++; Debug.WriteLine("正在进行第" + retryCount + "次重试..."); AutoRefresh(); return; } else { State = DownloadState.Error; DisposeThreads(); } } //上面的else执行后没有return,会执行这里(事件回调内含Async不应在lock块中?) DownloadError?.Invoke(e); }
private void OnDownloadError(string message) { if (DownloadStatus != DownloadStatus.Cancelled) { DownloadStatus = DownloadStatus.Error; } DownloadError?.Invoke(this, new DownloadErrorEventArgs(message)); }
private void OnDownloadError(string errorType) { DownloadError?.Invoke(this, new DoanlodErrorArgs { Tag = TagName, ErrorType = errorType }); }
private void StartDisposeTemporaryFile() { Task.Run(async() => { try { StorageFile temp = await StorageFile.GetFileFromPathAsync(Message.TempFilePath); await temp.DeleteAsync(); } catch (Exception e) { DownloadError?.Invoke(e); } }); }
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); } }
/// <summary> /// 下载完成事件的回调函数 /// </summary> private async void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { if (sender != client) { return; } try { StorageFile file = await StorageFile.GetFileFromPathAsync(Message.TempFilePath); StorageFolder folder = await StorageManager.TryGetFolderAsync(Message.FolderToken); if (folder == null) { folder = await StorageManager.TryGetFolderAsync(Settings.DownloadsFolderToken); Message.FolderToken = Settings.DownloadsFolderToken; if (folder == null) { folder = ApplicationData.Current.LocalCacheFolder; Message.FolderToken = StorageApplicationPermissions.FutureAccessList .Add(ApplicationData.Current.LocalCacheFolder); } Toasts.ToastManager.ShowSimpleToast(Strings.AppResources.GetString("DownloadFolderPathIllegal"), Strings.AppResources.GetString("DownloadFolderPathIllegalMessage")); } await file.MoveAsync(folder, Message.FileName + Message.Extention, NameCollisionOption.GenerateUniqueName); Message.DownloadSize = (long)(await file.GetBasicPropertiesAsync()).Size; Message.IsDone = true; //触发事件 State = DownloadState.Done; DownloadComplete?.Invoke(Message); } catch (Exception ex) { //若用户把下载文件夹设置在奇怪的地方,这里会导致无法访问,触发异常 Debug.WriteLine(ex.ToString()); DownloadError?.Invoke(ex); } }
public static GlacierResult ProcessQueue(Topic topic) { // Check for notifications on topic and process any message try { var settings = SettingsManager.GetSettings(); using (var client = new AmazonGlacierClient( settings.AWSAccessKeyID, settings.AWSSecretAccessKey, RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName))) { var receiveMessageRequest = new ReceiveMessageRequest { QueueUrl = topic.QueueUrl, MaxNumberOfMessages = 1 }; var sqsClient = new AmazonSQSClient(settings.AWSAccessKeyID, settings.AWSSecretAccessKey, RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName)); var receiveMessageResponse = sqsClient.ReceiveMessage(receiveMessageRequest); if (receiveMessageResponse.Messages.Count == 0) { topic.Status = GlacierResult.Incomplete; SaveTopicFile(topic); return(topic.Status); } // Process message string status = GetResponseStatus(receiveMessageResponse); if (string.Equals(status, GlacierUtils.JOB_STATUS_SUCCEEDED, StringComparison.InvariantCultureIgnoreCase)) { DownloadGlacierJobOutput(topic.JobId, client, settings.AWSGlacierVault, topic.GetOutputFile()); Debug.WriteLine($"Downloaded job output to {topic.GetOutputFile()}"); if (topic.ArchiveId != null) { DownloadSuccess?.Invoke($"Glacier archive was downloaded to {topic.GetOutputFile()}"); } DeleteTopic(topic); return(GlacierResult.Completed); } else if (string.Equals(status, GlacierUtils.JOB_STATUS_FAILED, StringComparison.InvariantCultureIgnoreCase)) { DownloadError?.Invoke("Job failed, cannot download the file"); DeleteTopic(topic); return(GlacierResult.JobFailed); } else if (string.Equals(status, GlacierUtils.JOB_STATUS_INPROGRESS, StringComparison.InvariantCultureIgnoreCase)) { DownloadWarning?.Invoke("Job in progress, Queue ARN: " + topic.QueueARN); DeleteTopic(topic); return(GlacierResult.JobInProgress); } else { DeleteTopic(topic); return(GlacierResult.Error); } } } catch (AmazonServiceException azex) { // Handle specific potential errors here Debug.WriteLine("AmazonServiceException " + azex.Message); if (azex.StatusCode == System.Net.HttpStatusCode.Forbidden) { // Invalid credentials BackupError?.Invoke("Invalid AWS credentials were provided while connecting"); return(GlacierResult.Incomplete); } if (azex.InnerException != null && azex.InnerException is System.Net.WebException && ((System.Net.WebException)azex.InnerException).Status == System.Net.WebExceptionStatus.NameResolutionFailure) { // Not connected to internet BackupError?.Invoke("Network connection failure"); return(GlacierResult.Incomplete); } if (azex.InnerException != null && azex.InnerException is System.Net.WebException) { // Network errors BackupError?.Invoke($"A network error occurred ({((System.Net.WebException)azex.InnerException).Status})"); return(GlacierResult.Incomplete); } if (azex.StatusCode == System.Net.HttpStatusCode.BadRequest //&& topic.Status == GlacierResult.JobRequested && azex.Message.Contains("The specified queue does not exist") && DateTime.Now - topic.DateRequested < new TimeSpan(24, 0, 0)) { // Job was recently requested and the queue has not been created yet Debug.WriteLine("Job request may be in progress"); return(GlacierResult.JobRequested); } // TODO Check expiry? // Glacier ref: "A job ID will not expire for at least 24 hours after Amazon Glacier completes the job." DeleteTopic(topic); BackupWarning?.Invoke("An AWS Glacier job has expired, a new job will be issued"); // Reissue expired job InitiateGlacierJob(topic); return(topic.Status); } catch (Exception ex) { DeleteTopic(topic); throw ex; } }
protected virtual void OnDownloadError(DownloadEventErrorArgs e) { DownloadError?.Invoke(this, e); }
public static void OnDownloadError(object sender, DownloadErrorEventArgs ev) => DownloadError?.Invoke(sender, ev);
private void OnDownloadError(object sender, EventArgs args) { DownloadError.Invoke(this, args); }
public void DownloadCurrentEpisode() { var task = BackgroundDownloadHelper.Download(new Uri(CurrentEpisode.Key)); task.ContinueWith(t => DownloadError?.Invoke(this, EventArgs.Empty), TaskContinuationOptions.OnlyOnFaulted); }
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); } }