summaryrefslogtreecommitdiff
path: root/addons/document/document_storage.py
diff options
context:
space:
mode:
authorHarry (Open ERP) <hmo@tinyerp.com>2010-03-16 18:43:28 +0530
committerHarry (Open ERP) <hmo@tinyerp.com>2010-03-16 18:43:28 +0530
commit0db347d65f9c67f4b114a96874ea120cf7eacdee (patch)
tree14b476a2f5cfca2255c1a289b40a64ce35531dac /addons/document/document_storage.py
parent4b08bc496fe4945b25580d004b7f8745d00f7b15 (diff)
parentb509b2d0867f0283e7876c6ee636f7e18f48f11e (diff)
[MERGE] merged from lp:~openerp-commiter/openobject-addons/crm-improvement
bzr revid: hmo@tinyerp.com-20100316131328-lgttnpy712rqr7ga
Diffstat (limited to 'addons/document/document_storage.py')
-rw-r--r--addons/document/document_storage.py52
1 files changed, 49 insertions, 3 deletions
diff --git a/addons/document/document_storage.py b/addons/document/document_storage.py
index 2b50e7a27c9..0fd85e0be6d 100644
--- a/addons/document/document_storage.py
+++ b/addons/document/document_storage.py
@@ -154,7 +154,6 @@ class document_storage(osv.osv):
netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "ir.attachment #%d does not have a filename, but is at filestore, fix it!" % ira.id)
return None
fpath = os.path.join(boo.path, ira.store_fname)
- print "Trying to read \"%s\".." % fpath
return file(fpath, 'rb').read()
elif boo.type == 'db':
# TODO: we need a better api for large files
@@ -164,8 +163,19 @@ class document_storage(osv.osv):
out = ''
return out
elif boo.type == 'realstore':
- # fpath = os.path.join(boo.path,
- return None
+ if not ira.store_fname:
+ # On a migrated db, some files may have the wrong storage type
+ # try to fix their directory.
+ if ira.file_size:
+ netsvc.Logger().notifyChannel('document',netsvc.LOG_WARNING,"ir.attachment #%d does not have a filename, trying the name." %ira.id)
+ sfname = ira.name
+ fpath = os.path.join(boo.path,ira.store_fname or ira.name)
+ if os.path.exists(fpath):
+ return file(fpath,'rb').read()
+ elif not ira.store_fname:
+ return None
+ else:
+ raise IOError("File not found: %s" % fpath)
else:
raise TypeError("No %s storage" % boo.type)
@@ -219,6 +229,36 @@ class document_storage(osv.osv):
out = base64.encodestring(data)
cr.execute('UPDATE ir_attachment SET db_datas = %s WHERE id = %s',
(out, file_node.file_id))
+ elif boo.type == 'realstore':
+ try:
+ file_node.fix_ppath(cr, ira)
+ npath = file_node.full_path() or []
+ # npath may contain empty elements, for root directory etc.
+ for i, n in enumerate(npath):
+ if n == None:
+ del npath[i]
+ for n in npath:
+ for ch in ('*', '|', "\\", '/', ':', '"', '<', '>', '?', '..'):
+ if ch in n:
+ raise ValueError("Invalid char %s in path %s" %(ch, n))
+ dpath = [boo.path,]
+ dpath += npath[:-1]
+ path = os.path.join(*dpath)
+ if not os.path.isdir(path):
+ os.makedirs(path)
+ fname = os.path.join(path, npath[-1])
+ fp = file(fname,'wb')
+ fp.write(data)
+ fp.close()
+ logger.notifyChannel('document',netsvc.LOG_DEBUG,"Saved data to %s" % fname)
+ filesize = len(data) # os.stat(fname).st_size
+ store_fname = os.path.join(*npath)
+ # TODO Here, an old file would be left hanging.
+ except Exception,e :
+ import traceback
+ traceback.print_exc()
+ netsvc.Logger().notifyChannel('document',netsvc.LOG_WARNING,"Couldn't save data: %s" % e)
+ raise except_orm(_('Error!'), str(e))
else:
raise TypeError("No %s storage" % boo.type)
@@ -262,6 +302,12 @@ class document_storage(osv.osv):
return (storage_bo.id, 'file', os.path.join(path, fname))
elif storage_bo.type == 'db':
return None
+ elif storage_bo.type == 'realstore':
+ fname = fil_bo.store_fname
+ if not fname:
+ return None
+ path = storage_bo.path
+ return ( storage_bo.id, 'file', os.path.join(path,fname))
else:
raise TypeError("No %s storage" % boo.type)