/
CustomBootstrapper.cs
99 lines (88 loc) · 3.03 KB
/
CustomBootstrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// CustomBootstrapper.cs
// This file is part of XG - XDCC Grabscher
// http://www.larsformella.de/lang/en/portfolio/programme-software/xg
//
// Author:
// Lars Formella <ich@larsformella.de>
//
// Copyright (c) 2012 Lars Formella
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using CacheAspect;
using CacheAspect.Attributes;
using Nancy;
using Nancy.Conventions;
using Nancy.Responses;
namespace XG.Plugin.Webserver.Nancy
{
public class CustomBootstrapper : DefaultNancyBootstrapper
{
private byte[] favicon;
protected override byte[] FavIcon
{
get { return this.favicon?? (this.favicon= LoadFavIcon()); }
}
private byte[] LoadFavIcon()
{
using (var resourceStream = GetType().Assembly.GetManifestResourceStream("XG.Plugin.Webserver.Resources.favicon.ico"))
{
var tempFavicon = new byte[resourceStream.Length];
resourceStream.Read(tempFavicon, 0, (int)resourceStream.Length);
return tempFavicon;
}
}
private string[] resourceNames;
private bool ResourceExists(string resourceName)
{
if (resourceNames == null)
{
resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
}
return resourceNames.Contains(resourceName);
}
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
base.ConfigureConventions(nancyConventions);
#if DEBUG && __MonoCS__
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("fonts", "fonts"));
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Resources", "Resources"));
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Scripts", "Scripts"));
#else
nancyConventions.StaticContentsConventions.Add((ctx, rootPath) => GetResource (ctx.Request.Url.Path));
#endif
}
[Cache.Cacheable]
private EmbeddedFileResponse GetResource(string aPath)
{
try
{
var directoryName = "XG.Plugin.Webserver" + Path.GetDirectoryName(aPath).Replace(Path.DirectorySeparatorChar, '.').Replace("-", "_");
var fileName = Path.GetFileName(aPath);
if (ResourceExists(directoryName + "." + fileName))
{
return new EmbeddedFileResponse(GetType().Assembly, directoryName, fileName);
}
}
catch(Exception) {}
return null;
}
}
}