public async Task <bool> UploadInBackground(ucSINnerShare.MyUserState myState) { try { using (new CursorWait(true)) { if (myState != null) { myState.StatusText = "Preparing file to upload..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } this.PopulateTags(); await this.PrepareModel(); if (myState != null) { myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Uploading Metadata..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } HttpOperationResponse <ResultSinnerPostSIN> response = await ChummerHub.Client.Backend.Utils.PostSINner(this); if (response?.Body?.CallSuccess == true) { try { try { this.MySINnerFile.Id = response.Body.MySINners.FirstOrDefault().Id; } catch (Exception ex) { Log.Error(ex); throw; } Log.Debug("Character " + this.MyCharacter.Alias + " posted with ID " + this.MySINnerFile.Id); if (myState != null) { myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Uploading Filedata..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } var uploadresult = ChummerHub.Client.Backend.Utils.UploadChummer(this); if (uploadresult.CallSuccess != true) { if (myState != null) { myState.CurrentProgress += 3 * myState.ProgressSteps; myState.StatusText = "Failed uploading Filedata: " + uploadresult.ErrorText; myState.StatusText += Environment.NewLine + uploadresult.MyException; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } if (uploadresult.MyException is Exception aException) { throw aException; } return(false); } else { string msg = "Character " + this.MyCharacter.Alias + " uploaded with Id " + this.MySINnerFile.Id; Log.Trace(msg); if (myState != null) { myState.CurrentProgress += 3 * myState.ProgressSteps; myState.StatusText = msg; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } } } catch (Exception ex) { Log.Error(ex); throw; } } else { if (myState != null) { myState.CurrentProgress += 4 * myState.ProgressSteps; myState.StatusText = "Could not upload Metadata: " + response?.Body?.ErrorText; myState.StatusText += Environment.NewLine + response?.Body?.MyException; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } } } } catch (Exception e) { Log.Error(e); Program.MainForm.ShowMessageBox(e.ToString()); } return(true); }
public async Task <bool> Upload(ucSINnerShare.MyUserState myState = null, CustomActivity parentActivity = null) { using (var op_uploadChummer = Timekeeper.StartSyncron( "Uploading Chummer", parentActivity, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { try { using (new CursorWait(true, PluginHandler.MainForm)) { HttpOperationResponse <ResultSinnerGetSINById> found = null; using (var op_checkalreadyonlineChummer = Timekeeper.StartSyncron( "Checking if already online Chummer", op_uploadChummer, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { if (myState != null) { //1 Step myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Checking online version of file..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } if (MySINnerFile.DownloadedFromSINnersTime > this.MyCharacter.FileLastWriteTime) { if (myState != null) { myState.CurrentProgress += 4 * myState.ProgressSteps; myState.StatusText = "File already uploaded."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } return(true); } var client = StaticUtils.GetClient(); found = await client.GetSINByIdWithHttpMessagesAsync(this.MySINnerFile.Id.GetValueOrDefault()); await Backend.Utils.HandleError(found, found.Body); } if (myState != null) { //2 Step myState.CurrentProgress += myState.ProgressSteps; } using (var op_VisibilityChummer = Timekeeper.StartSyncron( "Setting Visibility for Chummer", op_uploadChummer, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { if (found.Response.StatusCode == System.Net.HttpStatusCode.OK) { if (found.Body.MySINner.LastChange >= this.MyCharacter.FileLastWriteTime) { if (myState != null) { myState.StatusText = "SINner already uploaded and updated online."; myState.CurrentProgress += 3 * myState.ProgressSteps; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } return(true); } if (myState != null) { myState.StatusText = "SINner needs to be uploaded."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } if (!MySINnerFile.SiNnerMetaData.Visibility.UserRights.Any()) { MySINnerFile.SiNnerMetaData.Visibility.UserRights = found.Body.MySINner.SiNnerMetaData.Visibility.UserRights; } } } using (var op_PopulatingChummer = Timekeeper.StartSyncron( "Populating Reflection Tags", op_uploadChummer, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { this.MySINnerFile.SiNnerMetaData.Tags = this.PopulateTags(); } using (var op_PopulatingChummer = Timekeeper.StartSyncron( "Preparing Model", op_uploadChummer, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { await this.PrepareModel(); } if (myState != null) { //3 Step myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Chummerfile prepared for uploading..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } HttpOperationResponse <ResultSinnerPostSIN> res = null; using (var op_PopulatingChummer = Timekeeper.StartSyncron( "Posting SINner", op_uploadChummer, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { res = await ChummerHub.Client.Backend.Utils.PostSINnerAsync(this); await Backend.Utils.HandleError(res, res.Body); } if (myState != null) { //4 Step myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Chummer Metadata stored in DB..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } if (res.Response.IsSuccessStatusCode) { using (var op_PopulatingChummer = Timekeeper.StartSyncron( "Uploading File", op_uploadChummer, CustomActivity.OperationType.DependencyOperation, MyCharacter?.FileName)) { var uploadres = await ChummerHub.Client.Backend.Utils.UploadChummerFileAsync(this); if (uploadres.Response.IsSuccessStatusCode) { if (myState != null) { //5 Step myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Chummer uploaded..."; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } return(true); } if (myState != null) { //5 Step myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Chummer upload failed: " + uploadres.Response.ReasonPhrase; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } return(false); } } if (myState != null) { //5 Step myState.CurrentProgress += myState.ProgressSteps; myState.StatusText = "Chummer upload of Metadata failed: " + res.Response.ReasonPhrase; myState.myWorker?.ReportProgress(myState.CurrentProgress, myState); } return(false); } } catch (Exception e) { op_uploadChummer?.tc?.TrackException(e); await Backend.Utils.HandleError(e); throw; } } }