Extend DirectoryResource for multiple SourceDirectories to be overlayed
parent
7fed29a80e
commit
05ef2c8374
|
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using ln.templates;
|
||||
using System.Collections;
|
||||
using ln.types;
|
||||
|
||||
namespace ln.http.resources
|
||||
{
|
||||
|
@ -13,25 +15,37 @@ namespace ln.http.resources
|
|||
{
|
||||
public ResourceTypeHookDelegate ResourceTypeHook { get; set; }
|
||||
|
||||
public virtual DirectoryInfo DirectoryInfo { get; }
|
||||
|
||||
protected Dictionary<string, Resource> cache = new Dictionary<string, Resource>();
|
||||
protected Dictionary<string, Resource> injectedResources = new Dictionary<string, Resource>();
|
||||
|
||||
IEnumerable<string> searchPaths;
|
||||
|
||||
public DirectoryResource(IEnumerable<string> search)
|
||||
: base("")
|
||||
{
|
||||
searchPaths = search;
|
||||
}
|
||||
public DirectoryResource(Resource container,IEnumerable<string> search)
|
||||
: base(container, System.IO.Path.GetFileName(search.First()))
|
||||
{
|
||||
searchPaths = search;
|
||||
}
|
||||
|
||||
public DirectoryResource(String path)
|
||||
: base("")
|
||||
{
|
||||
DirectoryInfo = new DirectoryInfo(path);
|
||||
searchPaths = new string[] { path };
|
||||
}
|
||||
public DirectoryResource(String path,string name)
|
||||
: base(name)
|
||||
{
|
||||
DirectoryInfo = new DirectoryInfo(path);
|
||||
searchPaths = new string[] { path };
|
||||
}
|
||||
public DirectoryResource(Resource container, String path)
|
||||
: base(container, System.IO.Path.GetFileName(path))
|
||||
{
|
||||
DirectoryInfo = new DirectoryInfo(path);
|
||||
searchPaths = new string[] { path };
|
||||
|
||||
if (container is DirectoryResource)
|
||||
{
|
||||
DirectoryResource parent = container as DirectoryResource;
|
||||
|
@ -39,23 +53,22 @@ namespace ln.http.resources
|
|||
}
|
||||
}
|
||||
|
||||
protected String GetCombinedPath(string rel)
|
||||
{
|
||||
return System.IO.Path.Combine(DirectoryInfo.FullName, rel);
|
||||
}
|
||||
//protected String GetCombinedPath(string rel)
|
||||
//{
|
||||
// return System.IO.Path.Combine(DirectoryInfo.FullName, rel);
|
||||
//}
|
||||
|
||||
public override bool Contains(string name)
|
||||
{
|
||||
if (injectedResources.ContainsKey(name))
|
||||
return true;
|
||||
|
||||
String cPath = GetCombinedPath(name);
|
||||
bool exists = File.Exists(cPath) || Directory.Exists(cPath);
|
||||
String cPath = PathHelper.FindPath(searchPaths);
|
||||
|
||||
if (!exists && cache.ContainsKey(name))
|
||||
if ((cPath == null) && cache.ContainsKey(name))
|
||||
cache.Remove(name);
|
||||
|
||||
return exists;
|
||||
return (cPath != null);
|
||||
}
|
||||
|
||||
public override void AddResource(Resource resource)
|
||||
|
@ -85,11 +98,16 @@ namespace ln.http.resources
|
|||
if (cache.ContainsKey(name))
|
||||
return cache[name];
|
||||
|
||||
String cPath = GetCombinedPath(name);
|
||||
String cPath = PathHelper.FindPath(searchPaths.Select((p) => System.IO.Path.Combine(p,name)));
|
||||
if (cPath == null)
|
||||
return null;
|
||||
|
||||
if (Directory.Exists(cPath))
|
||||
{
|
||||
return new DirectoryResource(this, cPath);
|
||||
DirectoryResource child = new DirectoryResource(this, searchPaths.Select((p)=> System.IO.Path.Combine(p,name)));
|
||||
child.ResourceTypeHook = ResourceTypeHook;
|
||||
cache[name] = child;
|
||||
return child;
|
||||
}
|
||||
else if (File.Exists(cPath))
|
||||
{
|
||||
|
@ -104,6 +122,7 @@ namespace ln.http.resources
|
|||
{
|
||||
resource = new FileResource(this, fileInfo);
|
||||
}
|
||||
cache[name] = resource;
|
||||
return resource;
|
||||
}
|
||||
|
||||
|
@ -112,7 +131,7 @@ namespace ln.http.resources
|
|||
|
||||
public override IEnumerable<Resource> GetResources()
|
||||
{
|
||||
return DirectoryInfo.EnumerateFileSystemInfos().Select((x) => GetResource(x.Name));
|
||||
return searchPaths.SelectMany((p) => new DirectoryInfo(p).EnumerateFileSystemInfos().Select((x) => GetResource(x.Name))).Distinct();
|
||||
}
|
||||
|
||||
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
||||
|
@ -167,7 +186,6 @@ namespace ln.http.resources
|
|||
{
|
||||
|
||||
DirectoryResource DirectoryResource { get; }
|
||||
public override DirectoryInfo DirectoryInfo => DirectoryResource.DirectoryInfo;
|
||||
|
||||
public FileInfo FileInfo { get; }
|
||||
|
||||
|
|
Loading…
Reference in New Issue