/// <summary> /// 非同期アップロードの開始 /// </summary> /// <returns></returns> public async Task Run() { // Oauthする UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(apiKeyStream).Secrets, // This OAuth 2.0 access scope allows an application to upload files to the // authenticated user's YouTube channel, but doesn't allow other types of access. new[] { YouTubeService.Scope.YoutubeUpload }, "user", CancellationToken.None ); // YoutubeAPIのサービス作成 using var youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = Assembly.GetExecutingAssembly().GetName().Name }); //ステータス更新用に情報を走査する foreach (var item in this.uploadItems) { if (!item.IsUpload) { continue; } this.status.FileCount++; this.status.FileAllByte += item.FileSize; } //実アップロードを実行 var index = 0; foreach (var item in this.uploadItems) { if (!item.IsUpload) { continue; } index++; status.FileName = Path.GetFileName(item.FilePath); status.FileIndex = index; // 非同期アップロード’(1ファイル) await UploadFile(youtubeService, item, index); status.FileCurrentUploadedByte = 0; status.FileUploadedByte += item.FileSize; // ステータス変更イベントを発行 YoutubeUploadStatusChanged?.Invoke(status.Clone()); } // すべてアップロード完了したイベントを発行する status.IsAllComplete = true; YoutubeUploadStatusChanged?.Invoke(status.Clone()); }
/// <summary> /// 状態変更イベント /// </summary> /// <param name="progress"></param> void VideosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress) { status.Status = progress.Status; switch (progress.Status) { case UploadStatus.Uploading: Debug.WriteLine("{0} bytes sent.", progress.BytesSent); status.FileCurrentUploadedByte = progress.BytesSent; break; case UploadStatus.Failed: Debug.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception); break; } YoutubeUploadStatusChanged?.Invoke(status.Clone()); }