Commits

Vivekananda Tadala committed d9b738d

updated few functions

  • Participants
  • Parent commits 4e37bc1

Comments (0)

Files changed (1)

File storages/backends/redisimagestore.py

 from django.core.exceptions import ImproperlyConfigured
 import string
 import random
-import StringIO
+from StringIO import StringIO
+from django.core.files.base import File
 
 try:
     import redis
 
 class RedisStorage( Storage ):
     _connection = None
-    def __init__( self, redis_settings = None ):
+    def __init__( self, location = None , base_url = None, redis_settings = None):
         if not self._connection:
             if not redis_settings:
                 redis_settings = settings.REDIS_STORAGE_OPTIONS
             self._connection = redis.StrictRedis( redis_settings.get( "HOST", "localhost" ), redis_settings.get( "PORT", 6379 ), redis_settings.get( "DB", 0 ) )
-
+        
+        if base_url is None:
+            base_url = settings.MEDIA_URL
+        self.base_url = base_url
+    
     def _open( self, name, mode = 'rb' ):
         '''
         open the file from redis
         @param mode: unused parameter
         '''
         content = self._connection.get( name )
-        return StringIO( content )
+        self._size = len(content)
+        temp_file = File (StringIO( content ), name)
+        return temp_file
 
     def _save( self, name, file ):
         '''
         '''
         file.seek( 0 )
         content = file.read()
+        self._size = len(content)
         self._connection.set( name, content )
         return name
 
-    def read( self, name ):
-        '''
-        returns the content of the file
-        @param name:
-        '''
-        return self._connection.get( name )
+#    def read( self, name ):
+#        '''
+#        returns the content of the file
+#        @param name:
+#        '''
+#        self._open(name)
+#        content = self.file.getvalue()
+#        self._size = len(content)
+#        return content
 
     def delete( self, name ):
         '''
         check if the given key already exists and returns new name based on the given key
         @param name:
         '''
-        if self._connection.exists( name ):
+        name = self.get_valid_name(name)
+        
+        while self._connection.exists( name ):
             chars = string.ascii_uppercase + string.digits + string.lowercase
             size = 6
             rand_str = ''.join( random.choice( chars ) for x in range( size ) )
-            return "%s_%s" % ( rand_str, name )
+            (prefix,extension) = name.rsplit(".",1)
+            name = "%s_%s.%s" % ( prefix, rand_str , extension )
+
+        return name
+    
+    def get_valid_name(self, name):
+        '''
+        Removes all unwanted characters from the
+        @param name:
+        '''
+#        allowed_chars = string.ascii_uppercase + string.digits + string.lowercase + "/"
+        name = name.lstrip("./")
+        return super( RedisStorage, self ).get_valid_name( name )
+        
+        
+    def exists(self,name):
+        '''
+        Checks if a file exists in redis or not
+        @param name:
+        '''
+        return self._connection.exists(name)
+
+    def size(self, name):
+        """
+        Returns the total size, in bytes, of the file specified by name.
+        """
+        import pdb; pdb.set_trace()
+        temp_file = self.open(name, "r")
+        if temp_file:
+            return temp_file.size
         else:
-            return name
-
-    def save( self, name, file ):
-        '''
-        saves the content of the file in redis
-        @param name:
-        @param file:
-        '''
-        return self._save( name, file )
+            return 0
+        
+#    Not required to overwrite
+#    def save( self, name, file ):
+#        '''
+#        Signature is different, need to check 
+#        saves the content of the file in redis
+#        @param name:
+#        @param file:
+#        '''
+#        name = self.get_available_name(name)
+#        return self._save( name, file )
 
     def url( self, name ):
         '''
         using MEDIA_URL generates the url for the saved file
         @param name:
         '''
-        return settings.MEDIA_URL + name
+        return self.base_url + name