public ActionResult GetFolder(int id, string path)
        {
            FolderModel model = null;

            var resultAction = Condition()
                               .DoIfNotAjax(() => View(model))
                               .DoIfAjax(() => Json(new AjaxResult
            {
                MainPanelHtml = this.RenderViewToString("~/Views/StorageAccount/Controls/StorageAccountFolderControl.ascx", model)
            }, JsonRequestBehavior.AllowGet));

            StorageAccount account = _storageAccountService.Load(id);

            if (account == null)
            {
                ModelState.AddModelError("notfound", ValidationResources.StorageAccountNotFoundError);
                return(resultAction);
            }
            Identity identity = (Identity)HttpContext.User.Identity;

            if (identity.UserId != account.UserId)
            {
                ModelState.AddModelError("forbidden", ValidationResources.NoPermissionsError);
                return(resultAction);
            }
#warning call service not plugin dirrectly
            IStoragePlugin storagePlugin = _storagePluginsService.GetStoragePlugin(account.StoragePluginId);
            if (storagePlugin == null)
            {
                ModelState.AddModelError("storage_plugin", ValidationResources.StoragePluginNotFoundError);
                return(resultAction);
            }

            StorageFolderResult queryModel = null;

            try
            {
                queryModel = storagePlugin.QueryStorage(id, path);
            }
            catch (PluginException ex)
            {
                ModelState.AddModelError("storage_plugin", GetErrorMessage(ex.ErrorCode));
                if (ex.ErrorCode == PluginErrorCodes.PluginError)
                {
                    Logger.Error("Storage plugin error", ex);
                }
                return(resultAction);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("storage_plugin", ErrorResources.PluginError);
                Logger.Error("Storage plugin error", ex);
                return(resultAction);
            }

            model = new FolderModel()
            {
                Content        = queryModel,
                StorageAccount = account
            };

            return(resultAction);
        }
        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);
        }
示例#3
0
        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;
        }