-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhoxFileMgr.cpp
More file actions
158 lines (138 loc) · 4.07 KB
/
Copy pathhoxFileMgr.cpp
File metadata and controls
158 lines (138 loc) · 4.07 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// C++ Implementation: hoxFileMgr
//
// Description:
//
//
// Author: Huy Phan <hphan@hphan-hp>, (C) 2008-2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "hoxFileMgr.h"
#include "hoxDbClient.h"
#include "hoxExcept.h"
#include "hoxLog.h"
// =========================================================================
//
// hoxFileMgr
//
// =========================================================================
/* Define the static singleton instance. */
hoxFileMgr* hoxFileMgr::s_instance = NULL;
bool hoxFileMgr::m_bCacheEnabled = false;
bool hoxFileMgr::m_bErrorIfNotInCache = false;
/*static*/
hoxFileMgr*
hoxFileMgr::getInstance()
{
if ( hoxFileMgr::s_instance == NULL )
{
hoxFileMgr::s_instance = new hoxFileMgr();
}
return hoxFileMgr::s_instance;
}
void
hoxFileMgr::preloadFile( const std::string& sFile )
{
const char* FNAME = "hoxFileMgr::preloadFile";
hoxLog(LOG_DEBUG, "%s: ENTER [%s].", FNAME, sFile.c_str());
try
{
const std::string sFullPath = _sLocation + sFile;
const hoxFile_SPtr pFile = _loadFile( sFullPath );
_files[sFile] = pFile;
}
catch( hoxError ex )
{
hoxLog(LOG_WARN, "%s: Error caught [%d %s].", FNAME, ex.code(), ex.toString().c_str());
}
}
hoxFile_SPtr
hoxFileMgr::getFile( const std::string& sPath )
{
const char* FNAME = "hoxFileMgr::getFile";
hoxFile_SPtr pFile;
/* Lookup the file's content in the Cache first.
* If found, then simply return the "cached".
*/
if ( hoxFileMgr::m_bCacheEnabled )
{
pFile = _files[sPath];
if ( pFile )
{
hoxLog(LOG_DEBUG, "%s: Found in cache file [%s].", FNAME, sPath.c_str());
return pFile;
}
if ( hoxFileMgr::m_bErrorIfNotInCache )
{
pFile.reset( new hoxFile( sPath ) );
pFile->m_sContent = "File not found";
pFile->m_sType = "text/html";
hoxLog(LOG_WARN, "%s: File [%s] not in cache.", FNAME, sPath.c_str());
return pFile;
}
}
/* If not found, then load the file from disk. */
try
{
pFile = _loadFile( sPath );
if ( hoxFileMgr::m_bCacheEnabled )
{
_files[sPath] = pFile;
}
}
catch( hoxError ex )
{
pFile->m_sContent = "Error caught :" + ex.toString();
pFile->m_sType = "text/html";
hoxLog(LOG_WARN, "%s: Error caught [%d %s].", FNAME, ex.code(), ex.toString().c_str());
pFile.reset(); // Signal "error".
}
return pFile;
}
void
hoxFileMgr::clearCache()
{
const char* FNAME = "hoxFileMgr::clearCache";
hoxLog(LOG_INFO, "%s: ENTER. Cache-size = [%d]", FNAME, _files.size());
_files.clear();
}
hoxFile_SPtr
hoxFileMgr::_loadFile( const std::string& sFile )
{
const char* FNAME = "hoxFileMgr::_loadFile";
hoxFile_SPtr pFile( new hoxFile( sFile ) );
try
{
std::string sFileContent;
if ( hoxRC_OK != hoxDbClient::get_http_file( sFile, sFileContent ) )
{
throw hoxError(hoxRC_NOT_FOUND, "Failed to get HTTP file: " + sFile);
}
pFile->m_sContent = sFileContent;
pFile->m_sType = _getHttpContentType( sFile );
}
catch( hoxError ex )
{
hoxLog(LOG_WARN, "%s: Error caught [%d %s].", FNAME, ex.code(), ex.toString().c_str());
pFile.reset(); // Signal "error".
throw;
}
return pFile;
}
const std::string
hoxFileMgr::_getHttpContentType( const std::string& sPath ) const
{
std::string sExtension; // The file's extension.
std::string::size_type loc = sPath.find_last_of( '.' );
if ( loc != std::string::npos )
{
sExtension = sPath.substr( loc+1 );
}
if ( sExtension == "js" ) return "application/javascript";
else if ( sExtension == "png" ) return "image/png";
else if ( sExtension == "css" ) return "text/css";
/* else */ return "text/html";
}
/******************* END OF FILE *********************************************/