Commit d3aec190 authored by Yingjie Xu's avatar Yingjie Xu

Generic upload function, client should take care of the content.

parent acf6c303
...@@ -191,6 +191,68 @@ class NetworkcacheClient(object): ...@@ -191,6 +191,68 @@ class NetworkcacheClient(object):
(self.shacache_host, result.status, data)) (self.shacache_host, result.status, data))
return True return True
def upload_generic(self, file_descriptor, key=None, **kw):
''' Upload the file to the server.
If key is None, it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR.
'''
sha512sum = hashlib.sha512()
file_descriptor.seek(0)
while True:
d = file_descriptor.read(sha512sum.block_size)
if not d:
break
sha512sum.update(d)
sha512sum = sha512sum.hexdigest()
file_descriptor.seek(0)
if self.shacache_scheme == 'https':
shacache_connection = httplib.HTTPSConnection(self.shacache_host,
self.shacache_port, key_file = self.shacache_key_file,
cert_file = self.shacache_cert_file)
else:
shacache_connection = httplib.HTTPConnection(self.shacache_host,
self.shacache_port)
try:
shacache_connection.request('POST', self.shacache_path, file_descriptor,
self.shacache_header_dict)
result = shacache_connection.getresponse()
data = result.read()
finally:
shacache_connection.close()
if result.status != 201 or data != sha512sum:
raise UploadError('Failed to upload the file to SHACACHE Server.' \
'URL: %s. Response code: %s. Response data: %s' % \
(self.shacache_host, result.status, data))
if key is not None:
sha_entry = json.dumps(kw)
try:
signature = self._getSignatureString(sha_entry)
except Exception:
raise UploadError('Impossible to sign content, error:\n%s' %
traceback.format_exc())
data = [sha_entry, signature]
if self.shadir_scheme == 'https':
shadir_connection = httplib.HTTPSConnection(self.shadir_host,
self.shadir_port, key_file = self.shadir_key_file,
cert_file = self.shadir_cert_file)
else:
shadir_connection = httplib.HTTPConnection(self.shadir_host,
self.shadir_port)
try:
shadir_connection.request('PUT', '/'.join([self.shadir_path, key]),
json.dumps(data), self.shadir_header_dict)
result = shadir_connection.getresponse()
data = result.read()
finally:
shadir_connection.close()
if result.status != 201:
raise UploadError('Failed to upload data to SHADIR Server.' \
'URL: %s. Response code: %s. Response data: %s' % \
(self.shadir_host, result.status, data))
return True
def download(self, sha512sum): def download(self, sha512sum):
''' Download the file. ''' Download the file.
It uses http GET request method. It uses http GET request method.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment