public StorageFileStreamResult DownloadFile(StorageAccount storageAccount, string url)
        {
            if (storageAccount == null)
                throw new ArgumentNullException("storageAccount");
            if (string.IsNullOrEmpty(url))
                throw new ArgumentNullException("url");

            IStoragePlugin plugin = GetStoragePlugin(storageAccount.StoragePluginId);
            if (plugin == null)
                throw new StoragePluginNotFoundException(storageAccount.StoragePluginId);

            var streamResult = plugin.GetFileStream(url, storageAccount.Id);
            streamResult.FileStream = _streamFactory.MakeDownloadStream(streamResult.FileStream);
            return streamResult;
        }
        public StorageAccountCreationResult CreateStorageAccount(StorageAccount account)
        {
            if (account == null)
                throw new ArgumentNullException("account");

            return _connectionProvider.DoInTransaction(() =>
            {
                if (_storagePluginsService.GetStoragePlugin(account.StoragePluginId) == null)
                    return StorageAccountCreationResult.PluginNotFound;

                StorageAccount checkAccount = _storageAccountRepository.Load(account.AccountName, account.StoragePluginId, account.UserId);

                if (checkAccount != null)
                    return StorageAccountCreationResult.AccountExists;

                _storageAccountRepository.Insert(account);

                return StorageAccountCreationResult.Success;
            });
        }
        public StorageAccount Insert(StorageAccount account)
        {
            if (account == null)
                throw new ArgumentNullException("account");

            return SqlQueryExecutor.Execute(() =>
            {
                String query = string.Format(CultureInfo.InvariantCulture, "INSERT INTO {1} {0}; SELECT LAST_INSERT_ID();", InsertFieldList, TableName);
                int insertedId = (int)_connectionProvider.CurrentConnection.Query<long>(query, new { account.AccountName, account.StoragePluginId, account.UserId, account.Stamp }).FirstOrDefault();
                if (insertedId <= 0)
                    throw new MonsterDbException("Account insertion failed");

                String idAndStampQuery = string.Format(CultureInfo.InvariantCulture, "SELECT id AS Id, stamp AS Stamp FROM {0} WHERE id=@Id;", TableName);
                IdAndStamp idAndStamp = _connectionProvider.CurrentConnection.Query<IdAndStamp>(idAndStampQuery, new { Id = insertedId }).FirstOrDefault();

                if (idAndStamp == null)
                    throw new MonsterDbException("Account insertion failed");

                account.Id = idAndStamp.Id;
                account.Stamp = idAndStamp.Stamp;
                return account;
            });
        }
        public ActionResult Add(StorageAccountModel storageAccountModel)
        {
            if (storageAccountModel == null)
            {
                storageAccountModel = new StorageAccountModel();
                ModelState.AddModelError("empty_model", ValidationResources.AccountInvalidError);
            }
            IEnumerable<SelectListItem> storagePlugins = GetSupportedStoragePlugins();
            storageAccountModel.StoragePlugins = storagePlugins;

            if (ModelState.IsValid)
            {
                StorageAccount account = new StorageAccount
                    {
                        AccountName = storageAccountModel.AccountName,
                        StoragePluginId = storageAccountModel.PluginId,
                        UserId = User.Identity.UserId
                    };
                StorageAccountCreationResult result = _storageAccountService.CreateStorageAccount(account);
                switch (result)
                {
                    case StorageAccountCreationResult.PluginNotFound:
                        ModelState.AddModelError("storage_plugin_not_found", ValidationResources.StoragePluginNotFoundError);
                        break;
                    case StorageAccountCreationResult.AccountExists:
                        ModelState.AddModelError("storage_account_exists", ValidationResources.AccountExistsError);
                        break;
                    case StorageAccountCreationResult.Success:
                        return RedirectToAction("Edit", new {account.Id});
                }
            }

            return View(storageAccountModel);
        }