On 08/21/2014 11:43 AM, lvroyce0210(a)gmail.com wrote:
From: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
Add model function to upload file
Signed-off-by: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
---
src/kimchi/model/storagevolumes.py | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
index a7823c2..0b9d96a 100644
--- a/src/kimchi/model/storagevolumes.py
+++ b/src/kimchi/model/storagevolumes.py
@@ -54,6 +54,29 @@ class StorageVolumesModel(object):
if create_func and p in params:
return create_func(pool_name, params)
+ def _create_volume_with_file(self, pool_name, params):
+ pool = StoragePoolModel.get_storagepool(pool_name,
self.conn)
I'd suggest to get the pool info right before using it.
+ dir_path = StoragePoolModel(conn=self.conn,
+ objstore=self.objstore).lookup(pool_name)['path']
+ upload_file = params['file']
+ file_name = params['resumableFilename']
+ size = 0
+ allData=''
+ while True:
+ data = upload_file.file.read(8192)
+ allData += data
+ if not data:
+ break
+ size += len(data)
+
+ file_path = os.path.join(dir_path, file_name)
+ f = open(file_path, 'wb')
+ f.write(allData)
+ f.close()
This file write code can raise some exceptions, like if the disk is full
or something like that
It would be good to use try/except to handle those possible problems.
+ # Refresh to make sure volume can be found in following
lookup
+ pool.refresh()
+ return file_name
+
def _create_volume_with_capacity(self, pool_name, params):
vol_xml = """
<volume>