bugs fix
This commit is contained in:
parent
f2c54df2b0
commit
41a540bd1e
13 changed files with 165 additions and 114 deletions
|
@ -21,16 +21,8 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
|
||||
private readonly ILog Log;
|
||||
|
||||
private IFolder _currentFolder;
|
||||
private bool _isRoot = true;
|
||||
|
||||
private Lazy<IList<SystemFile>> _rootFolders;
|
||||
private Lazy<string> _webDavRootPath;
|
||||
|
||||
public string RootPath
|
||||
{
|
||||
get { return _webDavRootPath.Value; }
|
||||
}
|
||||
private IFolder _currentFolder;
|
||||
|
||||
public WebDavManager(ICryptography cryptography)
|
||||
{
|
||||
|
@ -38,49 +30,29 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
Log = LogManager.GetLogger(this.GetType());
|
||||
|
||||
_webDavSession = new WebDavSession();
|
||||
|
||||
_rootFolders = new Lazy<IList<SystemFile>>(ConnectToWebDavServer);
|
||||
_webDavRootPath = new Lazy<string>(() =>
|
||||
{
|
||||
if (_rootFolders.Value.Any())
|
||||
{
|
||||
var folder = _rootFolders.Value.First();
|
||||
var uri = new Uri(folder.Url);
|
||||
return uri.Scheme + "://" + uri.Host + uri.Segments[0] + uri.Segments[1];
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void OpenFolder(string pathPart)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pathPart))
|
||||
{
|
||||
_isRoot = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_isRoot = false;
|
||||
|
||||
_webDavSession.Credentials = new NetworkCredential(WspContext.User.Login, _cryptography.Decrypt(WspContext.User.EncryptedPassword), WebDavAppConfigManager.Instance.UserDomain);
|
||||
|
||||
_currentFolder = _webDavSession.OpenFolder(_webDavRootPath.Value + pathPart);
|
||||
}
|
||||
|
||||
public IEnumerable<IHierarchyItem> GetChildren()
|
||||
public IEnumerable<IHierarchyItem> OpenFolder(string pathPart)
|
||||
{
|
||||
IHierarchyItem[] children;
|
||||
|
||||
if (_isRoot)
|
||||
if (string.IsNullOrWhiteSpace(pathPart))
|
||||
{
|
||||
children = _rootFolders.Value.Select(x => new WebDavHierarchyItem {Href = new Uri(x.Url), ItemType = ItemType.Folder}).ToArray();
|
||||
children = ConnectToWebDavServer().Select(x => new WebDavHierarchyItem { Href = new Uri(x.Url), ItemType = ItemType.Folder }).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
if (_currentFolder == null || _currentFolder.Path.ToString() != pathPart)
|
||||
{
|
||||
_webDavSession.Credentials = new NetworkCredential(WspContext.User.Login,
|
||||
_cryptography.Decrypt(WspContext.User.EncryptedPassword),
|
||||
WebDavAppConfigManager.Instance.UserDomain);
|
||||
|
||||
_currentFolder = _webDavSession.OpenFolder(string.Format("{0}{1}/{2}",WebDavAppConfigManager.Instance.WebdavRoot, WspContext.User.OrganizationId , pathPart));
|
||||
}
|
||||
|
||||
children = _currentFolder.GetChildren();
|
||||
}
|
||||
}
|
||||
|
||||
List<IHierarchyItem> sortedChildren = children.Where(x => x.ItemType == ItemType.Folder).OrderBy(x => x.DisplayName).ToList();
|
||||
sortedChildren.AddRange(children.Where(x => x.ItemType != ItemType.Folder).OrderBy(x => x.DisplayName));
|
||||
|
@ -88,30 +60,40 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
return sortedChildren;
|
||||
}
|
||||
|
||||
public bool IsFile(string fileName)
|
||||
public bool IsFile(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fileName) | _currentFolder == null)
|
||||
return false;
|
||||
string folder = GetFileFolder(path);
|
||||
|
||||
try
|
||||
if (string.IsNullOrWhiteSpace(folder))
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
//Stream stream = resource.GetReadStream();
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
}
|
||||
return false;
|
||||
|
||||
var resourceName = GetResourceName(path);
|
||||
|
||||
OpenFolder(folder);
|
||||
|
||||
IResource resource = _currentFolder.GetResource(resourceName);
|
||||
|
||||
return resource.ItemType == ItemType.Resource;
|
||||
}
|
||||
|
||||
public byte[] GetFileBytes(string fileName)
|
||||
|
||||
public byte[] GetFileBytes(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
string folder = GetFileFolder(path);
|
||||
|
||||
var resourceName = GetResourceName(path);
|
||||
|
||||
OpenFolder(folder);
|
||||
|
||||
IResource resource = _currentFolder.GetResource(resourceName);
|
||||
|
||||
Stream stream = resource.GetReadStream();
|
||||
byte[] fileBytes = ReadFully(stream);
|
||||
|
||||
return fileBytes;
|
||||
}
|
||||
catch (InvalidOperationException exception)
|
||||
|
@ -120,12 +102,17 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
}
|
||||
}
|
||||
|
||||
public IResource GetResource(string fileName)
|
||||
public IResource GetResource(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
return resource;
|
||||
string folder = GetFileFolder(path);
|
||||
|
||||
var resourceName = GetResourceName(path);
|
||||
|
||||
OpenFolder(folder);
|
||||
|
||||
return _currentFolder.GetResource(resourceName);
|
||||
}
|
||||
catch (InvalidOperationException exception)
|
||||
{
|
||||
|
@ -133,11 +120,17 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
}
|
||||
}
|
||||
|
||||
public string GetFileUrl(string fileName)
|
||||
public string GetFileUrl(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
string folder = GetFileFolder(path);
|
||||
|
||||
var resourceName = GetResourceName(path);
|
||||
|
||||
OpenFolder(folder);
|
||||
|
||||
IResource resource = _currentFolder.GetResource(resourceName);
|
||||
return resource.Href.ToString();
|
||||
}
|
||||
catch (InvalidOperationException exception)
|
||||
|
@ -171,19 +164,6 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
return rootFolders;
|
||||
}
|
||||
|
||||
private byte[] ReadFully(Stream input)
|
||||
{
|
||||
var buffer = new byte[16*1024];
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
int read;
|
||||
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
|
||||
ms.Write(buffer, 0, read);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string CreateFileId(string path)
|
||||
{
|
||||
return _cryptography.Encrypt(path).Replace("/", "AAAAA");
|
||||
|
@ -193,5 +173,45 @@ namespace WebsitePanel.WebDav.Core.Managers
|
|||
{
|
||||
return _cryptography.Decrypt(id.Replace("AAAAA", "/"));
|
||||
}
|
||||
|
||||
#region Helpers
|
||||
|
||||
private byte[] ReadFully(Stream input)
|
||||
{
|
||||
var buffer = new byte[16 * 1024];
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
int read;
|
||||
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
|
||||
ms.Write(buffer, 0, read);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private string GetFileFolder(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !path.Contains('/'))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
string fileName = path.Split('/').Last();
|
||||
int index = path.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase);
|
||||
string folder = path.Remove(index - 1, fileName.Length + 1);
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
private string GetResourceName(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !path.Contains('/'))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return path.Split('/').Last(); ;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue