Commits

ben  committed 60e86b9

添加图片md5

  • Participants
  • Parent commits 6a45275

Comments (0)

Files changed (1)

 import random
 import string
 import os
+import hashlib
 
 from google.appengine.ext import db
 from util.b36 import Base36
 from google.appengine.api import images
 from google.appengine.api import memcache
 
+def get_md5(bf):
+    md5 = hashlib.md5()
+    md5.update(bf)
+    return md5.hexdigest()
+
 class Album(db.Model):
     '''
     sa3album Album Model
     width = db.IntegerProperty()
     height =db.IntegerProperty()
     bf = db.BlobProperty()
+    md5 = db.StringProperty(indexed=True)
     
     view_count = db.IntegerProperty(default=0) #view count
     
     def put(self):
         if self.is_saved():
             memcache.delete("f::%s" % self.key().name())
+        else:  #calc image md5
+            self.md5 = get_md5(self.bf)
         super(Image,self).put()
-            
     
     def delete(self):
         memcache.delete("s::%s" % self.key().name())
         
     @classmethod
     def add(cls,album,name,mime,bf,**kwargs):
+        #先检查md5看图片是否已经上传过.
+        md5 = get_md5(bf)
+        img = Image.all().filter('md5 =',md5).get()
+        if img:
+            logging.info("%s exist" % md5)
+            return img
+        
         key_name = Counter.get_max('image')
         img = Image(album=album,name=name,mime=mime,bf=bf,key_name = key_name,**kwargs)
         img.size = len(img.bf)