public virtual StorageFileStreamResult Execute(WebDavConfigurationModel model, string filePath) { model = model.With(m => !string.IsNullOrEmpty(m.ServerUrl)) .With(m => !string.IsNullOrEmpty(m.AccountLogin)) .With(m => !string.IsNullOrEmpty(m.AccountPassword)); if (model == null) { throw new PluginException(PluginErrorCodes.InvalidCredentialsOrConfiguration); } if (string.IsNullOrEmpty(filePath)) { throw new PluginException(PluginErrorCodes.InvalidFileOrDirectoryName); } else { filePath = filePath.TrimEnd('/').RemoveCharDuplicates('/'); } if (string.IsNullOrEmpty(filePath)) { throw new PluginException(PluginErrorCodes.InvalidFileOrDirectoryName); } StorageFileStreamResult result = new StorageFileStreamResult(); Uri uri = new Uri(model.ServerUrl); ICloudStorageConfiguration config = new WebDavConfiguration(uri); GenericNetworkCredentials cred = new GenericNetworkCredentials(); cred.UserName = model.AccountLogin; cred.Password = model.AccountPassword; CloudStorage storage = null; try { storage = new CloudStorage(); ICloudStorageAccessToken storageToken = storage.Open(config, cred); var file = storage.GetFile(filePath, null); result.FileName = file.Name; result.FileStream = file.GetDataTransferAccessor().GetDownloadStream(); } finally { if (storage != null) { storage.Close(); } } return(result); }
public void ApplyConfiguration(int accountId, DateTime accountStamp, object configurationModel) { WebDavConfigurationModel webDavConfigurationModel = configurationModel as WebDavConfigurationModel; if (webDavConfigurationModel == null) { return; } Dictionary <string, string> settings = new Dictionary <string, string> { { "login", webDavConfigurationModel.AccountLogin }, { "password", _cryptoService.EncryptString(webDavConfigurationModel.AccountPassword, _securityConfiguration.EncryptionKey, _securityConfiguration.EncryptionSalt) }, { "server", webDavConfigurationModel.ServerUrl } }; _accountService.SaveSettings(settings, accountId, accountStamp); }
public virtual StorageFileStreamResult Execute(WebDavConfigurationModel model, string filePath) { model = model.With(m => !string.IsNullOrEmpty(m.ServerUrl)) .With(m => !string.IsNullOrEmpty(m.AccountLogin)) .With(m => !string.IsNullOrEmpty(m.AccountPassword)); if (model == null) throw new PluginException(PluginErrorCodes.InvalidCredentialsOrConfiguration); if (string.IsNullOrEmpty(filePath)) throw new PluginException(PluginErrorCodes.InvalidFileOrDirectoryName); else filePath = filePath.TrimEnd('/').RemoveCharDuplicates('/'); if (string.IsNullOrEmpty(filePath)) throw new PluginException(PluginErrorCodes.InvalidFileOrDirectoryName); StorageFileStreamResult result = new StorageFileStreamResult(); Uri uri = new Uri(model.ServerUrl); ICloudStorageConfiguration config = new WebDavConfiguration(uri); GenericNetworkCredentials cred = new GenericNetworkCredentials(); cred.UserName = model.AccountLogin; cred.Password = model.AccountPassword; CloudStorage storage = null; try { storage = new CloudStorage(); ICloudStorageAccessToken storageToken = storage.Open(config, cred); var file = storage.GetFile(filePath, null); result.FileName = file.Name; result.FileStream = file.GetDataTransferAccessor().GetDownloadStream(); } finally { if (storage != null) storage.Close(); } return result; }
public object GetAccountConfigurationModel(int accountId) { IEnumerable<StorageAccountSetting> settings = _accountService.GetSettingsForStoargeAccount(accountId); var model = new WebDavConfigurationModel { ServerUrl = settings.Where(s => string.Equals("server", s.SettingName, StringComparison.OrdinalIgnoreCase)) .Select(s => s.SettingValue) .FirstOrDefault(), AccountLogin = settings.Where(s => string.Equals("login", s.SettingName, StringComparison.OrdinalIgnoreCase)) .Select(s => s.SettingValue) .FirstOrDefault(), }; string accountPasswordEncrypted = settings.Where(s => string.Equals("password", s.SettingName, StringComparison.OrdinalIgnoreCase)) .Select(s => s.SettingValue) .FirstOrDefault(); if (!string.IsNullOrEmpty(accountPasswordEncrypted)) model.AccountPassword = _cryptoService.DecryptString(accountPasswordEncrypted, _securityConfiguration.EncryptionKey, _securityConfiguration.EncryptionSalt); return model; }
public object GetAccountConfigurationModel(int accountId) { IEnumerable <StorageAccountSetting> settings = _accountService.GetSettingsForStoargeAccount(accountId); var model = new WebDavConfigurationModel { ServerUrl = settings.Where(s => string.Equals("server", s.SettingName, StringComparison.OrdinalIgnoreCase)) .Select(s => s.SettingValue) .FirstOrDefault(), AccountLogin = settings.Where(s => string.Equals("login", s.SettingName, StringComparison.OrdinalIgnoreCase)) .Select(s => s.SettingValue) .FirstOrDefault(), }; string accountPasswordEncrypted = settings.Where(s => string.Equals("password", s.SettingName, StringComparison.OrdinalIgnoreCase)) .Select(s => s.SettingValue) .FirstOrDefault(); if (!string.IsNullOrEmpty(accountPasswordEncrypted)) { model.AccountPassword = _cryptoService.DecryptString(accountPasswordEncrypted, _securityConfiguration.EncryptionKey, _securityConfiguration.EncryptionSalt); } return(model); }
public virtual StorageFolderResult QueryStorage(int accountId, string path) { WebDavConfigurationModel model = GetAccountConfigurationModel(accountId) as WebDavConfigurationModel; return(BuildQueryExecutor(() => FolderQueryReference.Target.Execute(model, path, accountId)).Run <StorageFolderResult>()); }
public virtual StorageFileStreamResult GetFileStream(string fileUrl, int accountId) { WebDavConfigurationModel model = GetAccountConfigurationModel(accountId) as WebDavConfigurationModel; return(BuildQueryExecutor(() => FileStreamQueryReference.Target.Execute(model, fileUrl)).Run <StorageFileStreamResult>()); }
public StorageFolderResult Execute(WebDavConfigurationModel model, string path, int accountId) { model = model.With(m => !string.IsNullOrEmpty(m.ServerUrl)) .With(m => !string.IsNullOrEmpty(m.AccountLogin)) .With(m => !string.IsNullOrEmpty(m.AccountPassword)); if (model == null) { throw new PluginException(PluginErrorCodes.InvalidCredentialsOrConfiguration); } StorageFolderResult result = new StorageFolderResult(); Uri uri = new Uri(model.ServerUrl); ICloudStorageConfiguration config = new WebDavConfiguration(uri); GenericNetworkCredentials cred = new GenericNetworkCredentials(); cred.UserName = model.AccountLogin; cred.Password = model.AccountPassword; if (string.IsNullOrEmpty(path)) { path = "/"; } else { path = path.RemoveCharDuplicates('/'); } if (!path.Equals("/", StringComparison.OrdinalIgnoreCase)) { path = path.TrimEnd('/'); } CloudStorage storage = null; try { storage = new CloudStorage(); ICloudStorageAccessToken storageToken = storage.Open(config, cred); ICloudDirectoryEntry directory = storage.GetFolder(path, true); result.CurrentFolderName = directory.Name; result.CurrentFolderUrl = path; path = path.TrimEnd('/'); foreach (var entry in directory) { var dirEntry = entry as ICloudDirectoryEntry; string entryPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, "/", entry.Name); if (dirEntry != null) { result.AddItem(new StorageFolder { Name = dirEntry.Name, Path = entryPath, StorageAccountId = accountId }); } else { result.AddItem(new StorageFile { Name = entry.Name, Path = entryPath, StorageAccountId = accountId }); } } StoragePathItem rootPath = new StoragePathItem { Name = "/", Url = "/" }; string relativePath = path.Trim('/').RemoveCharDuplicates('/'); if (relativePath.Length > 0) { string[] pathItems = relativePath.Split('/'); StringBuilder pathUrlsBuilder = new StringBuilder("/"); foreach (var pathItem in pathItems) { pathUrlsBuilder.Append(pathItem); rootPath.AppendItem(new StoragePathItem { Name = pathItem, Url = pathUrlsBuilder.ToString() }); pathUrlsBuilder.Append("/"); } } result.CurrentPath = rootPath; } finally { if (storage != null) { storage.Close(); } } return(result); }
public StorageFolderResult Execute(WebDavConfigurationModel model, string path, int accountId) { model = model.With(m => !string.IsNullOrEmpty(m.ServerUrl)) .With(m => !string.IsNullOrEmpty(m.AccountLogin)) .With(m => !string.IsNullOrEmpty(m.AccountPassword)); if (model == null) throw new PluginException(PluginErrorCodes.InvalidCredentialsOrConfiguration); StorageFolderResult result = new StorageFolderResult(); Uri uri = new Uri(model.ServerUrl); ICloudStorageConfiguration config = new WebDavConfiguration(uri); GenericNetworkCredentials cred = new GenericNetworkCredentials(); cred.UserName = model.AccountLogin; cred.Password = model.AccountPassword; if (string.IsNullOrEmpty(path)) path = "/"; else path = path.RemoveCharDuplicates('/'); if (!path.Equals("/", StringComparison.OrdinalIgnoreCase)) path = path.TrimEnd('/'); CloudStorage storage = null; try { storage = new CloudStorage(); ICloudStorageAccessToken storageToken = storage.Open(config, cred); ICloudDirectoryEntry directory = storage.GetFolder(path, true); result.CurrentFolderName = directory.Name; result.CurrentFolderUrl = path; path = path.TrimEnd('/'); foreach (var entry in directory) { var dirEntry = entry as ICloudDirectoryEntry; string entryPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, "/", entry.Name); if (dirEntry != null) { result.AddItem(new StorageFolder { Name = dirEntry.Name, Path = entryPath, StorageAccountId = accountId }); } else { result.AddItem(new StorageFile { Name = entry.Name, Path = entryPath, StorageAccountId = accountId }); } } StoragePathItem rootPath = new StoragePathItem { Name = "/", Url = "/" }; string relativePath = path.Trim('/').RemoveCharDuplicates('/'); if (relativePath.Length > 0) { string[] pathItems = relativePath.Split('/'); StringBuilder pathUrlsBuilder = new StringBuilder("/"); foreach (var pathItem in pathItems) { pathUrlsBuilder.Append(pathItem); rootPath.AppendItem(new StoragePathItem { Name = pathItem, Url = pathUrlsBuilder.ToString() }); pathUrlsBuilder.Append("/"); } } result.CurrentPath = rootPath; } finally { if (storage != null) storage.Close(); } return result; }