Added avatar manager which will help when transfering and caching avatars. Using boost::filesystem. MD5 sum is used to identify avatars, different types of files are possible. Avatar cache (and the avatars provided) use for example <md5sum>.png as file names, so that no additional storage for the md5sum is required.
A cache directory with write permissions is required, however, as avatars are not cached in RAM.
This commit is contained in:
@@ -45,6 +45,7 @@ HEADERS += \
|
|||||||
src/config/configfile.h \
|
src/config/configfile.h \
|
||||||
src/core/thread.h \
|
src/core/thread.h \
|
||||||
src/core/crypthelper.h \
|
src/core/crypthelper.h \
|
||||||
|
src/core/avatarmanager.h \
|
||||||
src/engine/boardinterface.h \
|
src/engine/boardinterface.h \
|
||||||
src/engine/enginefactory.h \
|
src/engine/enginefactory.h \
|
||||||
src/engine/handinterface.h \
|
src/engine/handinterface.h \
|
||||||
@@ -106,6 +107,7 @@ SOURCES += \
|
|||||||
src/gui/guiinterface.cpp \
|
src/gui/guiinterface.cpp \
|
||||||
src/core/common/thread.cpp \
|
src/core/common/thread.cpp \
|
||||||
src/core/common/crypthelper.cpp \
|
src/core/common/crypthelper.cpp \
|
||||||
|
src/core/avatarmanager.cpp \
|
||||||
src/core/tinyxml/tinystr.cpp \
|
src/core/tinyxml/tinystr.cpp \
|
||||||
src/core/tinyxml/tinyxml.cpp \
|
src/core/tinyxml/tinyxml.cpp \
|
||||||
src/core/tinyxml/tinyxmlerror.cpp \
|
src/core/tinyxml/tinyxmlerror.cpp \
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* 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. *
|
||||||
|
***************************************************************************/
|
||||||
|
/* A manager for avatar files, MD5sums and a cache. */
|
||||||
|
|
||||||
|
#ifndef _AVATARMANAGER_H_
|
||||||
|
#define _AVATARMANAGER_H_
|
||||||
|
|
||||||
|
#include <core/crypthelper.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class AvatarManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
AvatarManager();
|
||||||
|
~AvatarManager();
|
||||||
|
|
||||||
|
bool Init(const std::string &dataDir, const std::string &cacheDir);
|
||||||
|
|
||||||
|
bool GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf);
|
||||||
|
bool GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) const;
|
||||||
|
bool StoreAvatarInCache(const MD5Buf &md5buf, const std::string &fileExtension, const unsigned char *data, unsigned size);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
typedef std::map<MD5Buf, std::string> AvatarMap;
|
||||||
|
|
||||||
|
void InternalReadDirectory(const std::string &dir);
|
||||||
|
|
||||||
|
private:
|
||||||
|
AvatarMap m_avatars;
|
||||||
|
|
||||||
|
std::string m_cacheDir;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* 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"
|
||||||
|
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
#include <openssl/md5.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost::filesystem;
|
||||||
|
|
||||||
|
|
||||||
|
AvatarManager::AvatarManager()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
AvatarManager::~AvatarManager()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
AvatarManager::Init(const std::string &dataDir, const std::string &cacheDir)
|
||||||
|
{
|
||||||
|
bool retVal = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
InternalReadDirectory(dataDir);
|
||||||
|
InternalReadDirectory(cacheDir);
|
||||||
|
m_cacheDir = cacheDir;
|
||||||
|
retVal = true;
|
||||||
|
} catch (...)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
AvatarManager::GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf)
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
|
if (exists(fileName))
|
||||||
|
{
|
||||||
|
AvatarMap::const_iterator i = m_avatars.begin();
|
||||||
|
AvatarMap::const_iterator end = m_avatars.end();
|
||||||
|
while (i != end)
|
||||||
|
{
|
||||||
|
if (i->second == fileName)
|
||||||
|
{
|
||||||
|
md5buf = i->first;
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
if (CryptHelper::MD5Sum(fileName, md5buf))
|
||||||
|
{
|
||||||
|
m_avatars.insert(AvatarMap::value_type(md5buf, fileName));
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
AvatarManager::GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) const
|
||||||
|
{
|
||||||
|
bool retVal = false;
|
||||||
|
AvatarMap::const_iterator pos = m_avatars.find(md5buf);
|
||||||
|
if (pos != m_avatars.end())
|
||||||
|
{
|
||||||
|
fileName = pos->second;
|
||||||
|
retVal = true;
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
AvatarManager::StoreAvatarInCache(const MD5Buf &md5buf, const std::string &fileExtension, const unsigned char *data, unsigned size)
|
||||||
|
{
|
||||||
|
bool retVal = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
path tmpPath(m_cacheDir);
|
||||||
|
tmpPath /= (md5buf.ToString() + "." + fileExtension);
|
||||||
|
string fileName(tmpPath.file_string());
|
||||||
|
ofstream o(fileName.c_str());
|
||||||
|
o.write((const char *)data, size);
|
||||||
|
m_avatars.insert(AvatarMap::value_type(md5buf, fileName));
|
||||||
|
retVal = true;
|
||||||
|
} catch (...)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
AvatarManager::InternalReadDirectory(const std::string &dir)
|
||||||
|
{
|
||||||
|
directory_iterator i(dir);
|
||||||
|
directory_iterator end;
|
||||||
|
|
||||||
|
while (i != end)
|
||||||
|
{
|
||||||
|
if (is_regular(i->status()))
|
||||||
|
{
|
||||||
|
string md5sum(basename(i->path()));
|
||||||
|
MD5Buf tmpBuf;
|
||||||
|
if (tmpBuf.FromString(md5sum))
|
||||||
|
{
|
||||||
|
m_avatars.insert(AvatarMap::value_type(tmpBuf, i->path().file_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -89,11 +89,16 @@ MD5Buf::FromString(const std::string &text)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
MD5Buf::operator==(const MD5Buf &other)
|
MD5Buf::operator==(const MD5Buf &other) const
|
||||||
{
|
{
|
||||||
return memcmp(data, other.data, MD5_DATA_SIZE) == 0;
|
return memcmp(data, other.data, MD5_DATA_SIZE) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
MD5Buf::operator<(const MD5Buf &other) const
|
||||||
|
{
|
||||||
|
return memcmp(data, other.data, MD5_DATA_SIZE) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ struct MD5Buf
|
|||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
bool FromString(const std::string &text);
|
bool FromString(const std::string &text);
|
||||||
|
|
||||||
bool operator==(const MD5Buf &other);
|
bool operator==(const MD5Buf &other) const;
|
||||||
|
bool operator<(const MD5Buf &other) const;
|
||||||
|
|
||||||
unsigned char data[MD5_DATA_SIZE];
|
unsigned char data[MD5_DATA_SIZE];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user