Exemple #1
0
    def get_handlers(cls):

        _root = os.path.join(os.path.dirname(__file__),
                             "../resource/assets/ppkefu/assets")
        _generic_store = get_config_server_generic_store()
        if _generic_store == None:
            return []

        handlers = [
            (r"/", MainHandler),
            (r"/js/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/js"
            }),
            (r"/css/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/css"
            }),
            (r"/fonts/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/fonts"
            }),
            (r"/img/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/img"
            }),
            (r"/download/(.*)", DownloadHandler, {
                "path": "/"
            }),
            (r"/icon/([^\/]+)?$", tornado.web.StaticFileHandler, {
                "path": _generic_store + os.path.sep
            }),
            (r"/material/([^\/]+)?$", MaterialFileHandler, {
                "path": _generic_store + os.path.sep
            }),
            (r"/upload/(.*)", UploadHandler),
        ]
        return handlers
Exemple #2
0
    def get_handlers(cls):

        _root = os.path.join(os.path.dirname(__file__), "../ppkefu/dist")
        _generic_store = get_config_server_generic_store()
        if _generic_store == None:
            logging.error("PPKefu not run for PPMessage not configed")
            return []

        handlers = [
            (r"/", MainHandler),
            (r"/js/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/js"
            }),
            (r"/css/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/css"
            }),
            (r"/fonts/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/fonts"
            }),
            (r"/img/(.*)", tornado.web.StaticFileHandler, {
                "path": _root + "/img"
            }),
            (r"/download/(.*)", DownloadHandler, {
                "path": "/"
            }),
            (r"/icon/([^\/]+)?$", tornado.web.StaticFileHandler, {
                "path": _generic_store + os.path.sep
            }),
            (r"/material/([^\/]+)?$", MaterialFileHandler, {
                "path": _generic_store + os.path.sep
            }),
            (r"/upload/(.*)", UploadHandler),
        ]
        return handlers
Exemple #3
0
    def _update(self):
        _redis = self.application.redis
        _request = json.loads(self.request.body)

        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        _user_icon = _request.get("user_icon")

        if _user_uuid == None or _app_uuid == None:
            self.setErrorCode(API_ERR.NO_PARA)
            return

        _old_password = _request.get("old_password")
        _user_password = _request.get("user_password")
        if _old_password != None and _user_password != None:
            _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
            _ex_password = _redis.hget(_key, "user_password")
            if _ex_password != _old_password:
                self.setErrorCode(API_ERR.MIS_ERR)
                return

        # remove not table fields
        _data = copy.deepcopy(_request)
        del _data["app_uuid"]
        del _data["user_uuid"]

        if _old_password != None:
            del _data["old_password"]

        if _user_icon != None:
            if _user_icon.startswith("http"):
                IOLoop.current().spawn_callback(download_random_identicon,
                                                _user_icon)
            else:
                _generic_store = get_config_server_generic_store()
                _abs = _generic_store + os.path.sep + _user_icon
                _dest = get_config_server_identicon_store()
                shutil.copy(_abs, _dest)
                if os.path.exists(_abs):
                    IOLoop.current().spawn_callback(upload_random_identicon,
                                                    _abs)
                    _data["user_icon"] = get_random_identicon_url(_user_icon)

        if len(_data) > 0:
            _updated = generic_update(_redis, DeviceUser, _user_uuid, _data)
            if not _updated:
                self.setErrorCode(API_ERR.GENERIC_UPDATE)
                return

        return
    def _update(self):
        _redis = self.application.redis
        _request = json.loads(self.request.body)

        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        _user_icon = _request.get("user_icon")
        
        if _user_uuid == None or _app_uuid == None:
            self.setErrorCode(API_ERR.NO_PARA)
            return

        _old_password = _request.get("old_password")
        _user_password = _request.get("user_password")
        if _old_password != None and _user_password != None:
            _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
            _ex_password = _redis.hget(_key, "user_password")
            if _ex_password != _old_password:
                self.setErrorCode(API_ERR.MIS_ERR)
                return

        # remove not table fields
        _data = copy.deepcopy(_request)
        del _data["app_uuid"]
        del _data["user_uuid"]

        if _old_password != None:
            del _data["old_password"]

        if _user_icon != None:
            if _user_icon.startswith("http"):
                IOLoop.current().spawn_callback(download_random_identicon, _user_icon)
            else:
                _generic_store = get_config_server_generic_store()
                _abs = _generic_store + os.path.sep + _user_icon
                _dest = get_config_server_identicon_store()
                shutil.copy(_abs, _dest)
                if os.path.exists(_abs):
                    IOLoop.current().spawn_callback(upload_random_identicon, _abs)
                    _data["user_icon"] = get_random_identicon_url(_user_icon)

        if len(_data) > 0:
            _updated = generic_update(_redis, DeviceUser, _user_uuid, _data)
            if not _updated:
                self.setErrorCode(API_ERR.GENERIC_UPDATE)
                return

        return
