def create_file_with_data(_redis, _data, _mime, _user_uuid, _file_name=None, _material_type=None): if _data == None or len(_data) == 0: return None _hash = hashlib.sha1(_data).hexdigest() _key = FileInfo.__tablename__ + ".file_hash." + _hash _uuid = _redis.get(_key) if _uuid != None: return _uuid _name = str(uuid.uuid1()) if _file_name == None: _file_name = _name _store_dir = BOOTSTRAP_DATA.get("server").get("generic_store") _path = _store_dir + os.path.sep + _name _f = open(_path, "wb") _f.write(_data) _f.close() _row = FileInfo(**{ "uuid": _name, "user_uuid": _user_uuid, "file_name": _file_name, "file_size": len(_data), "file_path": _path, "file_hash": _hash, "file_mime": _mime, "material_type": _material_type, }) _row.async_add(_redis) _row.create_redis_keys(_redis) return _name
def create_file_with_data(_redis, _data, _mime, _user_uuid, _file_name=None, _material_type=None): _store_dir = get_config_server_generic_store() if _store_dir == None: logging.error("no generic_store configed") return None if _data == None or len(_data) == 0: return None _hash = hashlib.sha1(_data).hexdigest() _key = FileInfo.__tablename__ + ".file_hash." + _hash _uuid = _redis.get(_key) if _uuid != None: return _uuid _name = str(uuid.uuid1()) if _file_name == None: _file_name = _name _path = _store_dir + os.path.sep + _name _f = open(_path, "wb") _f.write(_data) _f.close() _row = FileInfo( **{ "uuid": _name, "user_uuid": _user_uuid, "file_name": _file_name, "file_size": len(_data), "file_path": _path, "file_hash": _hash, "file_mime": _mime, "material_type": _material_type, }) _row.async_add(_redis) _row.create_redis_keys(_redis) return _name
def post(self, id=None): _redis = self.application.redis logging.info(self.request.body) if self.content_length > UPLOAD_MAX_BYTE: logging.error("upload file over limit: %d > %d" % self.content_length, UPLOAD_MAX_BYTE) self.send_error() return _generic_store = get_config_server_generic_store() if _generic_store == None: logging.error("Generic store not configed") self.send_error() return _info = self.request.files['file'][0] if _info == None: logging.error("No file info in request.files: %s", self.request.files) self.send_error() return _uuid = str(uuid.uuid1()) _file_name = _info.get("filename") or _uuid _file_type = _info.get("content_type") or "application/octet-stream" _file_body = _info.get("body") or "" _file_size = len(_file_body) _file_sha1 = hashlib.sha1(_file_body).hexdigest() _material_type = self.get_argument("material_type", default=None) _user_uuid = self.get_argument("user_uuid", default=None) if _file_size == 0: logging.error("file size 0: %s" % _file_name) self.send_error() return _key = FileInfo.__tablename__ + ".file_hash." + _file_sha1 _existed_uuid = _redis.get(_key) if _existed_uuid != None: logging.info("same file: %s, %s" % (_existed_uuid, _file_name)) self.write(json.dumps({"fuuid": _existed_uuid})) self.finish() return _new_path = _generic_store + os.path.sep + _uuid with open(_new_path, "w") as _of: _of.write(_file_body) _add = { "uuid": _uuid, "file_name": _file_name, "file_size": _file_size, "file_hash": _file_sha1, "file_path": _new_path, "file_mime": _file_type, "material_type": _material_type, "user_uuid": _user_uuid, } _row = FileInfo(**_add) _row.create_redis_keys(_redis) _row.async_add(_redis) #self.set_header("Content-Type", "text/plain") self.set_header("Access-Control-Allow-Origin", "*") self.write(json.dumps({"fuuid": _uuid})) logging.info(str({"fuuid": _uuid})) self.finish() return
def post(self, id=None): _redis = self.application.redis logging.info(self.request.body) if self.content_length > UPLOAD_MAX_BYTE: logging.error( "upload file over limit: %d > %d" % self.content_length, UPLOAD_MAX_BYTE) self.send_error() return _generic_store = get_config_server_generic_store() if _generic_store == None: logging.error("Generic store not configed") self.send_error() return _info = self.request.files['file'][0] if _info == None: logging.error("No file info in request.files: %s", self.request.files) self.send_error() return _uuid = str(uuid.uuid1()) _file_name = _info.get("filename") or _uuid _file_type = _info.get("content_type") or "application/octet-stream" _file_body = _info.get("body") or "" _file_size = len(_file_body) _file_sha1 = hashlib.sha1(_file_body).hexdigest() _material_type = self.get_argument("material_type", default=None) _user_uuid = self.get_argument("user_uuid", default=None) if _file_size == 0: logging.error("file size 0: %s" % _file_name) self.send_error() return _key = FileInfo.__tablename__ + ".file_hash." + _file_sha1 _existed_uuid = _redis.get(_key) if _existed_uuid != None: logging.info("same file: %s, %s" % (_existed_uuid, _file_name)) self.write(json.dumps({"fuuid": _existed_uuid})) self.finish() return _new_path = _generic_store + os.path.sep + _uuid with open(_new_path, "w") as _of: _of.write(_file_body) _add = { "uuid": _uuid, "file_name": _file_name, "file_size": _file_size, "file_hash": _file_sha1, "file_path": _new_path, "file_mime": _file_type, "material_type": _material_type, "user_uuid": _user_uuid, } _row = FileInfo(**_add) _row.create_redis_keys(_redis) _row.async_add(_redis) #self.set_header("Content-Type", "text/plain") self.set_header("Access-Control-Allow-Origin", "*") self.write(json.dumps({"fuuid": _uuid})) logging.info(str({"fuuid": _uuid})) self.finish() return
def post(self, id=None): _upload_type = self.get_argument("upload_type", default=None) if _upload_type is None: logging.error("No upload_type set.") return if _upload_type == "file": _list = self.request.files.get("file") if _list is None or len(_list) == 0: logging.error("No files with upload_file input name") return _file_name = _list[0]["filename"] _file_body = _list[0]["body"] _file_mime = _list[0]["content_type"] elif _upload_type == "content_txt": _file_body = self.get_argument("content_txt") if isinstance(_file_body, unicode): _file_body = _file_body.encode("utf-8") _file_mime = "text/plain" _file_name = "txt" elif _upload_type == "content_icon": _file_body = self.get_argument("content_icon") if isinstance(_file_body, unicode): _file_body = _file_body.encode("utf-8") _file_body = _file_body.split("data:image/png;base64,", 1)[1] _file_body = base64.decodestring(_file_body) _file_mime = "image/png" _file_name = "icon" elif _upload_type == "content_html": _file_body = self.get_argument("content_html") _file_mime = "text/html" _file_name = "html" else: logging.error("Error can not handle %s." % (_upload_type)) return _file_sha1 = sha1(_file_body).hexdigest() _new_name = str(uuid.uuid1()) _generic_store = BOOTSTRAP_DATA.get("server") _generic_store = _generic_store.get("generic_store") _new_path = _generic_store + os.path.sep + _new_name with open(_new_path, "wb") as _new_file: _new_file.write(_file_body) _stat_result = os.stat(_new_path) _file_size = _stat_result[stat.ST_SIZE] # FIXME: md5 as key to determine, is there duplicated content _values = { "uuid": _new_name, "file_mime": _file_mime, "file_name": _file_name, "file_size": _file_size, "file_hash": _file_sha1, "file_path": _new_path } _row = FileInfo(**_values) _row.create_redis_keys(self.application.redis) _row.async_add() _r = {} _r["fid"] = _new_name _r["mime"] = _file_mime self.set_header("Access-Control-Allow-Origin", "*") self.set_header("Content-Type", "application/json") self.write(json.dumps(_r)) return
def post(self): logging.info(self.request.body) _field_name = "file" _field_file_name = _field_name + ".name" _field_file_path = _field_name + ".path" _field_file_type = _field_name + ".content_type" _field_file_size = _field_name + ".size" _file_name = self.get_argument(_field_file_name, default=None) _file_path = self.get_argument(_field_file_path, default=None) _file_type = self.get_argument(_field_file_type, default=None) _file_size = self.get_argument(_field_file_size, default=None) _material_type = self.get_argument("material_type", default=None) _user_uuid = self.get_argument("user_uuid", default=None) self.set_header("Access-Control-Allow-Origin", "*") if _file_name is None \ or _file_path is None \ or _file_type is None \ or _file_size is None: logging.error("Error for input params") self.send_error() return _file_sha1 = None with open(_file_path, "r") as _r: _d = _r.read() _file_sha1 = sha1(_d).hexdigest() if _file_sha1 == None: logging.error("sha1 meets error") self.send_error() return _r = {} _redis = self.application.redis _key = FileInfo.__tablename__ + ".file_hash." + _file_sha1 _uuid = _redis.get(_key) if _uuid != None: logging.info("get the same file: %s, %s" % (_uuid, _file_name)) # FIXME: check user name and file name # messageboy hold the file name #if _user_uuid == _old["user_uuid"]: _r["fuuid"] = _uuid self.write(json.dumps(_r)) logging.info(_r) return _fuuid = str(uuid.uuid1()) _server_config = BOOTSTRAP_DATA.get("server") _generic_store = _server_config.get("generic_store") _new_path = _generic_store + os.path.sep + _fuuid move(_file_path, _new_path) _add = { "uuid": _fuuid, "file_name": _file_name, "file_size": _file_size, "file_hash": _file_sha1, "file_path": _new_path, "file_mime": _file_type, "material_type": _material_type, "user_uuid": _user_uuid, } _row = FileInfo(**_add) _row.create_redis_keys(_redis) _row.async_add() _r = {} _r["fuuid"] = _fuuid #self.set_header("Content-Type", "application/json") self.set_header("Access-Control-Allow-Origin", "*") self.write(json.dumps(_r)) logging.info(str(_r)) return
def post(self): logging.info(self.request.body) _field_name = "file" _field_file_name = _field_name + ".name" _field_file_path = _field_name + ".path" _field_file_type = _field_name + ".content_type" _field_file_size = _field_name + ".size" _file_name = self.get_argument(_field_file_name, default=None) _file_path = self.get_argument(_field_file_path, default=None) _file_type = self.get_argument(_field_file_type, default=None) _file_size = self.get_argument(_field_file_size, default=None) _material_type = self.get_argument("material_type", default=None) _user_uuid = self.get_argument("user_uuid", default=None) self.set_header("Access-Control-Allow-Origin", "*") if _file_name is None or _file_path is None or _file_type is None or _file_size is None: logging.error("Error for input params") self.send_error() return _file_sha1 = None with open(_file_path, "r") as _r: _d = _r.read() _file_sha1 = sha1(_d).hexdigest() if _file_sha1 == None: logging.error("sha1 meets error") self.send_error() return _r = {} _redis = self.application.redis _key = FileInfo.__tablename__ + ".file_hash." + _file_sha1 _uuid = _redis.get(_key) if _uuid != None: logging.info("get the same file: %s, %s" % (_uuid, _file_name)) # FIXME: check user name and file name # messageboy hold the file name # if _user_uuid == _old["user_uuid"]: _r["fuuid"] = _uuid self.write(json.dumps(_r)) logging.info(_r) return _fuuid = str(uuid.uuid1()) _server_config = BOOTSTRAP_DATA.get("server") _generic_store = _server_config.get("generic_store") _new_path = _generic_store + os.path.sep + _fuuid move(_file_path, _new_path) _add = { "uuid": _fuuid, "file_name": _file_name, "file_size": _file_size, "file_hash": _file_sha1, "file_path": _new_path, "file_mime": _file_type, "material_type": _material_type, "user_uuid": _user_uuid, } _row = FileInfo(**_add) _row.create_redis_keys(_redis) _row.async_add() _r = {} _r["fuuid"] = _fuuid # self.set_header("Content-Type", "application/json") self.set_header("Access-Control-Allow-Origin", "*") self.write(json.dumps(_r)) logging.info(str(_r)) return