Example #1
0
        public async Task <Account> CreateAccountAsync(AccountInfo info)
        {
            var token   = new CancellationToken();
            var storage = new Glacier(info.StorageVault, info.StorageRootPath, info.StorageAccessKeyId, info.StorageSecretAccessKey, info.StorageRegionEndpoint);
            await storage.InitAsync(token);

            var account         = new Account(storage);
            var accountCredPath = "accounts/" + info.AccountName + "/drive-credentials/";

            foreach (var d in info.Drives)
            {
                switch (d.DriveType)
                {
                case DriveType.GoogleDrive:

                    var drive =
                        await
                        GoogleDrive.CreateInstance(account, d.DriveId,
                                                   GoogleClientSecrets.Load(new MemoryStream(Resources.client_secret)).Secrets,
                                                   d.DriveRootPath,
                                                   accountCredPath + d.DriveId,
                                                   token);

                    drive.ImageMaxSize = d.DriveImageMaxSize;
                    await drive.GetServiceAsync(token);

                    account.Drives.Add(drive);
                    break;

                case DriveType.LocalDrive:
                    var localDrive = new Local.LocalDrive(account, d.DriveId, d.DriveRootPath)
                    {
                        ImageMaxSize = d.DriveImageMaxSize
                    };
                    account.Drives.Add(localDrive);
                    break;

                default:
                    throw new NotSupportedException("Drive with this type is not supported");
                }
            }

            var accountInventoryPath = "accounts/" + info.AccountName + "/glacier-inventory.xml";

            using (var store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
            {
                if (store.FileExists(accountInventoryPath))
                {
                    using (var input = store.OpenFile(accountInventoryPath, FileMode.Open))
                    {
                        var reader = new StreamReader(input);
                        var xml    = await reader.ReadToEndAsync();

                        var doc          = XDocument.Load(new XmlTextReader(new StringReader(xml)));
                        var glacierDrive = new GlacierPseudoDrive(account, "inventory", doc);
                        account.Drives.Add(glacierDrive);
                    }
                }
            }
            return(account);
        }