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()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We're pulling StaticFiles off the shelf now, want to revisit this?