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!')
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!')
def __init__(self, client, name, max_buffer=MAX_BUFFER): self.client = client r = self.client.get_file(name) self.bytes = int(r.getheader('Content-Length')) if r > max_buffer: temp = tempfile.TemporaryFile() else: temp = StringIO() shutil.copyfileobj(r, temp) temp.seek(0) super(SpooledReader, self).__init__(temp, name)
def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): # TODO: chunked transport of large files epath = self.encode_path(path) if "w" in mode: self.proxy.set_contents(epath, xmlrpclib.Binary(b(""))) if "r" in mode or "a" in mode or "+" in mode: try: data = self.proxy.get_contents(epath, "rb").data except IOError: if "w" not in mode and "a" not in mode: raise ResourceNotFoundError(path) if not self.isdir(dirname(path)): raise ParentDirectoryMissingError(path) self.proxy.set_contents(path, xmlrpclib.Binary(b(""))) else: data = b("") f = StringIO(data) if "a" not in mode: f.seek(0, 0) else: f.seek(0, 2) oldflush = f.flush oldclose = f.close oldtruncate = f.truncate def newflush(): self._lock.acquire() try: oldflush() self.proxy.set_contents(epath, xmlrpclib.Binary(f.getvalue())) finally: self._lock.release() def newclose(): self._lock.acquire() try: f.flush() oldclose() finally: self._lock.release() def newtruncate(size=None): self._lock.acquire() try: oldtruncate(size) f.flush() finally: self._lock.release() f.flush = newflush f.close = newclose f.truncate = newtruncate return f
def open(self, path, mode="r"): # TODO: chunked transport of large files path = self.encode_path(path) if "w" in mode: self.proxy.set_contents(path, xmlrpclib.Binary(b(""))) if "r" in mode or "a" in mode or "+" in mode: try: data = self.proxy.get_contents(path, "rb").data except IOError: if "w" not in mode and "a" not in mode: raise ResourceNotFoundError(path) if not self.isdir(dirname(path)): raise ParentDirectoryMissingError(path) self.proxy.set_contents(path, xmlrpclib.Binary(b(""))) else: data = b("") f = StringIO(data) if "a" not in mode: f.seek(0, 0) else: f.seek(0, 2) oldflush = f.flush oldclose = f.close oldtruncate = f.truncate def newflush(): self._lock.acquire() try: oldflush() self.proxy.set_contents(path, xmlrpclib.Binary(f.getvalue())) finally: self._lock.release() def newclose(): self._lock.acquire() try: f.flush() oldclose() finally: self._lock.release() def newtruncate(size=None): self._lock.acquire() try: oldtruncate(size) f.flush() finally: self._lock.release() f.flush = newflush f.close = newclose f.truncate = newtruncate return f
def open(self, path, mode="r"): # TODO: chunked transport of large files path = self.encode_path(path) if "w" in mode: self.proxy.set_contents(path,xmlrpclib.Binary("")) if "r" in mode or "a" in mode or "+" in mode: try: data = self.proxy.get_contents(path).data except IOError: if "w" not in mode and "a" not in mode: raise ResourceNotFoundError(path) if not self.isdir(dirname(path)): raise ParentDirectoryMissingError(path) self.proxy.set_contents(path,xmlrpclib.Binary("")) else: data = "" f = StringIO(data) if "a" not in mode: f.seek(0,0) else: f.seek(0,2) oldflush = f.flush oldclose = f.close oldtruncate = f.truncate def newflush(): oldflush() self.proxy.set_contents(path,xmlrpclib.Binary(f.getvalue())) def newclose(): f.flush() oldclose() def newtruncate(size=None): oldtruncate(size) f.flush() f.flush = newflush f.close = newclose f.truncate = newtruncate return f