2007-09-08 16:26:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 by Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "avatarmanager.h"
|
2007-10-25 20:26:44 +00:00
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
#include <core/loghelper.h>
|
2007-09-08 16:26:10 +00:00
|
|
|
|
2007-09-08 20:28:58 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2007-10-07 15:52:12 +00:00
|
|
|
#include <boost/lambda/lambda.hpp>
|
2007-10-25 20:26:44 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2007-09-08 16:26:10 +00:00
|
|
|
#include <openssl/md5.h>
|
|
|
|
|
|
2007-09-08 20:28:58 +00:00
|
|
|
#include <fstream>
|
2007-10-06 12:50:19 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
2007-10-25 21:20:35 +00:00
|
|
|
#ifdef POKERTH_DEDICATED_SERVER
|
|
|
|
|
#define MAX_NUMBER_OF_FILES 1024
|
|
|
|
|
#define MAX_AVATAR_CACHE_AGE 2592000 // 1 Month
|
|
|
|
|
#else
|
|
|
|
|
#define MAX_NUMBER_OF_FILES 256
|
|
|
|
|
#define MAX_AVATAR_CACHE_AGE 2592000
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-18 23:00:55 +00:00
|
|
|
#define PNG_HEADER "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
|
|
|
|
|
#define PNG_HEADER_SIZE (sizeof(PNG_HEADER) - 1)
|
|
|
|
|
#define JPG_HEADER "\xff\xd8"
|
|
|
|
|
#define JPG_HEADER_SIZE (sizeof(JPG_HEADER) - 1)
|
|
|
|
|
#define GIF_HEADER_1 "GIF87a"
|
|
|
|
|
#define GIF_HEADER_2 "GIF89a"
|
|
|
|
|
#define GIF_HEADER_SIZE (sizeof(GIF_HEADER_1) - 1)
|
|
|
|
|
#define MAX_HEADER_SIZE PNG_HEADER_SIZE
|
|
|
|
|
|
2007-09-08 20:28:58 +00:00
|
|
|
|
2007-09-08 16:26:10 +00:00
|
|
|
using namespace std;
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
|
2007-10-05 14:23:36 +00:00
|
|
|
struct AvatarFileState
|
|
|
|
|
{
|
|
|
|
|
ifstream inputStream;
|
|
|
|
|
};
|
2007-09-08 16:26:10 +00:00
|
|
|
|
|
|
|
|
AvatarManager::AvatarManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AvatarManager::~AvatarManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
AvatarManager::Init(const std::string &dataDir, const std::string &cacheDir)
|
|
|
|
|
{
|
2007-10-10 22:21:24 +00:00
|
|
|
bool retVal = true;
|
|
|
|
|
bool tmpRet;
|
2007-10-24 22:20:35 +00:00
|
|
|
path tmpCachePath(cacheDir);
|
|
|
|
|
path tmpDataPath(dataDir);
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_cacheDirMutex);
|
2007-10-24 22:20:35 +00:00
|
|
|
m_cacheDir = tmpCachePath.directory_string();
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
2007-10-07 15:52:12 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_avatarsMutex);
|
2007-10-24 22:20:35 +00:00
|
|
|
tmpRet = InternalReadDirectory((tmpDataPath / "gfx/avatars/default/people/").directory_string(), m_avatars);
|
2007-10-10 22:21:24 +00:00
|
|
|
retVal = retVal && tmpRet;
|
2007-10-24 22:20:35 +00:00
|
|
|
tmpRet = InternalReadDirectory((tmpDataPath / "gfx/avatars/default/misc/").directory_string(), m_avatars);
|
2007-10-10 22:21:24 +00:00
|
|
|
retVal = retVal && tmpRet;
|
2007-10-07 15:52:12 +00:00
|
|
|
}
|
2007-10-24 22:20:35 +00:00
|
|
|
if (cacheDir.empty() || tmpCachePath.empty())
|
|
|
|
|
LOG_ERROR("Cache directory was not set!");
|
|
|
|
|
else
|
2007-10-07 15:52:12 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
2007-10-24 22:20:35 +00:00
|
|
|
tmpRet = InternalReadDirectory(tmpCachePath.directory_string(), m_cachedAvatars);
|
2007-10-10 22:21:24 +00:00
|
|
|
retVal = retVal && tmpRet;
|
2007-10-07 15:52:12 +00:00
|
|
|
}
|
2007-10-24 22:20:35 +00:00
|
|
|
|
2007-10-10 22:21:24 +00:00
|
|
|
return retVal;
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-05 14:23:36 +00:00
|
|
|
boost::shared_ptr<AvatarFileState>
|
2007-10-06 12:50:19 +00:00
|
|
|
AvatarManager::OpenAvatarFileForChunkRead(const std::string &fileName, unsigned &outFileSize, AvatarFileType &outFileType)
|
2007-10-05 14:23:36 +00:00
|
|
|
{
|
|
|
|
|
outFileSize = 0;
|
2007-10-06 12:50:19 +00:00
|
|
|
outFileType = AVATAR_FILE_TYPE_UNKNOWN;
|
2007-10-05 14:23:36 +00:00
|
|
|
boost::shared_ptr<AvatarFileState> retVal;
|
|
|
|
|
try
|
|
|
|
|
{
|
2007-10-06 12:50:19 +00:00
|
|
|
path filePath(fileName);
|
|
|
|
|
string ext(extension(filePath));
|
2007-10-25 20:35:57 +00:00
|
|
|
if (boost::algorithm::iequals(ext, ".png"))
|
2007-10-06 12:50:19 +00:00
|
|
|
outFileType = AVATAR_FILE_TYPE_PNG;
|
2007-10-25 20:35:57 +00:00
|
|
|
else if (boost::algorithm::iequals(ext, ".jpg") || boost::algorithm::iequals(ext, ".jpeg"))
|
2007-10-06 12:50:19 +00:00
|
|
|
outFileType = AVATAR_FILE_TYPE_JPG;
|
2007-10-25 20:35:57 +00:00
|
|
|
else if (boost::algorithm::iequals(ext, ".gif"))
|
2007-10-06 12:50:19 +00:00
|
|
|
outFileType = AVATAR_FILE_TYPE_GIF;
|
2007-10-05 14:23:36 +00:00
|
|
|
boost::shared_ptr<AvatarFileState> fileState(new AvatarFileState);
|
|
|
|
|
fileState->inputStream.open(fileName.c_str(), ios_base::in | ios_base::binary);
|
|
|
|
|
if (!fileState->inputStream.fail())
|
|
|
|
|
{
|
|
|
|
|
// Find out file size.
|
|
|
|
|
// Not fully portable, but works on win/linux/mac.
|
|
|
|
|
fileState->inputStream.seekg(0, ios_base::beg);
|
|
|
|
|
std::streampos startPos = fileState->inputStream.tellg();
|
|
|
|
|
fileState->inputStream.seekg(0, ios_base::end);
|
|
|
|
|
std::streampos endPos = fileState->inputStream.tellg();
|
|
|
|
|
fileState->inputStream.seekg(0, ios_base::beg);
|
|
|
|
|
std::streamoff posDiff(endPos - startPos);
|
|
|
|
|
outFileSize = (unsigned)posDiff;
|
2007-10-18 23:00:55 +00:00
|
|
|
if (outFileSize >= MIN_AVATAR_FILE_SIZE && outFileSize <= MAX_AVATAR_FILE_SIZE)
|
|
|
|
|
{
|
|
|
|
|
// Validate type of file by verifying image header.
|
|
|
|
|
unsigned char fileHeader[MAX_HEADER_SIZE];
|
|
|
|
|
fileState->inputStream.read((char *)fileHeader, sizeof(fileHeader));
|
|
|
|
|
fileState->inputStream.seekg(0, ios_base::beg);
|
|
|
|
|
|
|
|
|
|
if (IsValidAvatarFileType(outFileType, fileHeader, sizeof(fileHeader)))
|
|
|
|
|
retVal = fileState;
|
|
|
|
|
}
|
2007-10-05 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
} catch (...)
|
|
|
|
|
{
|
2007-10-24 22:20:35 +00:00
|
|
|
LOG_ERROR("Exception caught when trying to open avatar.");
|
2007-10-05 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
|
AvatarManager::ChunkReadAvatarFile(boost::shared_ptr<AvatarFileState> fileState, unsigned char *data, unsigned chunkSize)
|
|
|
|
|
{
|
|
|
|
|
unsigned retVal = 0;
|
|
|
|
|
if (fileState.get())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!fileState->inputStream.fail() && !fileState->inputStream.eof())
|
|
|
|
|
{
|
|
|
|
|
fileState->inputStream.read((char *)data, chunkSize);
|
|
|
|
|
retVal = fileState->inputStream.gcount();
|
|
|
|
|
}
|
|
|
|
|
} catch (...)
|
|
|
|
|
{
|
2007-10-24 22:20:35 +00:00
|
|
|
LOG_ERROR("Exception caught when trying to read avatar.");
|
2007-10-05 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-10 22:21:24 +00:00
|
|
|
int
|
2007-10-06 12:50:19 +00:00
|
|
|
AvatarManager::AvatarFileToNetPackets(const string &fileName, unsigned requestId, NetPacketList &packets)
|
2007-10-05 14:23:36 +00:00
|
|
|
{
|
2007-10-31 20:23:27 +00:00
|
|
|
// Serialize avatar access for now.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_loadMutex);
|
|
|
|
|
|
2007-10-10 22:21:24 +00:00
|
|
|
int retVal = ERR_NET_INVALID_AVATAR_FILE;
|
2007-10-05 14:23:36 +00:00
|
|
|
unsigned fileSize;
|
2007-10-06 12:50:19 +00:00
|
|
|
AvatarFileType fileType;
|
|
|
|
|
boost::shared_ptr<AvatarFileState> tmpState = OpenAvatarFileForChunkRead(fileName, fileSize, fileType);
|
|
|
|
|
if (tmpState.get() && fileSize && fileType != AVATAR_FILE_TYPE_UNKNOWN)
|
2007-10-05 14:23:36 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> avatarHeader(new NetPacketAvatarHeader);
|
|
|
|
|
NetPacketAvatarHeader::Data avatarHeaderData;
|
|
|
|
|
avatarHeaderData.requestId = requestId;
|
|
|
|
|
avatarHeaderData.avatarFileSize = fileSize;
|
2007-10-06 12:50:19 +00:00
|
|
|
avatarHeaderData.avatarFileType = fileType;
|
2007-10-05 14:23:36 +00:00
|
|
|
static_cast<NetPacketAvatarHeader *>(avatarHeader.get())->SetData(avatarHeaderData);
|
2007-10-06 12:50:19 +00:00
|
|
|
packets.push_back(avatarHeader);
|
2007-10-05 14:23:36 +00:00
|
|
|
|
|
|
|
|
unsigned numBytes = 0;
|
|
|
|
|
unsigned totalBytesRead = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacketAvatarFile> avatarFile(new NetPacketAvatarFile);
|
|
|
|
|
NetPacketAvatarFile::Data avatarFileData;
|
|
|
|
|
avatarFileData.fileData.resize(MAX_FILE_DATA_SIZE);
|
|
|
|
|
numBytes = ChunkReadAvatarFile(tmpState, &avatarFileData.fileData[0], MAX_FILE_DATA_SIZE);
|
|
|
|
|
if (numBytes)
|
|
|
|
|
{
|
|
|
|
|
avatarFileData.fileData.resize(numBytes);
|
|
|
|
|
avatarFileData.requestId = requestId;
|
|
|
|
|
totalBytesRead += numBytes;
|
|
|
|
|
static_cast<NetPacketAvatarFile *>(avatarFile.get())->SetData(avatarFileData);
|
2007-10-06 12:50:19 +00:00
|
|
|
packets.push_back(avatarFile);
|
2007-10-05 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
} while (numBytes);
|
2007-10-10 22:21:24 +00:00
|
|
|
|
|
|
|
|
if (fileSize != totalBytesRead)
|
|
|
|
|
retVal = ERR_NET_WRONG_AVATAR_SIZE;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> avatarEnd(new NetPacketAvatarEnd);
|
|
|
|
|
NetPacketAvatarEnd::Data avatarEndData;
|
|
|
|
|
avatarEndData.requestId = requestId;
|
|
|
|
|
static_cast<NetPacketAvatarEnd *>(avatarEnd.get())->SetData(avatarEndData);
|
|
|
|
|
packets.push_back(avatarEnd);
|
|
|
|
|
retVal = 0;
|
|
|
|
|
}
|
2007-10-05 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-08 16:26:10 +00:00
|
|
|
bool
|
2007-10-07 15:52:12 +00:00
|
|
|
AvatarManager::GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) const
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
if (exists(fileName))
|
|
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
// Scan default avatars first.
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_avatarsMutex);
|
|
|
|
|
AvatarMap::const_iterator i = m_avatars.begin();
|
|
|
|
|
AvatarMap::const_iterator end = m_avatars.end();
|
|
|
|
|
while (i != end)
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
if (i->second == fileName)
|
|
|
|
|
{
|
|
|
|
|
md5buf = i->first;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-10-07 15:52:12 +00:00
|
|
|
// Check cached avatars next.
|
|
|
|
|
if (!found)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
|
|
|
|
AvatarMap::const_iterator i = m_cachedAvatars.begin();
|
|
|
|
|
AvatarMap::const_iterator end = m_cachedAvatars.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if (i->second == fileName)
|
|
|
|
|
{
|
|
|
|
|
md5buf = i->first;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate md5 sum if not found.
|
2007-09-08 16:26:10 +00:00
|
|
|
if (!found)
|
|
|
|
|
{
|
|
|
|
|
if (CryptHelper::MD5Sum(fileName, md5buf))
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
AvatarManager::GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) const
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_avatarsMutex);
|
|
|
|
|
AvatarMap::const_iterator pos = m_avatars.find(md5buf);
|
|
|
|
|
if (pos != m_avatars.end())
|
|
|
|
|
{
|
|
|
|
|
fileName = pos->second;
|
|
|
|
|
retVal = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!retVal)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
|
|
|
|
AvatarMap::const_iterator pos = m_cachedAvatars.find(md5buf);
|
|
|
|
|
if (pos != m_cachedAvatars.end())
|
|
|
|
|
{
|
|
|
|
|
fileName = pos->second;
|
|
|
|
|
retVal = true;
|
|
|
|
|
}
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-07 15:52:12 +00:00
|
|
|
bool
|
|
|
|
|
AvatarManager::HasAvatar(const MD5Buf &md5buf) const
|
|
|
|
|
{
|
|
|
|
|
string tmpFile;
|
|
|
|
|
return GetAvatarFileName(md5buf, tmpFile);
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-08 16:26:10 +00:00
|
|
|
bool
|
2007-10-06 12:50:19 +00:00
|
|
|
AvatarManager::StoreAvatarInCache(const MD5Buf &md5buf, AvatarFileType avatarFileType, const unsigned char *data, unsigned size)
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
2007-10-24 22:20:35 +00:00
|
|
|
string cacheDir;
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cacheDirMutex);
|
|
|
|
|
cacheDir = m_cacheDir;
|
|
|
|
|
}
|
2007-09-08 16:26:10 +00:00
|
|
|
try
|
|
|
|
|
{
|
2007-10-06 12:50:19 +00:00
|
|
|
string ext;
|
|
|
|
|
switch (avatarFileType)
|
|
|
|
|
{
|
|
|
|
|
case AVATAR_FILE_TYPE_PNG:
|
|
|
|
|
ext = ".png";
|
|
|
|
|
break;
|
|
|
|
|
case AVATAR_FILE_TYPE_JPG:
|
|
|
|
|
ext = ".jpg";
|
|
|
|
|
break;
|
|
|
|
|
case AVATAR_FILE_TYPE_GIF:
|
|
|
|
|
ext = ".gif";
|
|
|
|
|
break;
|
2007-10-16 20:48:25 +00:00
|
|
|
case AVATAR_FILE_TYPE_UNKNOWN:
|
|
|
|
|
break;
|
2007-10-06 12:50:19 +00:00
|
|
|
}
|
2007-10-24 22:20:35 +00:00
|
|
|
if (!ext.empty() && !cacheDir.empty())
|
2007-10-07 15:52:12 +00:00
|
|
|
{
|
2007-10-18 23:00:55 +00:00
|
|
|
// Check header before storing file.
|
|
|
|
|
if (IsValidAvatarFileType(avatarFileType, data, size))
|
2007-10-16 20:48:25 +00:00
|
|
|
{
|
2007-10-24 22:20:35 +00:00
|
|
|
path tmpPath(cacheDir);
|
2007-10-18 23:00:55 +00:00
|
|
|
tmpPath /= (md5buf.ToString() + ext);
|
|
|
|
|
string fileName(tmpPath.file_string());
|
|
|
|
|
ofstream o(fileName.c_str(), ios_base::out | ios_base::binary);
|
2007-10-24 22:20:35 +00:00
|
|
|
if (!o.fail())
|
2007-10-18 23:00:55 +00:00
|
|
|
{
|
2007-10-24 22:20:35 +00:00
|
|
|
o.write((const char *)data, size);
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
|
|
|
|
m_cachedAvatars.insert(AvatarMap::value_type(md5buf, fileName));
|
|
|
|
|
}
|
|
|
|
|
retVal = true;
|
2007-10-18 23:00:55 +00:00
|
|
|
}
|
2007-10-16 20:48:25 +00:00
|
|
|
}
|
2007-10-07 15:52:12 +00:00
|
|
|
}
|
2007-09-08 16:26:10 +00:00
|
|
|
} catch (...)
|
|
|
|
|
{
|
2007-10-24 22:20:35 +00:00
|
|
|
LOG_ERROR("Exception caught when trying to store avatar.");
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-18 23:00:55 +00:00
|
|
|
bool
|
|
|
|
|
AvatarManager::IsValidAvatarFileType(AvatarFileType avatarFileType, const unsigned char *fileHeader, unsigned fileHeaderSize)
|
|
|
|
|
{
|
|
|
|
|
bool validType = false;
|
|
|
|
|
|
|
|
|
|
switch (avatarFileType)
|
|
|
|
|
{
|
|
|
|
|
case AVATAR_FILE_TYPE_PNG:
|
|
|
|
|
if (fileHeaderSize >= PNG_HEADER_SIZE
|
|
|
|
|
&& memcmp(fileHeader, PNG_HEADER, PNG_HEADER_SIZE) == 0)
|
|
|
|
|
{
|
|
|
|
|
validType = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AVATAR_FILE_TYPE_JPG:
|
|
|
|
|
if (fileHeaderSize >= JPG_HEADER_SIZE
|
|
|
|
|
&& memcmp(fileHeader, JPG_HEADER, JPG_HEADER_SIZE) == 0)
|
|
|
|
|
{
|
|
|
|
|
validType = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AVATAR_FILE_TYPE_GIF:
|
|
|
|
|
if (fileHeaderSize >= GIF_HEADER_SIZE
|
|
|
|
|
&& (memcmp(fileHeader, GIF_HEADER_1, GIF_HEADER_SIZE) == 0
|
|
|
|
|
|| memcmp(fileHeader, GIF_HEADER_2, GIF_HEADER_SIZE) == 0))
|
|
|
|
|
{
|
|
|
|
|
validType = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AVATAR_FILE_TYPE_UNKNOWN:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return validType;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-24 22:20:35 +00:00
|
|
|
void
|
|
|
|
|
AvatarManager::RemoveOldAvatarCacheEntries()
|
|
|
|
|
{
|
|
|
|
|
string cacheDir;
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cacheDirMutex);
|
|
|
|
|
cacheDir = m_cacheDir;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
path cachePath(cacheDir);
|
|
|
|
|
cacheDir = cachePath.directory_string();
|
|
|
|
|
// Never delete anything if we do not have a special cache dir set.
|
|
|
|
|
if (!cacheDir.empty())
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
|
|
|
|
|
|
|
|
|
// First pass: Remove files which no longer exist.
|
|
|
|
|
// Count files and record age.
|
|
|
|
|
AvatarList removeList;
|
|
|
|
|
TimeAvatarMap timeMap;
|
|
|
|
|
unsigned fileCount = 0;
|
|
|
|
|
{
|
|
|
|
|
AvatarMap::const_iterator i = m_cachedAvatars.begin();
|
|
|
|
|
AvatarMap::const_iterator end = m_cachedAvatars.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
bool keepFile = false;
|
|
|
|
|
path filePath(i->second);
|
|
|
|
|
string fileString(filePath.file_string());
|
|
|
|
|
// Only consider files which are definitely in the cache dir.
|
|
|
|
|
if (fileString.size() > cacheDir.size() && fileString.substr(0, cacheDir.size()) == cacheDir)
|
|
|
|
|
{
|
|
|
|
|
// Only consider files with MD5 as file name.
|
|
|
|
|
MD5Buf tmpBuf;
|
|
|
|
|
if (exists(filePath) && tmpBuf.FromString(basename(filePath)))
|
|
|
|
|
{
|
|
|
|
|
++fileCount;
|
|
|
|
|
timeMap.insert(TimeAvatarMap::value_type(last_write_time(filePath), i->first));
|
|
|
|
|
keepFile = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!keepFile)
|
|
|
|
|
removeList.push_back(i->first);
|
|
|
|
|
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
AvatarList::const_iterator i = removeList.begin();
|
|
|
|
|
AvatarList::const_iterator end = removeList.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
m_cachedAvatars.erase(*i);
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
removeList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove and physically delete files in one of the
|
|
|
|
|
// following cases:
|
|
|
|
|
// 1. More than MAX_NUMBER_OF_FILES files are present
|
|
|
|
|
// - delete until only MAX_NUMBER_OF_FILES/2 are left.
|
|
|
|
|
// 2. Files are older than 30 days.
|
|
|
|
|
|
|
|
|
|
if (m_cachedAvatars.size() > MAX_NUMBER_OF_FILES)
|
|
|
|
|
{
|
|
|
|
|
while (!timeMap.empty() && m_cachedAvatars.size() > MAX_NUMBER_OF_FILES / 2)
|
|
|
|
|
{
|
|
|
|
|
TimeAvatarMap::iterator i = timeMap.begin();
|
|
|
|
|
AvatarMap::iterator pos = m_cachedAvatars.find(i->second);
|
|
|
|
|
if (pos != m_cachedAvatars.end())
|
|
|
|
|
{
|
|
|
|
|
path tmpPath(pos->second);
|
|
|
|
|
remove(tmpPath);
|
|
|
|
|
m_cachedAvatars.erase(pos);
|
|
|
|
|
}
|
|
|
|
|
timeMap.erase(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get reference time.
|
|
|
|
|
time_t curTime = time(NULL);
|
|
|
|
|
while (!timeMap.empty() && !m_cachedAvatars.empty())
|
|
|
|
|
{
|
|
|
|
|
TimeAvatarMap::iterator i = timeMap.begin();
|
|
|
|
|
if (curTime - i->first < MAX_AVATAR_CACHE_AGE)
|
|
|
|
|
break;
|
|
|
|
|
AvatarMap::iterator pos = m_cachedAvatars.find(i->second);
|
|
|
|
|
if (pos != m_cachedAvatars.end())
|
|
|
|
|
{
|
|
|
|
|
path tmpPath(pos->second);
|
|
|
|
|
remove(tmpPath);
|
|
|
|
|
m_cachedAvatars.erase(pos);
|
|
|
|
|
}
|
|
|
|
|
timeMap.erase(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (...)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR("Exception caught while cleaning up cache.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-07 15:52:12 +00:00
|
|
|
bool
|
|
|
|
|
AvatarManager::InternalReadDirectory(const std::string &dir, AvatarMap &avatars)
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
bool retVal = true;
|
|
|
|
|
try
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
// This method is not thread safe. Only call after locking the map.
|
|
|
|
|
directory_iterator i(dir);
|
|
|
|
|
directory_iterator end;
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
if (is_regular(i->status()))
|
2007-09-08 16:26:10 +00:00
|
|
|
{
|
2007-10-07 15:52:12 +00:00
|
|
|
string md5sum(basename(i->path()));
|
|
|
|
|
MD5Buf md5buf;
|
|
|
|
|
string fileName(i->path().file_string());
|
2007-10-13 14:23:04 +00:00
|
|
|
if (md5buf.FromString(md5sum))
|
2007-10-07 15:52:12 +00:00
|
|
|
{
|
2007-10-13 14:23:04 +00:00
|
|
|
// Only consider files with md5sum as name.
|
2007-10-07 15:52:12 +00:00
|
|
|
avatars.insert(AvatarMap::value_type(md5buf, fileName));
|
2007-10-13 14:23:04 +00:00
|
|
|
}
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
2007-10-07 15:52:12 +00:00
|
|
|
++i;
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
2007-10-07 15:52:12 +00:00
|
|
|
} catch (...)
|
|
|
|
|
{
|
2007-10-24 23:17:12 +00:00
|
|
|
LOG_ERROR("Exception caught when trying to scan avatar directory.");
|
2007-10-07 15:52:12 +00:00
|
|
|
retVal = false;
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
2007-10-07 15:52:12 +00:00
|
|
|
return retVal;
|
2007-09-08 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
|