Example #1
0
        /// <summary>
        /// Saves a zip of the user's images to GoogleDrive, if configured.
        /// </summary>
        /// <exception cref="MyFlightbookException"></exception>
        /// <param name="activeBrand">The brand to use.  Current brand is used if null.</param>
        /// <param name="gd">The GoogleDrive object to use - Must be initialized and refreshed!</param>
        /// <returns>True for success</returns>
        public async Task <GoogleDriveResultDictionary> BackupImagesToGoogleDrive(GoogleDrive gd, Brand activeBrand = null)
        {
            if (activeBrand == null)
            {
                activeBrand = Branding.CurrentBrand;
            }

            if (gd == null)
            {
                throw new ArgumentNullException(nameof(gd));
            }

            if (User.GoogleDriveAccessToken == null)
            {
                throw new MyFlightbookException(Resources.Profile.errNotConfiguredGoogleDrive);
            }

            using (FileStream fs = new FileStream(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite, FileShare.None, Int16.MaxValue, FileOptions.DeleteOnClose))
            {
                WriteZipOfImagesToStream(fs, activeBrand);
                GoogleDriveResultDictionary result = await gd.PutFile(BackupImagesFilename(activeBrand, true), fs, "application/zip").ConfigureAwait(true);

                return(result);
            }
        }
Example #2
0
        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);
        }