def _upload(self, path, data_length): cherrypy.response.timeout = 3600 * 24 cherrypy.response.headers["Access-Control-Allow-Origin"] = "*" path = unicode( urllib.unquote(path.replace("\\", "/").strip()).encode('utf-8')) (parentFolder, filename) = os.path.split(path) try: tmp_file_path = os.path.join( parentFolder, ".popotmp_" + str(time.time() * 1000)[-8:]) tmp_file = open(tmp_file_path, "wb") tmp_length = 0 data = cherrypy.request.body.fp while True: file_data = data.read(8192) if not file_data: break tmp_length += len(file_data) tmp_file.write(file_data) WebFunc.refreshToken() tmp_file.close() if tmp_length != data_length: os.remove(tmp_file_path) return None return tmp_file_path except Exception, e: Log.exception('Files Upload Failed! Reason[%s]' % e) if tmp_file: tmp_file.close() if os.path.exists(tmp_file_path): os.remove(tmp_file_path) return None
class ResponseFile(): #Only a File Object like StringIO def __init__(self, file_base, size, conn=None, ftype=None): self.closed = False self.pos = 0 self.buf = file_base self.len = int(size) self.conn = conn self.type = ftype def __len__(self): return self.len def setBuf(self, body): self.buf += body self.len += len(body) def read(self, size=None): if size is None or size < 0: size = self.len - self.pos elif size > self.len - self.pos: size = self.len - self.pos if size <= 0: return None try: r = self.buf.read(size) self.pos += size except Exception, e: r = None WebFunc.refreshToken() return r