Exemple #5
0
    def get_handlers(cls):

        _root = os.path.join(os.path.dirname(__file__), "../resource/assets/ppkefu/assets")
        _generic_store = get_config_server_generic_store()
        if _generic_store == None:
            logging.error("PPKefu not run for PPMessage not configed")
            return []
        
        handlers = [
            (r"/", MainHandler),
            (r"/js/(.*)", tornado.web.StaticFileHandler, {"path": _root + "/js"}),
            (r"/css/(.*)", tornado.web.StaticFileHandler, {"path": _root + "/css"}),
            (r"/fonts/(.*)", tornado.web.StaticFileHandler, {"path": _root + "/fonts"}),
            (r"/img/(.*)", tornado.web.StaticFileHandler, {"path": _root + "/img"}),
            
            (r"/download/(.*)", DownloadHandler, {"path": "/"}),
            (r"/icon/([^\/]+)?$", tornado.web.StaticFileHandler, {"path": _generic_store + os.path.sep}),
            (r"/material/([^\/]+)?$", MaterialFileHandler, {"path": _generic_store + os.path.sep}),
            (r"/upload/(.*)", UploadHandler),
        ]
        return handlers
Exemple #6
0
    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
Exemple #7
0
    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
Exemple #8
0
    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 = hashlib.sha1(_file_body).hexdigest()
        _new_name = str(uuid.uuid1())

        _generic_store = get_config_server_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(self.application.redis)

        _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 _update(self):
        _redis = self.application.redis
        _request = json.loads(self.request.body)

        _user_uuid = _request.get("user_uuid")
        _user_icon = _request.get("user_icon")

        _user_mobile = _request.get("user_mobile")
        _user_email = _request.get("user_email")
        
        if _user_uuid == None :
            self.setErrorCode(API_ERR.NO_PARA)
            return

        _old_password = _request.get("old_password")
        _user_password = _request.get("user_password")
        if _old_password != None and _user_password != None:
            _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
            _ex_password = _redis.hget(_key, "user_password")
            if _ex_password != _old_password:
                self.setErrorCode(API_ERR.MIS_ERR)
                return

        # remove not table fields
        _data = copy.deepcopy(_request)
        del _data["user_uuid"]

        if _old_password != None:
            del _data["old_password"]

        if _user_icon != None:
            if _user_icon.startswith("http"):
                IOLoop.current().spawn_callback(download_random_identicon, _user_icon)
            else:
                _generic_store = get_config_server_generic_store()
                _abs = _generic_store + os.path.sep + _user_icon
                _dest = get_config_server_identicon_store()
                shutil.copy(_abs, _dest)
                if os.path.exists(_abs):
                    IOLoop.current().spawn_callback(upload_random_identicon, _abs)
                    _data["user_icon"] = get_random_identicon_url(_user_icon)

        if len(_data) > 0:
            _updated = generic_update(_redis, DeviceUser, _user_uuid, _data)
            if not _updated:
                self.setErrorCode(API_ERR.GENERIC_UPDATE)
                return

        if _user_mobile:
            import phonenumbers
            _p = phonenumbers.parse(_user_mobile, None)

            if not phonenumbers.is_valid_number(_p):
                self.setErrorCode(API_ERR.INVALID_PHONENUMBER)
                return

            _row = DeviceUser(uuid=_user_uuid,
                              user_mobile=_user_mobile)
            _row.update_redis_keys(_redis)
            _row.async_update(_redis)

        if _user_email:
            _key = DeviceUser.__tablename__ + ".user_email." + _user_email
            if _redis.exists(_key):
                self.setErrorCode(API_ERR.EX_USER)
                return

            _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
            _old_email = _redis.hget(_key, "user_email")
            if _old_email:
                _key = DeviceUser.__tablename__ + ".user_email." + _old_email
                _redis.delete(_key)
                
            _row = DeviceUser(uuid=_user_uuid,
                              user_email=_user_email)
            _row.update_redis_keys(_redis)
            _row.async_update(_redis)

        return
