-
Notifications
You must be signed in to change notification settings - Fork 3
/
LocalDataSource.cpp
57 lines (47 loc) · 1.71 KB
/
LocalDataSource.cpp
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
#include <omega.h>
#include "LocalDataSource.h"
#include <Awesomium/STLHelpers.h>
#include <fstream>
using namespace omega;
// Needed to fix build on linux:
// http://answers.awesomium.com/questions/4205/linux-undefined-reference-to-typeinfo-for-awesomiu.html
#ifdef __linux__
Awesomium::DataSource::~DataSource()
{
}
#endif
///////////////////////////////////////////////////////////////////////////////
void LocalDataSource::OnRequest(int request_id, const Awesomium::ResourceRequest& request, const Awesomium::WebString &url)
{
DataManager* dm = SystemManager::instance()->getDataManager();
String path = ToString(url);
// Find extension to deduct mime type
String filename;
String ext;
String mime;
StringUtils::splitBaseFilename(path, filename, ext);
if(ext == "html" || ext == "htm") mime = "text/html";
else if(ext == "css") mime = "text/css";
else if(ext == "webm") mime = "video/webm";
else if(ext == "jpeg" || ext == "jpg") mime = "image/jpeg";
else if(ext == "gif") mime = "image/gif";
else if(ext == "png") mime = "image/png";
else if(ext == "js") mime = "application/javascript";
String fullpath;
if(DataManager::findFile(path, fullpath))
{
std::ifstream file(fullpath.c_str(), std::ios::binary);
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if(file.read(buffer.data(), size))
{
SendResponse(request_id, size, (unsigned char*)buffer.data(), Awesomium::WSLit(mime.c_str()));
}
}
else
{
ofwarn("LocalDataSource::OnRequest: file not found: %1%", %path);
}
}