using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Library.ExtensionMethod;
using Sphorium.WebDAV.Server.Framework.BaseClasses;
using Sphorium.WebDAV.Server.Framework.Resources;
using Everest.Library;
using System.Collections.Specialized;
using Everest.CmsServices.MvcHelper;
namespace Everest.CmsServices.Rfc.WebDAV{
public class DavPropFind : DavPropFindBase
{
public DavPropFind()
{
this.ProcessDavRequest += new DavProcessEventHandler(DavPropFind_ProcessDavRequest);
}
void DavPropFind_ProcessDavRequest(object sender, EventArgs e)
{
WebDavUrlData urlData = new WebDavUrlData(WebDavHelper.GetRawHostUrl(HttpApplication.Request), RelativeRequestPath);
if (HttpApplication.Request.Headers["Depth"] == "0")
{
switch (urlData.UrlType)
{
case WebDavUrlType.Root:
OutputRoot(urlData);
break;
case WebDavUrlType.Application:
OutputApplication(urlData);
break;
case WebDavUrlType.Folder:
OutputFolder(urlData);
break;
case WebDavUrlType.Content:
ContentRequest(urlData);
break;
default:
break;
}
//HttpApplication.Response.Write(@"<?xml version=""1.0"" encoding=""utf-8""?><D:multistatus xmlns:D=""DAV:""><D:response><D:href>http://www.smallbusiness.com:10665/</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype>application/octet-stream</D:getcontenttype><D:getlastmodified>2010-03-18T17:47:55Z</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:getetag/><D:displayname>/</D:displayname><D:getcontentlanguage/><D:getcontentlength>0</D:getcontentlength><D:iscollection>1</D:iscollection><D:resourcetype><D:collection/></D:resourcetype></D:prop></D:propstat></D:response></D:multistatus>");
}
else
{
switch (urlData.UrlType)
{
case WebDavUrlType.Root:
OutputRoot(urlData);
RootRequest(urlData);
break;
case WebDavUrlType.Application:
OutputApplication(urlData);
ApplicationRequest(urlData);
break;
case WebDavUrlType.Folder:
OutputFolder(urlData);
FolderReqeust(urlData);
break;
case WebDavUrlType.Content:
ContentRequest(urlData);
break;
default:
break;
}
}
//var _dirInfo = new DirectoryInfo(@"E:\Opensources\WebDav\Sphorium.WebDAV.Server.Framework_111609_001247_ver3.1-src\Sphorium.WebDAV.Server.Framework\Framework");
//foreach (DirectoryInfo _subDir in _dirInfo.GetDirectories())
//{
// //TODO: Only populate the requested properties
// DavFolder _davFolder = new DavFolder(_subDir.Name, _subDir.Name);
// _davFolder.CreationDate = _subDir.CreationTime;
// _davFolder.LastModified = _subDir.LastWriteTime.ToUniversalTime();
// _davFolder.ContentLength = _subDir.GetDirectories().Length + _subDir.GetFiles().Length;
// base.CollectionResources.Add(_davFolder);
//}
}
private void OutputRoot(WebDavUrlData urlData)
{
var url = WebDavHelper.GetCurrentUrl(HttpApplication.Request);
DavFolder davFolder = new DavFolder("/", url);
davFolder.LastModified = DateTime.Now;
davFolder.CreationDate = DateTime.Now;
base.CollectionResources.Add(davFolder);
}
private void OutputApplication(WebDavUrlData urlData)
{
var url = WebDavHelper.GetCurrentUrl(HttpApplication.Request);
var app = CachedData.GetApplication(urlData.Application);
if (app != null)
{
DavFolder davFolder = new DavFolder(app.ApplicationName, url);
davFolder.LastModified = DateTime.Now;
davFolder.CreationDate = DateTime.Now;
base.CollectionResources.Add(davFolder);
}
}
private void OutputFolder(WebDavUrlData urlData)
{
var url = WebDavHelper.GetCurrentUrl(HttpApplication.Request);
var folder = CachedData.GetFolder(urlData.Application, urlData.FolderName);
if (folder != null)
{
DavFolder davFolder = new DavFolder(folder.FolderName, url);
davFolder.LastModified = DateTime.Now;
davFolder.CreationDate = DateTime.Now;
base.CollectionResources.Add(davFolder);
}
}
#region RootRequest
private void RootRequest(WebDavUrlData urlData)
{
string host = HttpApplication.Request.GetRawHostWithoutPort();
var app = CachedData.GetApplicationByHostName(host);
if (app != null)
{
OutputApplications(urlData, new aspnet_Applications[] { app });
}
else
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
OutputApplications(urlData, dataContext.aspnet_Applications);
}
}
private void OutputApplications(WebDavUrlData urlData, IEnumerable<aspnet_Applications> applications)
{
PermissionService permissionService = UnityManager.Resolve<PermissionService>();
foreach (var app in applications)
{
if (permissionService.CanAccessApplication(WebDavHelper.GetUserName(HttpApplication), app.ApplicationName))
{
DavFolder appFolder = new DavFolder(app.ApplicationName, urlData.BuildNextUrl(app.ApplicationName));
if (app.CreateDate != null)
{
appFolder.CreationDate = app.CreateDate.Value;
}
else
{
appFolder.CreationDate = DateTime.Now;
}
appFolder.LastModified = DateTime.Now;
appFolder.ContentLength = 0;
base.CollectionResources.Add(appFolder);
}
}
}
#endregion
#region ApplicationRequest
private void ApplicationRequest(WebDavUrlData urlData)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var folders = dataContext.QueryFolders(urlData.Application, FolderType.BinaryContent);
OutputFolders(urlData, folders);
}
private void OutputFolders(WebDavUrlData urlData, IEnumerable<Cms_Folder> folders)
{
PermissionService permissionService = UnityManager.Resolve<PermissionService>();
foreach (var folder in folders)
{
if (permissionService.IsAllowedEditContent(WebDavHelper.GetUserName(HttpApplication), folder.UUID, urlData.Application, ActionType.View))
{
DavFolder davFolder = new DavFolder(folder.FolderName, urlData.BuildNextUrl(folder.FolderName.ToString()));
davFolder.CreationDate = DateTime.Now;
davFolder.LastModified = DateTime.Now;
davFolder.ContentLength = 0;
base.CollectionResources.Add(davFolder);
}
}
}
#endregion
#region Folder Request
private void FolderReqeust(WebDavUrlData urlData)
{
BinaryContentService binaryContentService = UnityManager.Resolve<BinaryContentService>();
var folder = CachedData.GetFolder(urlData.Application, urlData.FolderName);
int count;
var contents = binaryContentService.QueryBinaryContents(folder.UUID, WebDavHelper.GetUserName(HttpApplication), "", null, null, "", out count, 1, int.MaxValue);
OutputContents(urlData, contents);
}
private void OutputContents(WebDavUrlData urlData, IEnumerable<IDictionary<string, object>> contents)
{
foreach (var content in contents)
{
OutputContent(urlData, content);
}
}
private void OutputContent(WebDavUrlData urlData, IDictionary<string, object> content)
{
var filePath = content["FilePath"].ToString();
var extension = Path.GetExtension(filePath);
DavFile file = new DavFile(content["Title"].ToString() + extension, urlData.BuildNextUrl(content["UserKey"].ToString() + extension));
file.CreationDate = (DateTime)content["PostDate"];
file.LastModified = (DateTime)content["ModifiedDate"];
file.ContentLength = (int)content["FileSize"];
base.FileResources.Add(file);
}
#endregion
#region Content Request
private void ContentRequest(WebDavUrlData urlData)
{
ContentService contentService = new ContentService(urlData.Application);
var content = contentService.SetQueryType(Everest.CmsServices.Providers.QueryType.All).GetBinaryContentByUserKey(urlData.ContentKey, null);
if (content == null)
{
this.AbortRequest(ServerResponseCode.NotFound);
}
else
{
OutputContent(urlData, content);
}
}
#endregion
}
}
|