GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Virtual file system Virtual file system
Demis Bellot edited this page
This page has moved to docs.servicestack.net/virtual-file-systemIn order to access physical files in view engines from multiple sources, ServiceStack includes its own pluggable virtual file system API that lets it support multiple filesystem backends. The virtual file system (VFS) is what allows ServiceStack to support view engines in a standard ASP.NET websites (e.g. serving directories from the root directory) as well in self-hosting stand-alone HttpListener websites and Windows Services serving from the output To enable ServiceStack to serve embedded resources in your Website's compiled SetConfig(new HostConfig { EmbeddedResourceSources = { typeof(TypeInDllWithEmbeddedResources).Assembly }, EmbeddedResourceBaseTypes = { typeof(TypeInDllWithEmbeddedResources) } }); By default ServiceStack automatically includes the Assembly where your See the ServiceStack.Gap project for different examples of how to create single .exe ILMerged applications with Embedded Resources and Compiled Razor Views. An easy way to register additional VirtualPath Providers in ServiceStack is to override the public override List<IVirtualPathProvider> GetVirtualFileSources() { var existingProviders = base.GetVirtualFileSources(); var memFs = new InMemoryVirtualPathProvider(this); //Get FileSystem Provider var fs = existingProviders.First(x => x is FileSystemVirtualPathProvider); //Process all .html files: foreach (var file in fs.GetAllMatchingFiles("*.html")) { var contents = Minifiers.HtmlAdvanced.Compress(file.ReadAllText()); memFs.WriteFile(file.VirtualPath, contents); } //Process all .css files: foreach (var file in fs.GetAllMatchingFiles("*.css") .Where(file => !file.VirtualPath.EndsWith(".min.css"))) { var contents = Minifiers.Css.Compress(file.ReadAllText()); memFs.WriteFile(file.VirtualPath, contents); } //Process all .js files foreach (var file in fs.GetAllMatchingFiles("*.js") .Where(file => !file.VirtualPath.EndsWith(".min.js"))) { try { var js = file.ReadAllText(); var contents = Minifiers.JavaScript.Compress(js); memFs.WriteFile(file.VirtualPath, contents); } catch (Exception ex) { //Report any errors in StartUpErrors collection on ?debug=requestinfo base.OnStartupException(new Exception( "JSMin Error in {0}: {1}".Fmt(file.VirtualPath, ex.Message))); } } //Give new Memory FS highest priority existingProviders.Insert(0, memFs); return existingProviders; } You can also globally replace the VFS used by setting it in your AppHost, e.g. If you only want to use an InMemory File System: base.VirtualPathProvider = new InMemoryVirtualPathProvider(this); Fine-grained control on which VFS to use can also be specified on any Plugins requiring access to the FileSystem like ServiceStack's built-in HTML ViewEngines, here's how you could override the VFS used in ServiceStack's Razors support: Plugins.Add(new RazorFormat { VirtualPathProvider = new InMemoryVirtualPathProvider(this) }); You can change the physical root path from where ServiceStack serves your files from by changing SetConfig(new HostConfig { WebHostPhysicalPath = "~/".MapProjectPath() });
The VFS supports multiple file source locations where you can override embedded files by including your own custom files in the same location as the embedded files. We can see how this works by overriding the built-in templates used in metadata pages: The HTML templates for the metadata pages are maintained as embedded html template resources. The VFS lets you replace built-in ServiceStack templates with your own by simply copying the metadata or HtmlFormat Template files you want to customize and placing them in your Website Directory at: Which you can customize locally that ServiceStack will pick up and use instead. The Virtual File System extended public interface IVirtualFiles : IVirtualPathProvider { void WriteFile(string filePath, string textContents); void WriteFile(string filePath, Stream stream); void WriteFiles(IEnumerable<IVirtualFile> files,Func<IVirtualFile,string> toPath=null); void AppendFile(string filePath, string textContents); void AppendFile(string filePath, Stream stream); void DeleteFile(string filePath); void DeleteFiles(IEnumerable<string> filePaths); void DeleteFolder(string dirPath); }
The new
All As typically when saving uploaded files you'd only want files written to a single explicit File Storage provider,
ServiceStack keeps a distinction between the existing read-only Virtual File Sources it uses internally whenever a
static file is requested and the new public class IAppHost { // Read/Write Virtual FileSystem. Defaults to Local FileSystem. IVirtualFiles VirtualFiles { get; set; } // Cascading file sources, inc. Embedded Resources, File System, In Memory, S3. IVirtualPathProvider VirtualFileSources { get; set; } } public class Service : IService //ServiceStack's convenient concrete base class { //... public IVirtualFiles VirtualFiles { get; set; } public IVirtualPathProvider VirtualFileSources { get; } } Internally ServiceStack only uses
The VFS is designed to be implementation agnostic so can be changed to use any file repository, e.g. it could easily be made to support a Redis, RDBMS, embedded Sqlite or other NoSQL back-ends. Like most of ServiceStack's substitutable API's, the interfaces for the VFS lives in the ServiceStack.Interfaces.dll under the ServiceStack.IO namespace. These are the API's that are needed to be implemented in order to create a new VFS: public interface IVirtualPathProvider { IVirtualDirectory RootDirectory { get; } string VirtualPathSeparator { get; } string RealPathSeparator { get; } string CombineVirtualPath(string basePath, string relativePath); bool FileExists(string virtualPath); bool DirectoryExists(string virtualPath); IVirtualFile GetFile(string virtualPath); string GetFileHash(string virtualPath); string GetFileHash(IVirtualFile virtualFile); IVirtualDirectory GetDirectory(string virtualPath); IEnumerable<IVirtualFile> GetAllMatchingFiles( string globPattern, int maxDepth = Int32.MaxValue); bool IsSharedFile(IVirtualFile virtualFile); bool IsViewFile(IVirtualFile virtualFile); } public interface IVirtualNode { IVirtualDirectory Directory { get; } string Name { get; } string VirtualPath { get; } string RealPath { get; } bool IsDirectory { get; } DateTime LastModified { get; } } public interface IVirtualFile : IVirtualNode { IVirtualPathProvider VirtualPathProvider { get; } string Extension { get; } string GetFileHash(); Stream OpenRead(); StreamReader OpenText(); string ReadAllText(); } public interface IVirtualDirectory : IVirtualNode, IEnumerable<IVirtualNode> { bool IsRoot { get; } IVirtualDirectory ParentDirectory { get; } IEnumerable<IVirtualFile> Files { get; } IEnumerable<IVirtualDirectory> Directories { get; } IVirtualFile GetFile(string virtualPath); IVirtualFile GetFile(Stack<string> virtualPath); IVirtualDirectory GetDirectory(string virtualPath); IVirtualDirectory GetDirectory(Stack<string> virtualPath); IEnumerable<IVirtualFile> GetAllMatchingFiles( string globPattern, int maxDepth = Int32.MaxValue); }
Clone this wiki locally |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |