def getServer(): ip = BOOTSTRAP_DATA.get("server").get("name") port = BOOTSTRAP_DATA.get("nginx").get("listen") protocol = "http://" if BOOTSTRAP_DATA.get("nginx").get("ssl") == "on": protocol = "https://" url = protocol + ip + ":" + port return url
def _create(dbsession): _dev_pem = None _pro_pem = None _dev_p12 = None _pro_p12 = None _apns_config = BOOTSTRAP_CONFIG.get("apns") _dev_file = _apns_config.get("dev") _pro_file = _apns_config.get("pro") _certs_dir = os.path.dirname(os.path.abspath(__file__)) _certs_dir = _certs_dir + os.path.sep + ".." + os.path.sep + "certs" + os.path.sep + "apnscerts" _dev_file = _certs_dir + os.path.sep + _dev_file _pro_file = _certs_dir + os.path.sep + _pro_file if not os.path.exists(_dev_file) or not os.path.exists(_pro_file): print("No dev or pro cert file found") sys.exit() with open(_dev_file, "rb") as _file: _dev_p12 = _file.read() _dev_pem = der2pem(_dev_p12) with open(_pro_file, "rb") as _file: _pro_p12 = _file.read() _pro_pem = der2pem(_pro_p12) _dev_p12 = base64.b64encode(_dev_p12) _dev_pem = base64.b64encode(_dev_pem) _pro_p12 = base64.b64encode(_pro_p12) _pro_pem = base64.b64encode(_pro_pem) _app_uuid = BOOTSTRAP_DATA.get("team").get("app_uuid") _name = BOOTSTRAP_DATA.get("apns").get("name") print(len(_dev_pem)) print(len(_dev_p12)) print(is_dev) _apns = APNSSetting( uuid=str(uuid.uuid1()), name=_name, app_uuid=_app_uuid, production_p12=_pro_p12, development_p12=_dev_p12, production_pem=_pro_pem, development_pem=_dev_pem, is_development=is_dev, is_production=not is_dev, createtime=datetime.datetime.now(), updatetime=datetime.datetime.now(), ) dbsession.add(_apns) dbsession.commit() return
def _create(dbsession): _dev_pem = None _pro_pem = None _dev_p12 = None _pro_p12 = None _apns_config = BOOTSTRAP_CONFIG.get("apns") _dev_file = _apns_config.get("dev") _pro_file = _apns_config.get("pro") _certs_dir = os.path.dirname(os.path.abspath(__file__)) _certs_dir = _certs_dir + os.path.sep + ".." + os.path.sep + "certs" + os.path.sep + "apnscerts" _dev_file = _certs_dir + os.path.sep + _dev_file _pro_file = _certs_dir + os.path.sep + _pro_file print(_pro_file) if not os.path.exists(_dev_file) or not os.path.exists(_pro_file): print("No dev or pro cert file found") sys.exit() with open(_dev_file, "rb") as _file: _dev_p12 = _file.read() _dev_pem = der2pem(_dev_p12) with open(_pro_file, "rb") as _file: _pro_p12 = _file.read() _pro_pem = der2pem(_pro_p12) _dev_p12 = base64.b64encode(_dev_p12) _dev_pem = base64.b64encode(_dev_pem) _pro_p12 = base64.b64encode(_pro_p12) _pro_pem = base64.b64encode(_pro_pem) _app_uuid = BOOTSTRAP_DATA.get("team").get("app_uuid") _name = BOOTSTRAP_DATA.get("apns").get("name") print(len(_dev_pem)) print(len(_dev_p12)) print(is_dev) _apns = APNSSetting( uuid=str(uuid.uuid1()), name=_name, app_uuid=_app_uuid, production_p12=_pro_p12, development_p12=_dev_p12, production_pem=_pro_pem, development_pem=_dev_pem, is_development=is_dev, is_production=not is_dev, createtime=datetime.datetime.now(), updatetime=datetime.datetime.now(), ) dbsession.add(_apns) dbsession.commit() return
def _icon_url(_file_name): _post = "/identicon/" + _file_name _server_name = BOOTSTRAP_DATA.get("server").get("name") _ssl = BOOTSTRAP_DATA.get("nginx").get("ssl") _protocol = "http" _port = BOOTSTRAP_DATA.get("nginx").get("listen") if _ssl == "yes": _protocol = "https" _port = BOOTSTRAP_DATA.get("nginx").get("ssl_listen") _url = _protocol + "://" + _server_name + ":" + _port + _post return _url
def create_group_icon(_redis, _users): if len(_users) == 0: return None _icon_list = [] for _uuid in _users: _user_key = DeviceUser.__tablename__ + ".uuid." + _uuid _user_icon = _redis.hget(_user_key, "user_icon") if _user_icon == None: _icon_list.append(_user_icon) continue _file_key = FileInfo.__tablename__ + ".uuid." + _user_icon _file_path = _redis.hget(_file_key, "file_path") _icon_list.append(_file_path) _data = ImageConverter.conversation_icon(_icon_list) if _data == None: logging.error( "conversation icon data is None, will not create icon file") return None _file_name = hashlib.sha1("".join(_users)).hexdigest() + ".png" _identicon_store = BOOTSTRAP_DATA.get("server").get("identicon_store") _file_path = _identicon_store + os.path.sep + _file_name _file = open(_file_path, "wb") _file.write(_data) _file.close() return _icon_url(_file_name)
def create_group_icon(_redis, _users): if len(_users) == 0: return None _icon_list = [] for _uuid in _users: _user_key = DeviceUser.__tablename__ + ".uuid." + _uuid _user_icon = _redis.hget(_user_key, "user_icon") if _user_icon == None: _icon_list.append(_user_icon) continue _file_key = FileInfo.__tablename__ + ".uuid." + _user_icon _file_path = _redis.hget(_file_key, "file_path") _icon_list.append(_file_path) _data = ImageConverter.conversation_icon(_icon_list) if _data == None: logging.error("conversation icon data is None, will not create icon file") return None _file_name = hashlib.sha1("".join(_users)).hexdigest() + ".png" _identicon_store = BOOTSTRAP_DATA.get("server").get("identicon_store") _file_path = _identicon_store + os.path.sep + _file_name _file = open(_file_path, "wb") _file.write(_data) _file.close() return _icon_url(_file_name)
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 __init__(self): settings = {} settings["debug"] = True settings["cookie_secret"] = "PzEMu2OLSsKGTH2cnyizyK+ydP38CkX3rhbmGPqrfBs=" self.redis = redis.Redis(REDIS_HOST, REDIS_PORT, db=1) DownloadHandler.set_cls_redis(self.redis) _root = os.path.abspath(os.path.dirname(__file__)) + "/ppkefu/www" _generic_store = BOOTSTRAP_DATA.get("server") _generic_store = _generic_store.get("generic_store") handlers = [ (r"^/$", MainHandler), (r"^/cordova.js$", CordovaHandler), (r"/js/(.*)", StaticFileHandler, {"path": _root + "/js"}), (r"/build/(.*)", StaticFileHandler, {"path": _root + "/build"}), (r"/css/(.*)", StaticFileHandler, {"path": _root + "/css"}), (r"/templates/(.*)", StaticFileHandler, {"path": _root + "/templates"}), (r"/lib/(.*)", StaticFileHandler, {"path": _root + "/lib"}), (r"/img/(.*)", StaticFileHandler, {"path": _root + "/img"}), (r"/download/(.*)", DownloadHandler, {"path": "/"}), (r"/icon/([^\/]+)?$", StaticFileHandler, {"path": _generic_store + os.path.sep}), (r"/material/([^\/]+)?$", MaterialFileHandler, {"path": _generic_store + os.path.sep}), (r"/upload/(.*)", UploadHandler), ] Application.__init__(self, handlers, **settings)
def get_handlers(cls): _root = os.path.abspath(os.path.dirname(__file__)) + "/ppkefu/www" _generic_store = BOOTSTRAP_DATA.get("server") _generic_store = _generic_store.get("generic_store") handlers = [ (r"^/$", MainHandler), (r"^/cordova.js$", CordovaHandler), (r"/js/(.*)", StaticFileHandler, { "path": _root + "/js" }), (r"/css/(.*)", StaticFileHandler, { "path": _root + "/css" }), (r"/fonts/(.*)", StaticFileHandler, { "path": _root + "/fonts" }), (r"/img/(.*)", StaticFileHandler, { "path": _root + "/img" }), (r"/download/(.*)", DownloadHandler, { "path": "/" }), (r"/icon/([^\/]+)?$", StaticFileHandler, { "path": _generic_store + os.path.sep }), (r"/material/([^\/]+)?$", MaterialFileHandler, { "path": _generic_store + os.path.sep }), (r"/upload/(.*)", UploadHandler), ] return handlers
def __init__(self): settings = {} settings["debug"] = True settings["cookie_secret"] = "PzEMu2OLSsKGTH2cnyizyK+ydP38CkX3rhbmGPqrfBs=" self.redis = redis.Redis(REDIS_HOST, REDIS_PORT, db=1) DownloadHandler.set_cls_redis(self.redis) _root = os.path.abspath(os.path.dirname(__file__)) + "/ppkefu/www" _generic_store = BOOTSTRAP_DATA.get("server") _generic_store = _generic_store.get("generic_store") handlers = [ (r"^/$", MainHandler), (r"^/cordova.js$", CordovaHandler), (r"/js/(.*)", StaticFileHandler, {"path": _root + "/js"}), (r"/css/(.*)", StaticFileHandler, {"path": _root + "/css"}), (r"/fonts/(.*)", StaticFileHandler, {"path": _root + "/fonts"}), (r"/img/(.*)", StaticFileHandler, {"path": _root + "/img"}), (r"/download/(.*)", DownloadHandler, {"path": "/"}), (r"/icon/([^\/]+)?$", StaticFileHandler, {"path": _generic_store + os.path.sep}), (r"/material/([^\/]+)?$", MaterialFileHandler, {"path": _generic_store + os.path.sep}), (r"/upload/(.*)", UploadHandler), ] Application.__init__(self, handlers, **settings)
def __init__(self, _app): self.application = _app _config = BOOTSTRAP_DATA.get("gcm") _api_key = _config.get("api_key") self.gcm = None if _api_key != None: self.gcm = GCM(_api_key) return
def create_user_icon(_uuid): _image = Identicon(_uuid, 64) _image = _image.draw_image() _file_name = _uuid + ".png" _identicon_store = BOOTSTRAP_DATA.get("server").get("identicon_store") _path = _identicon_store + os.path.sep + _file_name _image.save(_path) return _icon_url(_file_name)
def _push_to_ios(self, _user_uuid, _device_uuid): logging.info("push ios %s:%s" % (_user_uuid, _device_uuid)) _apns_config = BOOTSTRAP_DATA.get("apns") _apns_name = _apns_config.get("name") _apns_dev = _apns_config.get("dev") _apns_pro = _apns_config.get("pro") if _apns_name == None or len(_apns_name) == 0 or _apns_dev == None or len(_apns_dev) == 0 or _apns_pro == None or len(_apns_pro) == 0: logging.info("iospush not start for no apns config") return _device = self._devices_hash.get(_device_uuid) _user = self._users_hash.get(_user_uuid) _app_uuid = self._task["app_uuid"] _conversation_data = self._conversation_user_datas_hash.get(_user_uuid) if _user == None: logging.error("push ios failed for 0001") return if _device == None: logging.error("push ios failed for 0002") return _token = _device.get("device_ios_token") if _token == None or len(_token) == 0: logging.error("push ios failed for 2") return if _device["device_ios_token"] == IOS_FAKE_TOKEN: logging.error("push ios failed for 3") return if _conversation_data != None and _conversation_data["user_mute_notification"] == True: # user only do not want recv push for this conversation logging.error("push ios failed for 4") return _count = 0 if _user.get("user_show_badge") == True: _key = MessagePush.__tablename__ + \ ".app_uuid." + _app_uuid + \ ".user_uuid." + _user_uuid + \ ".device_uuid." + _device_uuid _count = len(self._redis.smembers(_key)) _config = { "user_language": _user.get("user_language"), "device_ios_token": _token, "unacked_notification_count": _count, "user_silence_notification": _user.get("user_silence_notification") } _push = { "config": _config, "body": self._task.get("message_body"), "app": _app_uuid } logging.info("push ios: %s" % str(_push)) async_signal_iospush_push(_push) return
def _create_user_name(self): """ Get user nickName """ _language = "en" _string = USER_NAME["en"] if BOOTSTRAP_DATA.get("user_language") == "zh_cn": _language = "zh-CN" _string = USER_NAME["cn"] if BOOTSTRAP_DATA.get("user_language") == "zh_tw": _language = "zh-TW" _string = USER_NAME["tw"] _ip = self.request.headers.get( "X-Real-Ip") or self.request.headers.get("remote_ip") if _ip == None or _ip == "127.0.0.1" or _ip == "localhost" or "192.168." in _ip: return _string.get("local") + "." + _string.get("user") _city = None try: _city = self.application.geoip_reader.city(_ip) except: logging.error("geoip failed: %s" % _ip) _city = None if _city == None: return _string.get("unknown") + "." + _string.get("user") _country_name = _city.country.names.get(_language) _city_name = _city.city.names.get(_language) if _city_name != None and _country_name != None: return _country_name + "." + _city_name + "." + _string.get("user") if _city_name != None: return _city_name + "." + _string.get("user") if _country_name != None: return _country_name + "." + _string.get("user") return _string.get("unknown") + "." + _string.get("user")
def _create_user_name(self): """ Get user nickName """ _language = "en" _string = USER_NAME["en"] if BOOTSTRAP_DATA.get("user_language") == "zh_cn": _language = "zh-CN" _string = USER_NAME["cn"] if BOOTSTRAP_DATA.get("user_language") == "zh_tw": _language = "zh-TW" _string = USER_NAME["tw"] _ip = self.request.headers.get("X-Real-Ip") or self.request.headers.get("remote_ip") if _ip == None or _ip == "127.0.0.1" or _ip == "localhost" or "192.168." in _ip: return _string.get("local") + "." + _string.get("user") _city = None try: _city = self.application.geoip_reader.city(_ip) except: logging.error("geoip failed: %s" % _ip) _city = None if _city == None: return _string.get("unknown") + "." + _string.get("user") _country_name = _city.country.names.get(_language) _city_name = _city.city.names.get(_language) if _city_name != None and _country_name != None: return _country_name + "." + _city_name + "." + _string.get("user") if _city_name != None: return _city_name + "." + _string.get("user") if _country_name != None: return _country_name + "." + _string.get("user") return _string.get("unknown") + "." + _string.get("user")
def _get_app_welcome(self, _r): _language = BOOTSTRAP_DATA.get("user_language") if _language == None: _language = "zh_cn" _welcome = self._app.get("welcome_message") if _welcome == None: _welcome = PPCOM_WELCOME.get(_language) _r["app_name"] = self._app.get("app_name") _r["app_welcome"] = _welcome return _r
def _get_app_welcome(self): _body = json.loads(self.request.body) _language = BOOTSTRAP_DATA.get("user_language") if _language == None: _language = "zh_cn" _welcome = self._app.get("welcome_message") if _welcome == None: _welcome = PPCOM_WELCOME.get(_language) _r = self.getReturnData() _r["app_name"] = self._app.get("app_name") _r["app_welcome"] = _welcome return
def _main(): tornado.options.parse_command_line() # FIXME: support more apns certifcate _apns = BOOTSTRAP_DATA.get("apns") if _apns == None or len(_apns) == 0: logging.info("apns not config, iospush can not start") sys.exit() _app = IOSPushApp() _app.get_delegate("").run_periodic() logging.info("IOSPush service starting...") tornado.ioloop.IOLoop.instance().start() return
def get_handlers(cls): _root = os.path.join(os.path.dirname(__file__), "../resource/assets/ppkefu/assets") _generic_store = BOOTSTRAP_DATA.get("server") _generic_store = _generic_store.get("generic_store") 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
def _push_to_android(self, _user_uuid, _device_uuid): _app_uuid = self._task["app_uuid"] _device = self._devices_hash.get(_device_uuid) _user = self._users_hash.get(_user_uuid) _conversation_data = self._conversation_user_datas_hash.get(_user_uuid) _count = 0 if _user.get("user_show_badge") == True: _key = MessagePush.__tablename__ + \ ".app_uuid." + _app_uuid + \ ".user_uuid." + _user_uuid + \ ".device_uuid." + _device_uuid _count = len(self._redis.smembers(_key)) _config = { "user_language": _user.get("user_language"), "unacked_notification_count": _count, "user_silence_notification": _user.get("user_silence_notification") } _mqtt_token = self._android_token(_user_uuid, _device_uuid) _gcm_token = _device.get("device_android_gcmtoken") if _device.get("device_use_gcmpush") == True and _gcm_token != None: _config["android_gcm_token"] = _gcm_token else: _config["android_mqtt_token"] = _mqtt_token, _push = { "config": _config, "body": self._task.get("message_body"), "app_uuid": _app_uuid } # logging.info("push android: %s" % str(_push)) if _config.get("android_gcm_token") != None: _gcm_config = BOOTSTRAP_DATA.get("gcm") _api_key = _gcm_config.get("api_key") if _api_key == None or len(_api_key) == 0: logging.info("gcmpush not start for gcm not config") return async_signal_gcmpush_push(_push) else : async_signal_mqttpush_push(_push) return
def get_handlers(cls): _root = os.path.abspath(os.path.dirname(__file__)) + "/ppkefu/www" _generic_store = BOOTSTRAP_DATA.get("server") _generic_store = _generic_store.get("generic_store") handlers = [ (r"^/$", MainHandler), (r"^/cordova.js$", CordovaHandler), (r"/js/(.*)", StaticFileHandler, {"path": _root + "/js"}), (r"/css/(.*)", StaticFileHandler, {"path": _root + "/css"}), (r"/fonts/(.*)", StaticFileHandler, {"path": _root + "/fonts"}), (r"/img/(.*)", StaticFileHandler, {"path": _root + "/img"}), (r"/download/(.*)", DownloadHandler, {"path": "/"}), (r"/icon/([^\/]+)?$", StaticFileHandler, {"path": _generic_store + os.path.sep}), (r"/material/([^\/]+)?$", MaterialFileHandler, {"path": _generic_store + os.path.sep}), (r"/upload/(.*)", UploadHandler), ] return handlers
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
import tornado.ioloop import tornado.options import tornado.httpserver import logging import sys tornado.options.define("port", default=IOSPUSH_PORT, help="", type=int) if __name__ == "__main__": tornado.options.parse_command_line() # FIXME: support more apns certifcate _apns = BOOTSTRAP_DATA.get("apns") if len(_apns) == 0: logging.info("apns not config, iospush can not start") sys.exit() _app = IOSPushApp() _http_server = tornado.httpserver.HTTPServer(_app) _http_server.listen(tornado.options.options.port) # set the periodic check outdated connection tornado.ioloop.PeriodicCallback(_app.outdate, 1000*60*5).start() # set the periodic check push request every 200 ms tornado.ioloop.PeriodicCallback(_app.push, 200).start()
def get_absolute_path(cls, root, path): _identicon_store = BOOTSTRAP_DATA.get("server") _identicon_store = _identicon_store.get("identicon_store") _p = _identicon_store + os.path.sep + path return _p
from ppmessage.bootstrap.data import BOOTSTRAP_DATA import tornado.httpserver import tornado.ioloop import tornado.options import logging import sys tornado.options.define("port", default=IOSPUSH_PORT, help="", type=int) if __name__ == "__main__": tornado.options.parse_command_line() _config = BOOTSTRAP_DATA.get("apns") _name = _config.get("name") _dev = _config.get("dev") _pro = _config.get("pro") if _name == None or len(_name) == 0 or _dev == None or len( _dev) == 0 or _pro == None or len(_pro) == 0: logging.info("apns not config, iospush can not start") sys.exit() _app = IOSPushApp() _io = BackendIO(getThread(), _app) _http_server = tornado.httpserver.HTTPServer(_app) _http_server.listen(tornado.options.options.port) # set the periodic check outdated connection
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
""" callback will run when loop check """ return True def outdate(self): _delta = datetime.timedelta(seconds=30) if self._gcm_client == None: return self._gcm_client.outdate(_delta) return if __name__ == "__main__": tornado.options.parse_command_line() _config = BOOTSTRAP_DATA.get("gcm") _api_key = _config.get("api_key") if _api_key == None or len(_api_key) == 0: logging.info("No gcm api_key config, gcmpush can not start.") sys.exit() _app = GcmPushApp(getWeb()) _io = BackendIO(getThread(), _app) _http_server = tornado.httpserver.HTTPServer(_app) _http_server.listen(tornado.options.options.port) logging.info("Starting gcmpush service......%d" % GCMPUSH_PORT) # set the periodic check outdated connection tornado.ioloop.PeriodicCallback(_app.outdate, 1000*30).start() tornado.ioloop.IOLoop.instance().start()
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 _push_to_ios(self, _user_uuid, _device_uuid): logging.info("push ios %s:%s" % (_user_uuid, _device_uuid)) _apns_config = BOOTSTRAP_DATA.get("apns") _apns_name = _apns_config.get("name") _apns_dev = _apns_config.get("dev") _apns_pro = _apns_config.get("pro") if _apns_name == None or len(_apns_name) == 0 or \ _apns_dev == None or len(_apns_dev) == 0 or \ _apns_pro == None or len(_apns_pro) == 0: logging.info("iospush not start for no apns config") return _app_uuid = self._task["app_uuid"] _user = self._users_hash.get(_user_uuid) _device = self._devices_hash.get(_device_uuid) _conversation_data = self._conversation_user_datas_hash.get(_user_uuid) if _user == None: logging.error("push ios failed for no user") return if _device == None: logging.error("push ios failed for no device") return _token = _device.get("device_ios_token") if _token == None or len(_token) == 0: logging.error("push ios failed for no ios token") return if _device["device_ios_token"] == IOS_FAKE_TOKEN: logging.error("push ios failed for fake token") return if _conversation_data != None and _conversation_data["user_mute_notification"] == True: # user only do not want recv push for this conversation logging.error("push ios failed for silence required") return _count = 0 if _user.get("user_show_badge") == True: _key = MessagePush.__tablename__ + \ ".app_uuid." + _app_uuid + \ ".user_uuid." + _user_uuid + \ ".device_uuid." + _device_uuid _count = self._redis.zcard(_key) _is_dev = bool(_device.get("is_development")) _config = { "is_development": _is_dev, "user_language": _user.get("user_language"), "device_ios_token": _token, "unacked_notification_count": _count, "user_silence_notification": _user.get("user_silence_notification") } _push = { "config": _config, "body": self._task.get("message_body"), "app": _app_uuid } logging.info("push ios: %s" % str(_push)) self._redis.rpush(REDIS_IOSPUSH_KEY, json.dumps(_push)) return
""" callback will run when loop check """ return True def outdate(self): _delta = datetime.timedelta(seconds=30) if self._gcm_client == None: return self._gcm_client.outdate(_delta) return if __name__ == "__main__": tornado.options.parse_command_line() _config = BOOTSTRAP_DATA.get("gcm") _api_key = _config.get("api_key") if _api_key == None or len(_api_key) == 0: logging.info("No gcm api_key config, gcmpush can not start.") sys.exit() _app = GcmPushApp(getWeb()) _io = BackendIO(getThread(), _app) _http_server = tornado.httpserver.HTTPServer(_app) _http_server.listen(tornado.options.options.port) logging.info("Starting gcmpush service......%d" % GCMPUSH_PORT) # set the periodic check outdated connection tornado.ioloop.PeriodicCallback(_app.outdate, 1000 * 30).start() tornado.ioloop.IOLoop.instance().start()
("/request_kefu_auth", RequestKefuAuthHandler), ("/request_console_auth", RequestConsoleAuthHandler), ("/auth_callback", AuthCallbackHandler), ] super(App, self).__init__(handlers, **settings) return if __name__ == "__main__": server = getServer() API_URI = server + "/api" AUTH_URI = server + "/ppauth/auth" TOKEN_URI = server + "/ppauth/token" REDIRECT_URI = "http://localhost:8090/auth_callback" kefu = BOOTSTRAP_DATA.get("THIRD_PARTY_KEFU") KEFU_API_UUID = kefu.get("api_uuid") KEFU_CLIENT_ID = kefu.get("api_key") KEFU_CLIENT_SECRET = kefu.get("api_secret") console = BOOTSTRAP_DATA.get("THIRD_PARTY_CONSOLE") CONSOLE_API_UUID = console.get("api_uuid") CONSOLE_CLIENT_ID = console.get("api_key") CONSOLE_CLIENT_SECRET = console.get("api_secret") tornado.options.define("port", default=8090, help="", type=int) tornado.options.parse_command_line() app = App() http_server = tornado.httpserver.HTTPServer(app) http_server.listen(tornado.options.options.port) logging.info("Starting Auth Callback servcie.")
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