Created
March 16, 2013 01:28
-
-
Save choudeshell/5174495 to your computer and use it in GitHub Desktop.
Embedded Resource Provider for Katana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Owin.FileSystems; | |
using System; | |
using System.Reflection; | |
namespace Metrical.Providers | |
{ | |
public class ResourceFileInfo : IFileInfo | |
{ | |
private string _filePath; | |
private Assembly _assembly; | |
public ResourceFileInfo(string file) | |
{ | |
_filePath = file; | |
_assembly = Assembly.GetExecutingAssembly(); | |
} | |
public System.IO.Stream CreateReadStream() | |
{ | |
return _assembly.GetManifestResourceStream(_filePath); | |
} | |
public bool IsDirectory | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
public DateTime LastModified | |
{ | |
get | |
{ | |
return DateTime.Now; | |
} | |
} | |
public long Length | |
{ | |
get | |
{ | |
using (var stream = CreateReadStream()) | |
{ | |
return stream.Length; | |
} | |
} | |
} | |
public string Name | |
{ | |
get | |
{ | |
return _filePath; | |
} | |
} | |
public string PhysicalPath | |
{ | |
get | |
{ | |
return _filePath; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Microsoft.Owin.FileSystems; | |
namespace Metrical.Providers | |
{ | |
public class ResourceFileSystemProvider : IFileSystem | |
{ | |
public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents) | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo) | |
{ | |
var flatPath = subpath.Replace('/', '.'); | |
var assembly = Assembly.GetExecutingAssembly(); | |
var assemblyName = assembly.GetName().Name; | |
var path = String.Format("{0}{1}", assemblyName, flatPath); | |
var resourceNames = assembly.GetManifestResourceNames(); | |
if (resourceNames.Contains(path)) | |
{ | |
fileInfo = new ResourceFileInfo(path); | |
return true; | |
} | |
fileInfo = null; | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.UseStaticFiles(options => | |
{ | |
options.WithFileSystem(new ResourceFileSystemProvider()); | |
}); |
We're pulling StaticFiles off the shelf now, want to revisit this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good idea. I'll add a constructor for ResourceFileSystemProvider that takes in either a string array of assembly names or an array of type assembly. I would rather have them explicitly defined than to pull all assemblies in a certain path.
In the final version I only enumerate the files in an assembly once then I cache the results.
Heh. Why didn't I think of that.
Where is the best place to pull-request documentation? I don't mind contributing.