]>
code.delx.au - pymsnt/blob - src/avatar.py
1 # Copyright 2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
6 from twisted
.internet
import reactor
7 from tlib
.xmlw
import Element
8 from debug
import LogEvent
, INFO
, WARN
, ERROR
19 def parsePhotoEl(photo
):
20 """ Pass the photo element as an avatar, returns the avatar imageData """
23 for e
in photo
.elements():
24 if(e
.name
== "BINVAL"):
25 imageData
= base64
.decodestring(e
.__str
__())
26 elif(e
.name
== "TYPE"):
27 imageType
= e
.__str
__()
29 if(imageType
!= "image/png"):
30 imageData
= utils
.convertToPNG(imageData
)
37 """ Represents an Avatar. Does not store the image in memory. """
38 def __init__(self
, imageData
, avatarCache
):
39 self
.__imageHash
= sha
.sha(imageData
).hexdigest()
40 self
.__avatarCache
= avatarCache
42 def getImageHash(self
):
43 """ Returns the SHA1 hash of the avatar. """
44 return self
.__imageHash
46 def getImageData(self
):
47 """ Returns this Avatar's imageData. This loads data from a file. """
48 return self
.__avatarCache
.getAvatarData(self
.__imageHash
)
50 def makePhotoElement(self
):
51 """ Returns an XML Element that can be put into the vCard. """
52 photo
= Element((None, "PHOTO"))
53 type = photo
.addElement("TYPE")
54 type.addContent("image/png")
55 binval
= photo
.addElement("BINVAL")
56 binval
.addContent(base64
.encodestring(self
.getImageData()))
59 def makeDataElement(self
):
60 """ Returns an XML Element that can be put into a jabber:x:avatar IQ stanza. """
61 data
= Element((None, "data"))
62 data
["mimetype"] = "image/png"
63 data
.addContent(base64
.encodestring(self
.getImageData()))
66 def __eq__(self
, other
):
67 return (other
and self
.__imageHash
== other
.__imageHash
)
71 """ Manages avatars on disk. Avatars are stored according to their SHA1 hash.
72 The layout is config.spooldir / config.jid / avatars / "first two characters of SHA1 hash" """
75 """ Returns the full path to the directory that a
76 particular key is in. Creates that directory if it doesn't already exist. """
77 d
= os
.path
.abspath(config
.spooldir
) + "/" + config
.jid
+ "/avatars/" + key
[0:3] + "/"
78 if not os
.path
.exists(d
):
82 def setAvatar(self
, imageData
):
83 """ Writes an avatar to disk according to its key.
84 Returns an Avatar object. """
85 avatar
= Avatar(imageData
, self
)
86 key
= avatar
.getImageHash()
87 LogEvent(INFO
, "", "Setting avatar %s" % (key
))
88 prev_umask
= os
.umask(SPOOL_UMASK
)
90 f
= open(self
.dir(key
) + key
, 'w')
94 LogEvent(WARN
, "", "IOError writing to avatar %s - %s" % (key
, str(e
)))
98 def getAvatar(self
, key
):
99 """ Loads the avatar with SHA1 hash of 'key' from disk and returns an Avatar object """
100 imageData
= self
.getAvatarData(key
)
102 return Avatar(imageData
, self
)
104 def getAvatarData(self
, key
):
105 """ Loads the avatar with SHA1 hash of 'key' from disk and returns the data """
107 filename
= self
.dir(key
) + key
108 if os
.path
.isfile(filename
):
109 LogEvent(INFO
, "Getting avatar.")
115 LogEvent(INFO
, "", "Avatar not found.")
117 LogEvent(WARN
, "", "IOError reading avatar.")