Exemple #1
0
 def upload(self, path, f):
     import pdb
     pdb.set_trace()
     if isinstance(f, basestring):
         # upload given string as file's contents.
         f = StringIO(f)
     l = None
     try:
         l = len(f)
     except:
         try:
             l = os.fstat(f.fileno()).st_size
         except:
             try:
                 f.seek(0, 2)
                 l = f.tell()
                 f.seek(0)
             except:
                 raise Exception('Could not determine length of file!')
     dirname, basename = pathsplit(path)
     try:
         info = self.info(path)
     except:
         try:
             info = self.info(dirname)
         except:
             raise Exception('Cannot upload to non-existent directory!')
     url = '%s/%s/%s' % (ULURL, self.auth_token, info['inode'])
     host = urlparse.urlparse(url).hostname
     conn = httplib.HTTPConnection(host, 443)
     boundary = mimetools.choose_boundary()
     fields = {
         'boundary': boundary,
         'mime': mimetypes.guess_type(basename)[0]
         or 'application/octet-stream',
         'name': basename,
     }
     head = MULTIPART_HEAD % fields
     tail = MULTIPART_TAIL % fields
     l += len(head) + len(tail)
     headers = {
         'Content-Length': l,
         'Content-Type': 'multipart/form-data; boundary=%s' % boundary,
     }
     conn.request('POST', url, '', headers)
     # now stream the file to box.net.
     conn.send(head)
     while True:
         data = f.read(4096)
         if not data:
             break
         conn.send(data)
     conn.send(tail)
     r = conn.getresponse()
     if r.status != 200:
         raise Exception('Error uploading data!')
Exemple #2
0
 def upload(self, path, f):
     import pdb; pdb.set_trace()
     if isinstance(f, basestring):
         # upload given string as file's contents.
         f = StringIO(f)
     l = None
     try:
         l = len(f)
     except:
         try:
             l = os.fstat(f.fileno()).st_size
         except:
             try:
                 f.seek(0, 2)
                 l = f.tell()
                 f.seek(0)
             except:
                 raise Exception('Could not determine length of file!')
     dirname, basename = pathsplit(path)
     try:
         info = self.info(path)
     except:
         try:
             info = self.info(dirname)
         except:
             raise Exception('Cannot upload to non-existent directory!')
     url = '%s/%s/%s' % (ULURL, self.auth_token, info['inode'])
     host = urlparse.urlparse(url).hostname
     conn = httplib.HTTPConnection(host, 443)
     boundary = mimetools.choose_boundary()
     fields = {
         'boundary': boundary,
         'mime': mimetypes.guess_type(basename)[0] or 'application/octet-stream',
         'name': basename,
     }
     head = MULTIPART_HEAD % fields
     tail = MULTIPART_TAIL % fields
     l += len(head) + len(tail)
     headers = {
         'Content-Length': l,
         'Content-Type': 'multipart/form-data; boundary=%s' % boundary,
     }
     conn.request('POST', url, '', headers)
     # now stream the file to box.net.
     conn.send(head)
     while True:
         data = f.read(4096)
         if not data:
             break
         conn.send(data)
     conn.send(tail)
     r = conn.getresponse()
     if r.status != 200:
         raise Exception('Error uploading data!')