aboutsummaryrefslogtreecommitdiff
path: root/src/content/static/Static.cpp
blob: 132f0a0b9f6e0aca1f837da247f1d2f2d4ed3124 (plain)
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
#include "Static.hpp"

Static::Static(std::string uriBasePath, std::shared_ptr<Filesystem> const & fs)
    : Content(), m_UriBasePath(uriBasePath), m_MainTemplatePath(""), m_StaticFiles(fs)
{
    for (auto const & file : fs->GetFiles())
    {
        m_Redirections.push_back(uriBasePath + "/" + file.first);
    }
}

bool Static::Init()
{
    std::cout << "Static files: " << m_StaticFiles->GetFiles().size() << std::endl;

    return true;
}

void Static::Shutdown()
{
    std::cout << "Static files module shutdown" << std::endl;
}

bool Static::Render(RequestResponse & rr, RenderData & rd, std::string & out)
{
    (void)rd;

    if (rr.GetUriPath() == m_UriBasePath)
    {
        return false;
    }

    rr.UseOutputHeader();
    auto & files = m_StaticFiles->GetFiles();
    auto const & path = std::string(rr.GetUriPath()).substr(m_UriBasePath.length() + 1, std::string::npos);

    if (rr.AddOutputHeader("Content-Type", files[path].mime) == false)
    {
        return false;
    }

    out = std::string(files[path].data.begin(), files[path].data.end());

    return true;
}

std::string const & Static::GetUriBasePath() const
{
    return m_UriBasePath;
}

std::string const & Static::GetMainTemplate() const
{
    return m_MainTemplatePath;
}

Redirections const & Static::GetRedirections() const
{
    return m_Redirections;
}