Beispiel #1
0
        public static OneDriveStorageProviderFile FromJSON(
            JObject fileJSON,
            string path)
        {
            string   name         = fileJSON["name"].Value <string>();
            bool     isFolder     = fileJSON.ContainsKey("folder");
            string   sha1Hash     = isFolder ? String.Empty : fileJSON["file"]["hashes"]["sha1Hash"].Value <string>();
            DateTime lastModified = fileJSON["fileSystemInfo"]["lastModifiedDateTime"].Value <DateTime>();

            switch (lastModified.Kind)
            {
            case DateTimeKind.Unspecified:
            {
                //incorrect date time kind
                throw new InvalidOperationException();
            }

            case DateTimeKind.Local:
            {
                lastModified = lastModified.ToUniversalTime();
                break;
            }
            }

            return(CloudStorageProviderFileBase.Create <OneDriveStorageProviderFile>(
                       name,
                       isFolder,
                       path,
                       !isFolder ? sha1Hash : null,
                       !isFolder ? lastModified : new Nullable <DateTime>()));
        }
 public static DropboxStorageProviderFile FromMetadata(Metadata metaData)
 {
     return(CloudStorageProviderFileBase.Create <DropboxStorageProviderFile>(
                metaData.Name,
                metaData.IsFolder,
                metaData.PathLower,
                metaData.IsFile ? metaData.AsFile.ContentHash : null,
                metaData.IsFile ? metaData.AsFile.ClientModified : new Nullable <DateTime>()));
 }
Beispiel #3
0
        public async Task UpdateVaultSyncStatus(VaultIndex index)
        {
            if (index.SyncMode == Common.SyncMode.CloudProvider)
            {
                DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseLow | LoggerMessageType.Information, "CloudStorageSyncManager Updating vault sync status of vault '{0}'.", index.ID);

                CloudStorageProviderBase storageProvider = null;
                if (_providers.ContainsKey(index))
                {
                    storageProvider = _providers[index];
                }
                else
                {
                    string authType = _getProviderValueCallback(index.Provider, "AuthType").ToString();
                    switch (authType)
                    {
                    case "OAuth":
                    {
                        string providerKey = (string)_getProviderValueCallback(index.Provider, "ProviderKey");
                        string accessToken = _getCredentialCallback(index.Provider);
                        if (!String.IsNullOrEmpty(accessToken))
                        {
                            Dictionary <string, string> parameters = new Dictionary <string, string>();
                            parameters.Add("AuthType", "OAuth");
                            parameters.Add("ProviderKey", providerKey);
                            parameters.Add("AccessToken", accessToken);
                            storageProvider = CloudStorageProviderBase.Create(
                                _logger,
                                index,
                                parameters);
                            _providers.Add(index, storageProvider);
                        }
                        else
                        {
                            index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.AuthenticationError, "The OAuth credentials for the associated cloud provider could not be retrieved. This may have be caused by uninstalling / reinstalling cachy. To fix this, re-authenticate with the cloud provider through the application settings 'SYNC' menu.");
                            return;
                        }
                        break;
                    }

                    case "Amazon":
                    {
                        string providerKey = (string)_getProviderValueCallback(index.Provider, "ProviderKey");
                        string secret      = _getCredentialCallback(index.Provider);
                        if (!String.IsNullOrEmpty(secret))
                        {
                            JObject        s3ConfigJSON = JObject.Parse(secret);
                            AmazonS3Config s3Config     = AmazonS3Config.FromJSON(s3ConfigJSON);
                            Dictionary <string, string> createParams = s3Config.ToDictionary();
                            createParams.Add("ProviderKey", providerKey);
                            storageProvider = CloudStorageProviderBase.Create(
                                DLoggerManager.Instance.Logger,
                                createParams);
                            _providers.Add(index, storageProvider);
                        }
                        else
                        {
                            index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.AuthenticationError, "The OAuth credentials for the associated cloud provider could not be retrieved. This may have be caused by uninstalling / reinstalling cachy. To fix this, re-authenticate with the cloud provider through the application settings 'SYNC' menu.");
                            return;
                        }
                        break;
                    }
                    }
                }

                CloudProviderResponse <CloudStorageProviderFileBase> fileResponse = await storageProvider.GetFileInfo(index.CloudProviderPath);

                switch (fileResponse.ResponseValue)
                {
                case CloudProviderResponse <CloudStorageProviderFileBase> .Response.Success:
                {
                    //does our local file exist?
                    if (System.IO.File.Exists(index.FullPath))
                    {
                        if (!index.LastModified.HasValue)
                        {
                            index.UpdateLastModified();
                            if (!index.LastModified.HasValue)
                            {
                                DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseLow | LoggerMessageType.Information, "CloudStorageSyncManager Uknown error for vault '{0}'.", index.ID);
                                index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.UnknownError);
                                break;
                            }
                        }

                        CloudStorageProviderFileBase remote = fileResponse.Result;

                        string currenthash = await Native.Native.FileHandler.HashFileAsync(
                            GetHashStyleFromProviderKey(storageProvider.TypeName),
                            index.FullPath);

                        bool hashesMatch = currenthash == remote.Hash;
                        if (hashesMatch)
                        {
                            DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseHigh | LoggerMessageType.Information, "CloudStorageSyncManager Vault '{0}' is up to date.", index.ID);
                            index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.UpToDate);
                        }
                        else
                        {
                            //These dates will *never* match
                            index.SyncStatus.SetStatus(remote.LastModified > index.LastModified ?
                                                       CloudProviderSyncStatus.SyncStatus.CloudCopyNewer :
                                                       CloudProviderSyncStatus.SyncStatus.LocalCopyNewer);
                        }
                    }
                    else
                    {
                        DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseLow | LoggerMessageType.Warning, "CloudStorageSyncManager Local copy does not exist for vault '{0}'.", index.ID);
                        index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.NoLocalCopyExists);
                    }

                    break;
                }

                case CloudProviderResponse <CloudStorageProviderFileBase> .Response.NotFound:
                {
                    DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseLow | LoggerMessageType.Warning, "CloudStorageSyncManager File not found in cloud for vault '{0}', provider type '{1}'.", index.ID, storageProvider.TypeName);
                    index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.NoCloudCopyExists);
                    break;
                }

                case CloudProviderResponse <CloudStorageProviderFileBase> .Response.AuthenticationError:
                {
                    DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseLow | LoggerMessageType.Error, "CloudStorageSyncManager Authentication error for cloud provider type '{0}'.", storageProvider.TypeName);
                    index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.AuthenticationError, "The OAuth credentials for the associated cloud provider are invalid. To fix this, re-authenticate with the cloud provider through the application settings 'SYNC' menu.");
                    break;
                }

                case CloudProviderResponse <CloudStorageProviderFileBase> .Response.UnknownError:
                {
                    DLoggerManager.Instance.Logger.Log(LoggerMessageType.VerboseLow | LoggerMessageType.Error, "CloudStorageSyncManager Unknown error for cloud provider type '{0}'.", storageProvider.TypeName);
                    index.SyncStatus.SetStatus(CloudProviderSyncStatus.SyncStatus.UnknownError);
                    break;
                }
                }
            }
        }