-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbeddedResourcesVirtualPathProvider.cs
64 lines (56 loc) · 1.68 KB
/
EmbeddedResourcesVirtualPathProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
namespace InfoCaster.Umbraco.TextFieldPreviewable
{
public class EmbeddedResourcesVirtualPathProvider : VirtualPathProvider
{
public EmbeddedResourcesVirtualPathProvider()
{
}
private bool IsEmbeddedResourcePath(string virtualPath)
{
var checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/Umbraco/TextFieldPreviewable/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return IsEmbeddedResourcePath(virtualPath) || base.FileExists(virtualPath);
}
public override VirtualFile GetFile(string virtualPath)
{
if (IsEmbeddedResourcePath(virtualPath))
return new EmbeddedResourceVirtualFile(virtualPath);
return base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsEmbeddedResourcePath(virtualPath))
return null;
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
public class EmbeddedResourceVirtualFile : VirtualFile
{
string _path;
public EmbeddedResourceVirtualFile(string virtualPath)
: base(virtualPath)
{
_path = VirtualPathUtility.ToAppRelative(virtualPath);
}
public override Stream Open()
{
var resourceName = _path.Split('/').Last();
var assembly = GetType().Assembly;
if (assembly != null)
return assembly.GetManifestResourceStream(resourceName);
return null;
}
}
}