protected async void lnkSaveGoogleDrive_Click(object sender, EventArgs e) { MyFlightbook.Profile pf = MyFlightbook.Profile.GetUser(Page.User.Identity.Name); LogbookBackup lb = new LogbookBackup(pf); if (pf.GoogleDriveAccessToken == null || String.IsNullOrEmpty(pf.GoogleDriveAccessToken.RefreshToken)) { return; } try { GoogleDrive gd = new GoogleDrive(pf.GoogleDriveAccessToken); await lb.BackupToGoogleDrive(gd).ConfigureAwait(false); if (ckIncludeImages.Checked) { await lb.BackupImagesToGoogleDrive(gd).ConfigureAwait(false); } // if we are here we were successful, so save the updated refresh token pf.GoogleDriveAccessToken = gd.AuthState; pf.FCommit(); lblDropBoxSuccess.Visible = true; } catch (Exception ex) when(!(ex is OutOfMemoryException)) { ShowDropboxError(ex.Message); } }
private async Task <bool> BackupToCloud() { StringBuilder sb = new StringBuilder(); StringBuilder sbFailures = new StringBuilder(); List <EarnedGratuity> lstUsersWithCloudBackup = EarnedGratuity.GratuitiesForUser(string.Empty, Gratuity.GratuityTypes.CloudBackup); if (!String.IsNullOrEmpty(UserRestriction)) { lstUsersWithCloudBackup.RemoveAll(eg => eg.Username.CompareCurrentCultureIgnoreCase(UserRestriction) != 0); } foreach (EarnedGratuity eg in lstUsersWithCloudBackup) { StorageID sid = StorageID.None; if (eg.UserProfile != null && ((sid = eg.UserProfile.BestCloudStorage) != StorageID.None) && eg.CurrentStatus != EarnedGratuity.EarnedGratuityStatus.Expired) { try { Profile pf = eg.UserProfile; LogbookBackup lb = new LogbookBackup(pf); switch (sid) { case StorageID.Dropbox: await BackupDropbox(lb, pf, sb, sbFailures).ConfigureAwait(false); break; case StorageID.OneDrive: await BackupOneDrive(lb, pf, sb, sbFailures).ConfigureAwait(false); break; case StorageID.GoogleDrive: await BackupGoogleDrive(lb, pf, sb, sbFailures).ConfigureAwait(false); break; case StorageID.iCloud: break; default: break; } } catch (Exception ex) when(!(ex is OutOfMemoryException)) { string szError = String.Format(CultureInfo.CurrentCulture, "eg user={0}{1}\r\n\r\n{2}\r\n\r\n{3}", eg == null ? "NULL eg!" : eg.Username, (eg != null && eg.UserProfile == null) ? " NULL PROFILE" : string.Empty, sbFailures.ToString(), sb.ToString()); util.NotifyAdminException("ERROR running nightly backup", new MyFlightbookException(szError, ex)); } } } ; util.NotifyAdminEvent("Dropbox report", sbFailures.ToString() + sb.ToString(), ProfileRoles.maskCanReport); return(true); }
protected async void lnkSaveDropbox_Click(object sender, EventArgs e) { Profile pf = MyFlightbook.Profile.GetUser(User.Identity.Name); LogbookBackup lb = new LogbookBackup(pf); try { if (await new MFBDropbox().ValidateDropboxToken(pf, true, true) == MFBDropbox.TokenStatus.None) { return; } Dropbox.Api.Files.FileMetadata result = await lb.BackupToDropbox(); if (ckIncludeImages.Checked) { result = await lb.BackupImagesToDropbox(Branding.CurrentBrand); } lblDropBoxSuccess.Visible = true; } catch (Dropbox.Api.ApiException <Dropbox.Api.Auth.TokenFromOAuth1Error> ex) { ShowDropboxError(ex.Message); } catch (Dropbox.Api.AuthException ex) { ShowDropboxError(ex.Message); } catch (Dropbox.Api.ApiException <Dropbox.Api.Files.UploadError> ex) { if (ex.ErrorResponse.IsPath && ex.ErrorResponse.AsPath != null && ex.ErrorResponse.AsPath.Value.Reason.IsInsufficientSpace) { ShowDropboxError(Resources.LocalizedText.DropboxErrorOutOfSpace); } else { ShowDropboxError(ex.Message); } } catch (Dropbox.Api.BadInputException ex) { ShowDropboxError(ex.Message); } catch (Dropbox.Api.HttpException ex) { ShowDropboxError(ex.Message); } catch (UnauthorizedAccessException ex) { ShowDropboxError(ex.Message); } catch (MyFlightbookException ex) { ShowDropboxError(ex.Message); } }
private async Task <bool> BackupGoogleDrive(LogbookBackup lb, Profile pf, StringBuilder sb, StringBuilder sbFailures) { try { if (pf.GoogleDriveAccessToken == null) { throw new UnauthorizedAccessException(); } GoogleDrive gd = new GoogleDrive(pf.GoogleDriveAccessToken); sb.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive: user {0} ", pf.UserName); GoogleDriveResultDictionary meta = await lb.BackupToGoogleDrive(gd, Branding.CurrentBrand).ConfigureAwait(false); if (meta != null) { sb.AppendFormat(CultureInfo.CurrentCulture, "Logbook backed up for user {0}...", pf.UserName); } System.Threading.Thread.Sleep(0); meta = await lb.BackupImagesToGoogleDrive(gd, Branding.CurrentBrand).ConfigureAwait(false); System.Threading.Thread.Sleep(0); if (meta != null) { sb.AppendFormat(CultureInfo.CurrentCulture, "and images backed up for user {0}.\r\n \r\n", pf.UserName); } // if we are here we were successful, so save the updated refresh token pf.GoogleDriveAccessToken = gd.AuthState; pf.FCommit(); } catch (MyFlightbookException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user (MyFlightbookException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.GoogleDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.GoogleDriveFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (UnauthorizedAccessException ex) { // De-register GoogleDrive. pf.GoogleDriveAccessToken = null; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user (UnauthorizedAccess) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.GoogleDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.GoogleDriveFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (System.IO.FileNotFoundException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user: FileNotFoundException, no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } catch (Exception ex) when(!(ex is OutOfMemoryException)) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user (Unknown Exception), no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } return(true); }
protected void lnkDownloadImagesZip_Click(object sender, EventArgs e) { Profile pf = MyFlightbook.Profile.GetUser(Page.User.Identity.Name); Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", String.Format(CultureInfo.InvariantCulture, "attachment;filename={0}", Branding.ReBrand(String.Format(CultureInfo.InvariantCulture, "{0}.zip", Resources.LocalizedText.ImagesBackupFilename)).Replace(" ", "-"))); using (System.IO.MemoryStream ms = new LogbookBackup(pf).ZipOfImagesForUser(Branding.CurrentBrand)) Response.BinaryWrite(ms.ToArray()); Response.End(); }
protected async void lnkSaveGoogleDrive_Click(object sender, EventArgs e) { MyFlightbook.Profile pf = MyFlightbook.Profile.GetUser(Page.User.Identity.Name); LogbookBackup lb = new LogbookBackup(pf); if (pf.GoogleDriveAccessToken == null || String.IsNullOrEmpty(pf.GoogleDriveAccessToken.RefreshToken)) { return; } try { GoogleDrive gd = new GoogleDrive(pf.GoogleDriveAccessToken); if (await gd.RefreshAccessToken()) { pf.FCommit(); } await lb.BackupToGoogleDrive(gd); if (ckIncludeImages.Checked) { await lb.BackupImagesToGoogleDrive(gd); } lblDropBoxSuccess.Visible = true; } catch (MyFlightbookException ex) { ShowDropboxError(ex.Message); } catch (System.Net.Http.HttpRequestException ex) { ShowDropboxError(ex.Message); } catch (Exception ex) { ShowDropboxError(ex.Message); } }
private void WriteInsurenceZip(Action <String, Stream> dispatch) { if (dispatch == null) { throw new ArgumentNullException(nameof(dispatch)); } Profile pf = Profile.GetUser(Page.User.Identity.Name); mfbDownload1.User = pf.UserName; mfbDownload1.UpdateData(); string szFile = Regex.Replace(Branding.ReBrand(String.Format(CultureInfo.InvariantCulture, "{0}-{1}-{2}", Branding.CurrentBrand.AppName, pf.UserFullName, Resources.LocalizedText.InsuranceBackupName)).Replace(" ", "-"), "[^0-9a-zA-Z-]", string.Empty); using (FileStream fs = new FileStream(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite, FileShare.None, Int16.MaxValue, FileOptions.DeleteOnClose)) { LogbookBackup lb = new LogbookBackup(pf) { IncludeImages = false }; lb.WriteZipOfImagesToStream(fs, Branding.CurrentBrand, (zip) => { // Add the flights ZipArchiveEntry zae = zip.CreateEntry(CSVFileName); using (StreamWriter sw = new StreamWriter(zae.Open())) { sw.Write('\uFEFF'); // UTF-8 BOM. sw.Write(mfbDownload1.CSVData()); } // And add the user's information, in JSON format. zae = zip.CreateEntry("PilotInfo.JSON"); using (StreamWriter sw = new StreamWriter(zae.Open())) { sw.Write(LogbookBackup.PilotInfoAsJSon(pf)); } }); dispatch(szFile, fs); } }
protected async void lnkSaveOneDrive_Click(object sender, EventArgs e) { MyFlightbook.Profile pf = MyFlightbook.Profile.GetUser(Page.User.Identity.Name); LogbookBackup lb = new LogbookBackup(pf); if (pf.OneDriveAccessToken == null || String.IsNullOrEmpty(pf.OneDriveAccessToken.RefreshToken)) { return; } try { OneDrive od = new OneDrive(pf.OneDriveAccessToken); await lb.BackupToOneDrive(od); if (ckIncludeImages.Checked) { await lb.BackupImagesToOneDrive(od); } // if we are here we were successful, so save the updated refresh token pf.OneDriveAccessToken.RefreshToken = od.AuthState.RefreshToken; pf.FCommit(); lblDropBoxSuccess.Visible = true; } catch (OneDriveException ex) { ShowDropboxError(OneDrive.MessageForException(ex)); } catch (MyFlightbookException ex) { ShowDropboxError(ex.Message); } catch (Exception ex) { ShowDropboxError(ex.Message); } }
private async Task <bool> BackupToCloud() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.Text.StringBuilder sbFailures = new System.Text.StringBuilder(); List <EarnedGrauity> lstUsersWithCloudBackup = EarnedGrauity.GratuitiesForUser(string.Empty, Gratuity.GratuityTypes.CloudBackup); if (!String.IsNullOrEmpty(UserRestriction)) { lstUsersWithCloudBackup.RemoveAll(eg => eg.Username.CompareCurrentCultureIgnoreCase(UserRestriction) != 0); } foreach (EarnedGrauity eg in lstUsersWithCloudBackup) { StorageID sid = StorageID.None; if (eg.UserProfile != null && ((sid = eg.UserProfile.BestCloudStorage) != StorageID.None) && eg.CurrentStatus != EarnedGrauity.EarnedGratuityStatus.Expired) { Profile pf = eg.UserProfile; LogbookBackup lb = new LogbookBackup(pf); switch (sid) { case StorageID.Dropbox: { MFBDropbox.TokenStatus ts = new MFBDropbox().ValidateDropboxToken(pf, true, true); if (ts == MFBDropbox.TokenStatus.None) { continue; } try { Dropbox.Api.Files.FileMetadata result = null; result = await lb.BackupToDropbox(Branding.CurrentBrand); sb.AppendFormat(CultureInfo.CurrentCulture, "Dropbox: user {0} ", pf.UserName); if (ts == MFBDropbox.TokenStatus.oAuth1) { sb.Append("Token UPDATED from oauth1! "); } sb.AppendFormat(CultureInfo.CurrentCulture, "Logbook backed up for user {0}...", pf.UserName); System.Threading.Thread.Sleep(0); result = await lb.BackupImagesToDropbox(Branding.CurrentBrand); System.Threading.Thread.Sleep(0); sb.AppendFormat(CultureInfo.CurrentCulture, "and images backed up for user {0}.\r\n \r\n", pf.UserName); } catch (Dropbox.Api.ApiException <Dropbox.Api.Files.UploadError> ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (Dropbox.Api.ApiException<Dropbox.Api.Files.UploadError) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); string szMessage = (ex.ErrorResponse.IsPath && ex.ErrorResponse.AsPath != null && ex.ErrorResponse.AsPath.Value.Reason.IsInsufficientSpace) ? Resources.LocalizedText.DropboxErrorOutOfSpace : ex.Message; util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, szMessage, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.AuthException ex) { // De-register dropbox. pf.DropboxAccessToken = string.Empty; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (AuthException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.BadInputException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (BadInputException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), false, false); } catch (Dropbox.Api.HttpException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (HttpException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.AccessException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (AccessException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.DropboxException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (Base dropbox exception) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (UnauthorizedAccessException ex) { // De-register dropbox. pf.DropboxAccessToken = string.Empty; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (UnauthorizedAccess) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (MyFlightbookException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (MyFlightbookException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (System.IO.FileNotFoundException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user: FileNotFoundException, no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } catch (Exception ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (Unknown Exception), no notification sent {0}: {1} {2}\r\n\r\n{3}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message, ex.StackTrace); if (ex.InnerException != null) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Inner exception: {0}\r\n{1}", ex.InnerException.Message, ex.InnerException.StackTrace); } } } break; case StorageID.OneDrive: { try { if (pf.OneDriveAccessToken == null) { throw new UnauthorizedAccessException(); } Microsoft.OneDrive.Sdk.Item item = null; OneDrive od = new OneDrive(pf.OneDriveAccessToken); item = await lb.BackupToOneDrive(od); sb.AppendFormat(CultureInfo.CurrentCulture, "OneDrive: user {0} ", pf.UserName); sb.AppendFormat(CultureInfo.CurrentCulture, "Logbook backed up for user {0}...", pf.UserName); System.Threading.Thread.Sleep(0); item = await lb.BackupImagesToOneDrive(od, Branding.CurrentBrand); System.Threading.Thread.Sleep(0); sb.AppendFormat(CultureInfo.CurrentCulture, "and images backed up for user {0}.\r\n \r\n", pf.UserName); // if we are here we were successful, so save the updated refresh token if (String.Compare(pf.OneDriveAccessToken.RefreshToken, od.AuthState.RefreshToken, StringComparison.Ordinal) != 0) { pf.OneDriveAccessToken.RefreshToken = od.AuthState.RefreshToken; pf.FCommit(); } } catch (Microsoft.OneDrive.Sdk.OneDriveException ex) { string szMessage = OneDrive.MessageForException(ex); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "OneDrive FAILED for user (OneDriveException) {0}: {1}\r\n\r\n", pf.UserName, szMessage + " " + ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.OneDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.OneDriveFailure, pf.UserFullName, szMessage, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (MyFlightbookException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "OneDrive FAILED for user (MyFlightbookException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.OneDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.OneDriveFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (UnauthorizedAccessException ex) { // De-register oneDrive. pf.OneDriveAccessToken = null; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "OneDrive FAILED for user (UnauthorizedAccess) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.OneDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.OneDriveFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (System.IO.FileNotFoundException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "OneDrive FAILED for user: FileNotFoundException, no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } catch (Exception ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "OneDrive FAILED for user (Unknown Exception), no notification sent {0}: {1} {2}\r\n\r\n{3}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message, ex.StackTrace); } } break; case StorageID.GoogleDrive: { try { if (pf.GoogleDriveAccessToken == null) { throw new UnauthorizedAccessException(); } GoogleDrive gd = new GoogleDrive(pf.GoogleDriveAccessToken); bool fRefreshed = await gd.RefreshAccessToken(); sb.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive: user {0} ", pf.UserName); IReadOnlyDictionary <string, string> meta = await lb.BackupToGoogleDrive(gd, Branding.CurrentBrand); if (meta != null) { sb.AppendFormat(CultureInfo.CurrentCulture, "Logbook backed up for user {0}...", pf.UserName); } System.Threading.Thread.Sleep(0); meta = await lb.BackupImagesToGoogleDrive(gd, Branding.CurrentBrand); System.Threading.Thread.Sleep(0); if (meta != null) { sb.AppendFormat(CultureInfo.CurrentCulture, "and images backed up for user {0}.\r\n \r\n", pf.UserName); } // if we are here we were successful, so save the updated refresh token if (fRefreshed) { pf.FCommit(); } } catch (MyFlightbookException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user (MyFlightbookException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.GoogleDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.GoogleDriveFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (UnauthorizedAccessException ex) { // De-register GoogleDrive. pf.GoogleDriveAccessToken = null; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user (UnauthorizedAccess) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.GoogleDriveFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.GoogleDriveFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (System.IO.FileNotFoundException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user: FileNotFoundException, no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } catch (Exception ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "GoogleDrive FAILED for user (Unknown Exception), no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } } break; case StorageID.iCloud: break; default: break; } } } ; util.NotifyAdminEvent("Dropbox report", sbFailures.ToString() + sb.ToString(), ProfileRoles.maskCanReport); return(true); }
private async Task <bool> BackupDropbox(LogbookBackup lb, Profile pf, StringBuilder sb, StringBuilder sbFailures) { try { MFBDropbox.TokenStatus ts = await new MFBDropbox().ValidateDropboxToken(pf, true).ConfigureAwait(false); if (ts == MFBDropbox.TokenStatus.None) { return(true); } Dropbox.Api.Files.FileMetadata result = null; result = await lb.BackupToDropbox(Branding.CurrentBrand).ConfigureAwait(false); sb.AppendFormat(CultureInfo.CurrentCulture, "Dropbox: user {0} ", pf.UserName); if (ts == MFBDropbox.TokenStatus.oAuth1) { sb.Append("Token UPDATED from oauth1! "); } sb.AppendFormat(CultureInfo.CurrentCulture, "Logbook backed up for user {0}...", pf.UserName); System.Threading.Thread.Sleep(0); result = await lb.BackupImagesToDropbox(Branding.CurrentBrand).ConfigureAwait(false); System.Threading.Thread.Sleep(0); sb.AppendFormat(CultureInfo.CurrentCulture, "and images backed up for user {0}.\r\n \r\n", pf.UserName); } catch (Dropbox.Api.ApiException <Dropbox.Api.Files.UploadError> ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (Dropbox.Api.ApiException<Dropbox.Api.Files.UploadError) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); string szMessage = (ex.ErrorResponse.IsPath && ex.ErrorResponse.AsPath != null && ex.ErrorResponse.AsPath.Value.Reason.IsInsufficientSpace) ? Resources.LocalizedText.DropboxErrorOutOfSpace : ex.Message; util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, szMessage, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.ApiException <Dropbox.Api.Auth.TokenFromOAuth1Error> ex) { // De-register dropbox. pf.DropboxAccessToken = string.Empty; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (TokenFromOAuth1Error, token removed) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.AuthException ex) { // De-register dropbox. pf.DropboxAccessToken = string.Empty; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (AuthException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.BadInputException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (BadInputException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), false, false); } catch (Dropbox.Api.HttpException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (HttpException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.AccessException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (AccessException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (Dropbox.Api.DropboxException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (Base dropbox exception) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (UnauthorizedAccessException ex) { // De-register dropbox. pf.DropboxAccessToken = string.Empty; pf.FCommit(); sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (UnauthorizedAccess) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, Resources.LocalizedText.DropboxErrorDeAuthorized), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (MyFlightbookException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (MyFlightbookException) {0}: {1}\r\n\r\n", pf.UserName, ex.Message); util.NotifyUser(Branding.ReBrand(Resources.EmailTemplates.DropboxFailureSubject, ActiveBrand), Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.EmailTemplates.DropboxFailure, pf.UserFullName, ex.Message, string.Empty), ActiveBrand), new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), true, false); } catch (System.IO.FileNotFoundException ex) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user: FileNotFoundException, no notification sent {0}: {1} {2}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message); } catch (Exception ex) when(!(ex is OutOfMemoryException)) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Dropbox FAILED for user (Unknown Exception), no notification sent {0}: {1} {2}\r\n\r\n{3}\r\n\r\n", pf.UserName, ex.GetType().ToString(), ex.Message, ex.StackTrace); if (ex.InnerException != null) { sbFailures.AppendFormat(CultureInfo.CurrentCulture, "Inner exception: {0}\r\n{1}", ex.InnerException.Message, ex.InnerException.StackTrace); } } return(true); }
private async Task <bool> BackupToCloud() { string szBackups = Path.Combine(Path.GetTempPath(), String.Format(CultureInfo.InvariantCulture, "{0}-CloudBackup.txt", DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))); string szBackupFailures = Path.Combine(Path.GetTempPath(), String.Format(CultureInfo.InvariantCulture, "{0}-CloudBackup-errors.txt", DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))); if (File.Exists(szBackups)) { File.Delete(szBackups); } if (File.Exists(szBackupFailures)) { File.Delete(szBackupFailures); } List <EarnedGratuity> lstUsersWithCloudBackup = EarnedGratuity.GratuitiesForUser(string.Empty, Gratuity.GratuityTypes.CloudBackup); if (!String.IsNullOrEmpty(UserRestriction)) { lstUsersWithCloudBackup.RemoveAll(eg => eg.Username.CompareCurrentCultureIgnoreCase(UserRestriction) != 0); } foreach (EarnedGratuity eg in lstUsersWithCloudBackup) { StringBuilder sb = new StringBuilder(); StringBuilder sbFailures = new StringBuilder(); StorageID sid = StorageID.None; if (eg.UserProfile != null && ((sid = eg.UserProfile.BestCloudStorage) != StorageID.None) && eg.CurrentStatus != EarnedGratuity.EarnedGratuityStatus.Expired) { try { Profile pf = eg.UserProfile; LogbookBackup lb = new LogbookBackup(pf); EventRecorder.LogCall("Nightly run: backing up to {storage} for user {user}", sid.ToString(), eg.Username); switch (sid) { case StorageID.Dropbox: await BackupDropbox(lb, pf, sb, sbFailures).ConfigureAwait(false); break; case StorageID.OneDrive: await BackupOneDrive(lb, pf, sb, sbFailures).ConfigureAwait(false); break; case StorageID.GoogleDrive: await BackupGoogleDrive(lb, pf, sb, sbFailures).ConfigureAwait(false); break; default: break; } } catch (Exception ex) when(!(ex is OutOfMemoryException)) { string szError = String.Format(CultureInfo.CurrentCulture, "eg user={0}{1}\r\n\r\n{2}\r\n\r\n{3}", eg == null ? "NULL eg!" : eg.Username, (eg != null && eg.UserProfile == null) ? " NULL PROFILE" : string.Empty, sbFailures.ToString(), sb.ToString()); util.NotifyAdminException("ERROR running nightly backup", new MyFlightbookException(szError, ex)); } finally { lock (lockObj) { using (StreamWriter swSuccess = new StreamWriter(szBackups, true, Encoding.UTF8)) using (StreamWriter swFailure = new StreamWriter(szBackupFailures, true, Encoding.UTF8)) { // Regardless of what happens, write this to the file (in case something dies). string sz = sb.ToString(); if (!String.IsNullOrWhiteSpace(sz)) { swSuccess.WriteLine(sz.Trim()); } sz = sbFailures.ToString(); if (!String.IsNullOrWhiteSpace(sz)) { swFailure.WriteLine(sz.Trim()); } swSuccess.Flush(); swFailure.Flush(); } } } } } using (StreamReader srSuccess = new StreamReader(szBackups, Encoding.UTF8)) using (StreamReader srFailure = new StreamReader(szBackupFailures, Encoding.UTF8)) { util.NotifyAdminEvent("Cloud Backup report", srFailure.ReadToEnd() + srSuccess.ReadToEnd(), ProfileRoles.maskCanReport); } // if we are here, we should be able to delete the temp files try { File.Delete(szBackups); File.Delete(szBackupFailures); } finally { } return(true); }