1 /**
2  * Модуль контроллера раздающего файлы
3  *
4  * Copyright: (c) 2015-2020, Milofon Project.
5  * License: Subject to the terms of the BSD 3-Clause License, as written in the included LICENSE.md file.
6  * Author: <m.galanin@milofon.pro> Maksim Galanin
7  * Date: 2020-04-30
8  */
9 
10 module dango.web.controllers.fileshare;
11 
12 private
13 {
14     import std.algorithm.searching : endsWith;
15     import std.format : fmt = format;
16 
17     import vibe.core.file : existsFile;
18     import vibe.http.fileserver;
19 
20     import dango.system.exception : enforceConfig;
21     import dango.system.properties : getOrEnforce;
22     import dango.web.controller;
23 }
24 
25 
26 /**
27  * Класс контроллера позволяющий раздавать статику из директории
28  */
29 class FileShareWebController : WebController
30 {
31     private
32     {
33         string _directory;
34         string _prefix;
35     }
36 
37 
38     /**
39      * Main constructor
40      */
41     this(string directory, string prefix) @safe
42     {
43         this._directory = directory;
44         this._prefix = prefix;
45     }
46 
47     /**
48      * Регистрация цепочек маршрутов контроллера
49      */
50     void registerChains(RegisterChainCallback dg) @safe
51     {
52         auto fsettings = new HTTPFileServerSettings;
53         fsettings.serverPathPrefix = _prefix;
54         dg(HTTPMethod.GET, "*", new Chain(serveStaticFiles(_directory, fsettings)));
55     }
56 }
57 
58 
59 /**
60  * Класс фабрика контроллера позволяющий раздавать статику из директории
61  */
62 class FileShareWebControllerFactory : WebControllerFactory
63 {
64     WebController createComponent(DependencyContainer cont, UniConf config) @safe
65     {
66         string directory = config.getOrEnforce!string("directory",
67                 "Not defined 'directory' parameter");
68         enforceConfig(directory.existsFile, fmt!"Not exists directory '%s'"(directory));
69         string prefix = config.getOrElse("prefix", "");
70         return new FileShareWebController(directory, prefix);
71     }
72 }
73