Skip to content

Instantly share code, notes, and snippets.

@choudeshell
Created March 16, 2013 01:28
Show Gist options
  • Save choudeshell/5174495 to your computer and use it in GitHub Desktop.
Save choudeshell/5174495 to your computer and use it in GitHub Desktop.
Embedded Resource Provider for Katana
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;
}
}
}
}
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;
}
}
}
app.UseStaticFiles(options =>
{
options.WithFileSystem(new ResourceFileSystemProvider());
});
@Tratcher
Copy link

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