Exemple #10
0
    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 = hashlib.sha1(_file_body).hexdigest()
        _new_name = str(uuid.uuid1())

        _generic_store = get_config_server_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(self.application.redis)
                
        _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
Exemple #11
0
    def _update(self):
        _redis = self.application.redis
        _request = json.loads(self.request.body)

        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        _user_icon = _request.get("user_icon")

        _user_mobile = _request.get("user_mobile")
        _user_email = _request.get("user_email")

        if _user_uuid == None or _app_uuid == None:
            self.setErrorCode(API_ERR.NO_PARA)
            return

        _old_password = _request.get("old_password")
        _user_password = _request.get("user_password")
        if _old_password != None and _user_password != None:
            _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
            _ex_password = _redis.hget(_key, "user_password")
            if _ex_password != _old_password:
                self.setErrorCode(API_ERR.MIS_ERR)
                return

        # remove not table fields
        _data = copy.deepcopy(_request)
        del _data["app_uuid"]
        del _data["user_uuid"]

        if _old_password != None:
            del _data["old_password"]

        if _user_icon != None:
            if _user_icon.startswith("http"):
                IOLoop.current().spawn_callback(download_random_identicon,
                                                _user_icon)
            else:
                _generic_store = get_config_server_generic_store()
                _abs = _generic_store + os.path.sep + _user_icon
                _dest = get_config_server_identicon_store()
                shutil.copy(_abs, _dest)
                if os.path.exists(_abs):
                    IOLoop.current().spawn_callback(upload_random_identicon,
                                                    _abs)
                    _data["user_icon"] = get_random_identicon_url(_user_icon)

        if len(_data) > 0:
            _updated = generic_update(_redis, DeviceUser, _user_uuid, _data)
            if not _updated:
                self.setErrorCode(API_ERR.GENERIC_UPDATE)
                return

        if _user_mobile:
            import phonenumbers
            _p = phonenumbers.parse(_user_mobile, None)

            if not phonenumbers.is_valid_number(_p):
                self.setErrorCode(API_ERR.INVALID_PHONENUMBER)
                return

            _row = DeviceUser(uuid=_user_uuid, user_mobile=_user_mobile)
            _row.update_redis_keys(_redis)
            _row.async_update(_redis)

        if _user_email:
            _key = DeviceUser.__tablename__ + ".user_email." + _user_email
            if _redis.exists(_key):
                self.setErrorCode(API_ERR.EX_USER)
                return

            _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
            _old_email = _redis.hget(_key, "user_email")
            if _old_email:
                _key = DeviceUser.__tablename__ + ".user_email." + _old_email
                _redis.delete(_key)

            _row = DeviceUser(uuid=_user_uuid, user_email=_user_email)
            _row.update_redis_keys(_redis)
            _row.async_update(_redis)

        return
Exemple #12
0
    def post(self):

        logging.info(self.request.body)
        _generic_store = get_config_server_generic_store()
        if _generic_store == None:
            logging.error("Generic store not configed")
            return
        
        _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())
        _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(_redis)

        _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