1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.app.files;
7
8 import diamond.core.apptype;
9
10 static if (isWeb)
11 {
12 import vibe.d : HTTPServerRequestDelegateS;
13
14 import diamond.http;
15
16 /**
17 * The handler for static file requests.
18 * Params:
19 * client = The client.
20 * staticFile = The static file handler.
21 */
22 package(diamond.app) void handleStaticFiles
23 (
24 HttpClient client,
25 HTTPServerRequestDelegateS staticFile
26 )
27 {
28 import diamond.authentication;
29
30 if (hasRoles && !hasAccess(client.role, client.method, client.route.toString()))
31 {
32 client.error(HttpStatus.unauthorized);
33 }
34
35 import diamond.extensions;
36 mixin ExtensionEmit!(ExtensionType.staticFileExtension, q{
37 if (!{{extensionEntry}}.handleStaticFile(client))
38 {
39 return;
40 }
41 });
42 emitExtension();
43
44 import diamond.core.webconfig;
45
46 foreach (headerKey,headerValue; webConfig.defaultHeaders.staticFiles)
47 {
48 client.rawResponse.headers[headerKey] = headerValue;
49 }
50
51 import std.array : split, join;
52
53 version (VIBE_D_OLD)
54 {
55 client.path = "/" ~ client.path.split("/")[2 .. $].join("/");
56 }
57 else
58 {
59 client.path = "/" ~ client.path.split("/")[1 .. $].join("/");
60 }
61
62 import diamond.core.websettings;
63
64 if (webSettings)
65 {
66 webSettings.onStaticFile(client);
67 }
68
69 staticFile(client.rawRequest, client.rawResponse);
70 }
71 }