[storage] missing qrefresh in previous patch applied: fix comment, error message, and use a storage specified encoding, not cubicweb's encoding stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 12 Apr 2010 15:28:26 +0200
branchstable
changeset 5219 35d44017c72b
parent 5218 aebd00a2d316
child 5220 42f854b6083d
[storage] missing qrefresh in previous patch applied: fix comment, error message, and use a storage specified encoding, not cubicweb's encoding
server/sources/storages.py
--- a/server/sources/storages.py	Mon Apr 12 15:21:08 2010 +0200
+++ b/server/sources/storages.py	Mon Apr 12 15:28:26 2010 +0200
@@ -74,8 +74,9 @@
 
 class BytesFileSystemStorage(Storage):
     """store Bytes attribute value on the file system"""
-    def __init__(self, defaultdir):
+    def __init__(self, defaultdir, fsencoding='utf-8'):
         self.default_directory = defaultdir
+        self.fsencoding = fsencoding
 
     def callback(self, source, value):
         """sql generator callback when some attribute with a custom storage is
@@ -120,19 +121,19 @@
         DeleteFileOp(entity._cw, filepath=self.current_fs_path(entity, attr))
 
     def new_fs_path(self, entity, attr):
+        # We try to get some hint about how to name the file using attribute's
+        # name metadata, so we use the real file name and extension when
+        # available. Keeping the extension is useful for example in the case of
+        # PIL processing that use filename extension to detect content-type, as
+        # well as providing more understandable file names on the fs.
         basename = [str(entity.eid), attr]
-        # We try to get some hint about how to name the file using attributes
-        # metadata. Using the real file name and extension when available.
-        #
-        # Keeping the extension might be usefull for exemple in the case of PIL
-        # processing that use filename extension to detect content-type.
         name = entity.attr_metadata(attr, 'name')
         if name is not None:
-            basename.append(name.encode(entity._cw.encoding))
+            basename.append(name.encode(self.fsencoding))
         fspath = uniquify_path(self.default_directory, '_'.join(basename))
         if fspath is None:
             msg = entity._cw._('failed to uniquify path (%s, %s)') % (
-                dirpath, basename)
+                dirpath, '_'.join(basename))
             raise ValidationError(entity.eid, {role_name(attr, 'subject'): msg})
         return fspath