ln.http/ln.http/FileRouter.cs

56 lines
1.5 KiB
C#

// /**
// * File: FileSystemRouter.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System.IO;
using ln.http.mime;
using ln.http.route;
using ln.mime;
namespace ln.http
{
public class FileRouter : HttpRoute
{
public string FileName { get; }
public string ContentType { get; }
public FileRouter(string filename)
:this(null, filename)
{
}
public FileRouter(string filename, MimeTypes mimeTypes)
:this(null, filename)
{
ContentType = mimeTypes.GetMimeTypeByExtension(Path.GetExtension(FileName)).ToString();
}
public FileRouter(string filename, string contentType)
:base(HttpMethod.GET, null)
{
ContentType = contentType;
if (!File.Exists(filename))
throw new FileNotFoundException();
FileName = filename;
_routerDelegate = RouteToFile;
}
public bool RouteToFile(HttpRequestContext requestContext, string routePath)
{
requestContext.Response =
HttpResponse
.OK()
.Content(new FileStream(FileName, FileMode.Open, FileAccess.Read))
.ContentType(ContentType);
return true;
}
}
}