Commit 56e45f59 authored by Lucas Carvalho's avatar Lucas Carvalho

implemented the first version of upload and download methods. Upload is using...

implemented the first version of upload and download methods. Upload is using both server already, shacache and shadir.
parent ef7a7da5
...@@ -13,56 +13,126 @@ ...@@ -13,56 +13,126 @@
############################################################################## ##############################################################################
import base64
import hashlib
import httplib import httplib
import json
import os import os
from urlparse import urlparse
class NetworkcacheClient(object): class NetworkcacheClient(object):
''' '''
NetworkcacheClient is a wrapper for httplib. NetworkcacheClient is a wrapper for httplib.
It must implement all the required methods to use the Networkcache HTTP It must implement all the required methods to use:
Server. - SHADIR
- SHACACHE
- put(file)
- get(key)
''' '''
def __init__(self, networkcache_url):
# XXX (lucas): Is it required to check if networkcache_url is a valid URL?
self.networkcache_url = networkcache_url
def _start(self): def __init__(self, shacache, shadir=None):
self.connection = httplib.HTTPConnection(self.networkcache_url) ''' Set the initial values. '''
# ShaCache Properties
shacache_urlparse = urlparse(shacache)
self.shacache_host = shacache_urlparse.hostname
if shacache_urlparse.port is not None:
self.shacache_host += ':%s' % shacache_urlparse.port
def _close(self): self.shacache_header_dict = {'Content-Type': 'application/json'}
self.connection.close() shacache_user = shacache_urlparse.username
shacache_passwd = shacache_urlparse.password
if shacache_user is not None:
authentication_string = '%s:%s' % (shacache_user, shacache_passwd)
base64string = base64.encodestring(authentication_string).strip()
self.shacache_header_dict['Authorization'] = 'Basic %s' % base64string
def put(self, path, file_content): self.shacache_path = '/'
''' if shacache_urlparse.path not in ('', '/'):
Upload the file to the server. self.shacache_path = shacache_urlparse.path
It uses http PUT resquest method.
shacache_urlparse = urlparse(shacache)
self.shacache_host = shacache_urlparse.hostname
if shacache_urlparse.port is not None:
self.shacache_host += ':%s' % shacache_urlparse.port
# ShaDir Properties
if shadir is not None:
shadir_urlparse = urlparse(shadir)
self.shadir_header_dict = {'Content-Type': 'application/json'}
shadir_user = shadir_urlparse.username
shadir_passwd = shadir_urlparse.password
if shadir_user is not None:
authentication_string = '%s:%s' % (shadir_user, shadir_passwd)
base64string = base64.encodestring(authentication_string).strip()
self.shadir_header_dict['Authorization'] = 'Basic %s' % base64string
self.shadir_path = '/'
if shadir_urlparse.path not in ('', '/'):
self.shadir_path = shadir_urlparse.path
shadir_urlparse = urlparse(shadir)
self.shadir_host = shadir_urlparse.hostname
if shadir_urlparse.port is not None:
self.shadir_host += ':%s' % shadir_urlparse.port
def upload(self, file_descriptor, directory_key=None, **kw):
''' Upload the file to the server.
If directory_key is None it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR.
''' '''
if file_content is None: if directory_key is not None and 'urlmd5' not in kw:
raise ValueError('File content should not be None.') msg = 'The parameter "urlmd5" is required once you set directory_key.'
raise ValueError(msg)
file_content = file_descriptor.read()
sha512sum = hashlib.sha512(file_content).hexdigest()
path = os.path.join(self.shacache_path, sha512sum)
self._start() self.shacache_connection = httplib.HTTPConnection(self.shacache_host)
try: self.shacache_connection.request('POST', path, file_content,
self.connection.request('PUT', path, file_content) self.shacache_header_dict)
result = self.connection.getresponse() result = self.shacache_connection.getresponse()
finally:
self._close()
if result.status != 200:
return result
if directory_key is not None:
path = os.path.join(self.shadir_path, directory_key)
file_name = kw.get('file', None)
if file_name is None:
kw['file'] = file_descriptor.name
sha512 = kw.get('sha512', None)
if sha512 is None:
kw['sha512'] = sha512sum
signature = kw.get('signature', None)
if signature is None:
signature = "SIGNATURE"
data = [kw, signature]
self.shadir_connection = httplib.HTTPConnection(self.shadir_host)
self.shadir_connection.request('POST', path, json.dumps(data),
self.shadir_header_dict)
result = self.shadir_connection.getresponse()
return result return result
def get(self, key): def download(self, sha512sum):
''' ''' Download the file.
Download the file. It uses http GET request method.
It uses http GET request method.
''' '''
path_info = '/%s' % key path_info = os.path.join(self.shacache_path, sha512sum)
self._start() self.shacache_connection = httplib.HTTPConnection(self.shacache_host)
try: self.shacache_connection.request('GET', path_info, headers=self.shacache_header_dict)
self.connection.request('GET', path_info) result = self.shacache_connection.getresponse()
result = self.connection.getresponse() if result.status != 200:
finally: raise FileNotFound(result.read())
self._close()
return result return result
def close(self):
''' It must be called to close the HTTP Connection. '''
self.shacache_connection.close()
self.shadir_connection.close()
class FileNotFound(Exception):
pass
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