1 /*****************************************************************************
2  * PokerTH - The open source texas holdem engine                             *
3  * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May          *
4  *                                                                           *
5  * This program is free software: you can redistribute it and/or modify      *
6  * it under the terms of the GNU Affero General Public License as            *
7  * published by the Free Software Foundation, either version 3 of the        *
8  * License, or (at your option) any later version.                           *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU Affero General Public License for more details.                       *
14  *                                                                           *
15  * You should have received a copy of the GNU Affero General Public License  *
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *                                                                           *
18  *                                                                           *
19  * Additional permission under GNU AGPL version 3 section 7                  *
20  *                                                                           *
21  * If you modify this program, or any covered work, by linking or            *
22  * combining it with the OpenSSL project's OpenSSL library (or a             *
23  * modified version of that library), containing parts covered by the        *
24  * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH           *
25  * (Felix Hammer, Florian Thauer, Lothar May) grant you additional           *
26  * permission to convey the resulting work.                                  *
27  * Corresponding Source for a non-source form of such a combination          *
28  * shall include the source code for the parts of OpenSSL used as well       *
29  * as that of the covered work.                                              *
30  *****************************************************************************/
31 /* A manager for avatar files, MD5sums and a cache. */
32 
33 #ifndef _AVATARMANAGER_H_
34 #define _AVATARMANAGER_H_
35 
36 #include <boost/thread.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 #include <map>
40 #include <list>
41 
42 #include <playerdata.h>
43 #include <core/crypthelper.h>
44 #include <net/netpacket.h>
45 
46 #define MIN_AVATAR_FILE_SIZE	32
47 #define MAX_AVATAR_FILE_SIZE	30720
48 
49 struct AvatarFileState;
50 class UploaderThread;
51 
52 class AvatarManager
53 {
54 public:
55 
56 	AvatarManager(bool useExternalServer = false, const std::string &externalServerAddress = "",
57 				  const std::string &externalServerUser = "", const std::string &externalServerPassword = "");
58 	~AvatarManager();
59 
60 	bool Init(const std::string &dataDir, const std::string &cacheDir);
61 
62 	bool AddSingleAvatar(const std::string &fileName);
63 
64 	static boost::shared_ptr<AvatarFileState> OpenAvatarFileForChunkRead(const std::string &fileName, unsigned &outFileSize, AvatarFileType &outFileType);
65 	static unsigned ChunkReadAvatarFile(boost::shared_ptr<AvatarFileState> fileState, unsigned char *data, unsigned chunkSize);
66 
67 	static int AvatarFileToNetPackets(const std::string &fileName, unsigned requestId, NetPacketList &packets);
68 	static AvatarFileType GetAvatarFileType(const std::string &fileName);
69 	static std::string GetAvatarFileExtension(AvatarFileType fileType);
70 
71 	bool GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) const;
72 	bool GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) const;
73 	bool HasAvatar(const MD5Buf &md5buf) const;
74 	bool StoreAvatarInCache(const MD5Buf &md5buf, AvatarFileType avatarFileType, const unsigned char *data, size_t size, bool upload);
75 
76 	static bool IsValidAvatarFileType(AvatarFileType avatarFileType, const unsigned char *fileHeader, size_t fileHeaderSize);
77 
78 	void RemoveOldAvatarCacheEntries();
79 
80 protected:
81 	typedef std::map<MD5Buf, std::string> AvatarMap;
82 	typedef std::list<MD5Buf> AvatarList;
83 	typedef std::map<std::time_t, MD5Buf> TimeAvatarMap;
84 
85 	bool InternalReadDirectory(const std::string &dir, AvatarMap &avatars);
86 
87 private:
88 	mutable boost::mutex	m_avatarsMutex;
89 	AvatarMap				m_avatars;
90 
91 	mutable boost::mutex	m_cachedAvatarsMutex;
92 	AvatarMap				m_cachedAvatars;
93 
94 	mutable boost::mutex	m_cacheDirMutex;
95 	std::string				m_cacheDir;
96 
97 	const bool				m_useExternalServer;
98 	const std::string		m_externalServerAddress;
99 	const std::string		m_externalServerUser;
100 	const std::string		m_externalServerPassword;
101 
102 	boost::shared_ptr<UploaderThread> m_uploader;
103 };
104 
105 #endif
106