webdav portal upload func added
This commit is contained in:
parent
1da7c6c3b3
commit
a882072b02
23 changed files with 553 additions and 17 deletions
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace WebsitePanel.WebDav.Core.Extensions
|
||||
{
|
||||
static class UriExtensions
|
||||
{
|
||||
public static Uri Append(this Uri uri, params string[] paths)
|
||||
{
|
||||
return new Uri(paths.Aggregate(uri.AbsoluteUri, (current, path) => string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/'))));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -110,6 +110,23 @@ namespace WebsitePanel.WebDav.Core
|
|||
webClient.UploadFile(Href, "PUT", filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads content of a file specified by filename to the server
|
||||
/// </summary>
|
||||
/// <param name="data">Posted file data to be uploaded</param>
|
||||
public void Upload(byte[] data)
|
||||
{
|
||||
var credentials = (NetworkCredential)_credentials;
|
||||
string auth = "Basic " +
|
||||
Convert.ToBase64String(
|
||||
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
|
||||
var webClient = new WebClient();
|
||||
webClient.Credentials = credentials;
|
||||
webClient.Headers.Add("Authorization", auth);
|
||||
|
||||
webClient.UploadData(Href, "PUT", data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads content of the resource from WebDAV server.
|
||||
/// </summary>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
using WebsitePanel.WebDav.Core.Client;
|
||||
|
||||
namespace WebsitePanel.WebDav.Core.Interfaces.Managers
|
||||
|
@ -8,6 +9,7 @@ namespace WebsitePanel.WebDav.Core.Interfaces.Managers
|
|||
IEnumerable<IHierarchyItem> OpenFolder(string path);
|
||||
bool IsFile(string path);
|
||||
byte[] GetFileBytes(string path);
|
||||
void UploadFile(string path, HttpPostedFileBase file);
|
||||
IResource GetResource(string path);
|
||||
string GetFileUrl(string path);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
using WebsitePanel.WebDav.Core.Security.Authentication.Principals;
|
||||
using WebsitePanel.WebDav.Core.Security.Authorization.Enums;
|
||||
|
||||
namespace WebsitePanel.WebDav.Core.Interfaces.Security
|
||||
{
|
||||
public interface IWebDavAuthorizationService
|
||||
{
|
||||
bool HasAccess(WspPrincipal principal, string path);
|
||||
WebDavPermissions GetPermissions(WspPrincipal principal, string path);
|
||||
}
|
||||
}
|
|
@ -3,11 +3,15 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Xml.Serialization;
|
||||
using log4net;
|
||||
using WebsitePanel.Providers.OS;
|
||||
using WebsitePanel.WebDav.Core.Client;
|
||||
using WebsitePanel.WebDav.Core.Config;
|
||||
using WebsitePanel.WebDav.Core.Exceptions;
|
||||
using WebsitePanel.WebDav.Core.Extensions;
|
||||
using WebsitePanel.WebDav.Core.Interfaces.Managers;
|
||||
using WebsitePanel.WebDav.Core.Security.Cryptography;
|
||||
using WebsitePanel.WebDav.Core.Wsp.Framework;
|
||||
|
@ -108,6 +112,24 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
}
|
||||
}
|
||||
|
||||
public void UploadFile(string path, HttpPostedFileBase file)
|
||||
{
|
||||
var resource = new WebDavResource();
|
||||
|
||||
var fileUrl = new Uri(WebDavAppConfigManager.Instance.WebdavRoot)
|
||||
.Append(WspContext.User.OrganizationId)
|
||||
.Append(path)
|
||||
.Append(Path.GetFileName(file.FileName));
|
||||
|
||||
resource.SetHref(fileUrl);
|
||||
resource.SetCredentials(new NetworkCredential(WspContext.User.Login, _cryptography.Decrypt(WspContext.User.EncryptedPassword)));
|
||||
|
||||
file.InputStream.Seek(0, SeekOrigin.Begin);
|
||||
var bytes = ReadFully(file.InputStream);
|
||||
|
||||
resource.Upload(bytes);
|
||||
}
|
||||
|
||||
public IResource GetResource(string path)
|
||||
{
|
||||
try
|
||||
|
@ -184,6 +206,14 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
}
|
||||
}
|
||||
|
||||
public void WriteTo(Stream sourceStream, Stream targetStream)
|
||||
{
|
||||
byte[] buffer = new byte[16 * 1024];
|
||||
int n;
|
||||
while ((n = sourceStream.Read(buffer, 0, buffer.Length)) != 0)
|
||||
targetStream.Write(buffer, 0, n);
|
||||
}
|
||||
|
||||
private string GetFileFolder(string path)
|
||||
{
|
||||
path = path.TrimEnd('/');
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
|
||||
namespace WebsitePanel.WebDav.Core.Security.Authorization.Enums
|
||||
{
|
||||
[Flags]
|
||||
public enum WebDavPermissions
|
||||
{
|
||||
Empty = 0,
|
||||
None = 1,
|
||||
Read = 2,
|
||||
Write = 4
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using WebsitePanel.WebDav.Core.Interfaces.Security;
|
||||
using WebsitePanel.WebDav.Core.Security.Authentication.Principals;
|
||||
using WebsitePanel.WebDav.Core.Security.Authorization.Enums;
|
||||
using WebsitePanel.WebDav.Core.Wsp.Framework;
|
||||
|
||||
namespace WebsitePanel.WebDav.Core.Security.Authorization
|
||||
{
|
||||
public class WebDavAuthorizationService : IWebDavAuthorizationService
|
||||
{
|
||||
public bool HasAccess(WspPrincipal principal, string path)
|
||||
{
|
||||
var permissions = GetPermissions(principal, path);
|
||||
|
||||
return permissions.HasFlag(WebDavPermissions.Read) || permissions.HasFlag(WebDavPermissions.Write);
|
||||
}
|
||||
|
||||
public WebDavPermissions GetPermissions(WspPrincipal principal, string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return WebDavPermissions.Read;
|
||||
}
|
||||
|
||||
var resultPermissions = WebDavPermissions.Empty;
|
||||
|
||||
var rootFolder = GetRootFolder(path);
|
||||
|
||||
var userGroups = WSP.Services.Organizations.GetSecurityGroupsByMember(principal.ItemId, principal.AccountId);
|
||||
|
||||
var rootFolders = WSP.Services.EnterpriseStorage.GetEnterpriseFolders(principal.ItemId);
|
||||
|
||||
var esRootFolder = rootFolders.FirstOrDefault(x => x.Name == rootFolder);
|
||||
|
||||
if (esRootFolder == null)
|
||||
{
|
||||
return WebDavPermissions.None;
|
||||
}
|
||||
|
||||
var permissions = WSP.Services.EnterpriseStorage.GetEnterpriseFolderPermissions(principal.ItemId, esRootFolder.Name);
|
||||
|
||||
foreach (var permission in permissions)
|
||||
{
|
||||
if ((!permission.IsGroup
|
||||
&& (permission.DisplayName == principal.UserName || permission.DisplayName == principal.DisplayName))
|
||||
|| (permission.IsGroup && userGroups.Any(x => x.DisplayName == permission.DisplayName)))
|
||||
{
|
||||
if (permission.Access.ToLowerInvariant().Contains("read"))
|
||||
{
|
||||
resultPermissions |= WebDavPermissions.Read;
|
||||
}
|
||||
|
||||
if (permission.Access.ToLowerInvariant().Contains("write"))
|
||||
{
|
||||
resultPermissions |= WebDavPermissions.Write;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultPermissions;
|
||||
}
|
||||
|
||||
private string GetRootFolder(string path)
|
||||
{
|
||||
return path.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)[0];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -123,10 +123,12 @@
|
|||
<Compile Include="Exceptions\UnauthorizedException.cs" />
|
||||
<Compile Include="Exceptions\WebDavException.cs" />
|
||||
<Compile Include="Exceptions\WebDavHttpException.cs" />
|
||||
<Compile Include="Extensions\UriExtensions.cs" />
|
||||
<Compile Include="IConnectionSettings.cs" />
|
||||
<Compile Include="IFolder.cs" />
|
||||
<Compile Include="IHierarchyItem.cs" />
|
||||
<Compile Include="IItemContent.cs" />
|
||||
<Compile Include="Interfaces\Security\IWebDavAuthorizationService.cs" />
|
||||
<Compile Include="Managers\AccessTokenManager.cs" />
|
||||
<Compile Include="Interfaces\Managers\IAccessTokenManager.cs" />
|
||||
<Compile Include="Interfaces\Managers\IWebDavManager.cs" />
|
||||
|
@ -145,6 +147,8 @@
|
|||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>HttpErrors.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Security\Authorization\Enums\WebDavPermissions.cs" />
|
||||
<Compile Include="Security\Authorization\WebDavAuthorizationService.cs" />
|
||||
<Compile Include="Security\Cryptography\CryptoUtils.cs" />
|
||||
<Compile Include="Security\Cryptography\ICryptography.cs" />
|
||||
<Compile Include="Security\Authentication\FormsAuthenticationService.cs" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue