Example #1
0
 def _parse_TEXT(self):
     if len(self._message_body) > MESSAGE_MAX_TEXT_LEN:
         _fid = create_file_with_data(self._redis, self._message_body,
                                      "text/plain", self._from_uuid)
         self._message_subtype = MESSAGE_SUBTYPE.TXT
         self._message_body = json.dumps({"fid": _fid})
     return True
Example #2
0
    def parse(self):
        self._message_type = self._message_type.upper()
        self._message_subtype = self._message_subtype.upper()
        if isinstance(self._message_body, unicode):
            self._message_body = self._message_body.encode("utf-8")

        if self._message_subtype == MESSAGE_SUBTYPE.TEXT:
            if len(self._message_body) > MESSAGE_MAX_TEXT_LEN:
                _fid = create_file_with_data(self._redis, self._message_body,
                                             "text/plain", self._from_uuid)
                self._message_body = json.dumps({"fid": _fid})
            return True

        elif self._message_subtype == MESSAGE_SUBTYPE.TXT:
            _fid = self._parseTxt(self._message_body)
            if _fid == None:
                return False
            self._message_body = json.dumps({"fid": _fid})
            return True

        elif self._message_subtype == MESSAGE_SUBTYPE.AUDIO:
            _audio = self._parseAudio(self._message_body)
            if _audio == None:
                return False
            self._message_body = _audio
            return True

        elif self._message_subtype == MESSAGE_SUBTYPE.IMAGE:
            _image = self._parseImage(self._message_body)
            if _image == None:
                return False
            self._message_body = json.dumps(_image)
            return True

        elif self._message_subtype == MESSAGE_SUBTYPE.VIDEO:
            _video = self._parseVideo(self._message_body)
            if _video == None:
                return False
            self._message_body = json.dumps(_video)
            return True

        elif self._message_subtype == MESSAGE_SUBTYPE.DOCUMENT:
            _document = self._parseDocument(self._message_body)
            if _document == None:
                return False
            self._message_body = json.dumps(_document)
            return True

        elif self._message_subtype == MESSAGE_SUBTYPE.FILE:
            _generic = self._parseFile(self._message_body)
            if _generic == None:
                return False
            self._message_body = json.dumps(_generic)
            return True

        else:
            logging.error("unsupport message: %s" % self._body)
            return False

        return True
Example #3
0
    def parse(self):
        self._message_type = self._message_type.upper()
        self._message_subtype = self._message_subtype.upper()
        if isinstance(self._message_body, unicode):
            self._message_body = self._message_body.encode("utf-8")

        if self._message_subtype == MESSAGE_SUBTYPE.TEXT:
            if len(self._message_body) > MESSAGE_MAX_TEXT_LEN:
                _fid = create_file_with_data(self._redis, self._message_body, "text/plain", self._from_uuid)
                self._message_body = json.dumps({"fid": _fid})
            return True
        
        elif self._message_subtype == MESSAGE_SUBTYPE.TXT:
            _fid = self._parseTxt(self._message_body)
            if _fid == None:
                return False
            self._message_body = json.dumps({"fid": _fid})
            return True
        
        elif self._message_subtype == MESSAGE_SUBTYPE.AUDIO:
            _audio = self._parseAudio(self._message_body)
            if _audio == None:
                return False
            self._message_body = _audio
            return True
            
        elif self._message_subtype == MESSAGE_SUBTYPE.IMAGE:
            _image = self._parseImage(self._message_body)
            if _image == None:
                return False
            self._message_body = json.dumps(_image)
            return True
            
        elif self._message_subtype == MESSAGE_SUBTYPE.VIDEO:
            _video = self._parseVideo(self._message_body)
            if _video == None:
                return False
            self._message_body = json.dumps(_video)
            return True
        
        elif self._message_subtype == MESSAGE_SUBTYPE.DOCUMENT:
            _document = self._parseDocument(self._message_body)
            if _document == None:
                return False
            self._message_body = json.dumps(_document)
            return True
        
        elif self._message_subtype == MESSAGE_SUBTYPE.FILE:
            _generic = self._parseFile(self._message_body)
            if _generic == None:
                return False
            self._message_body = json.dumps(_generic)
            return True
        
        else:
            logging.error("unsupport message: %s" % self._body)
            return False

        return True
Example #4
0
    def _parseImage(self, _body):
        _image = json.loads(_body)

        _fid = _image.get("fid")
        _mime = _image.get("mime")

        if _fid == None or _mime == None:
            logging.error("Error for message body of image message")
            return None
        
        _mime = _mime.lower()
        if _mime not in ["image/jpg", "image/jpeg", "image/png", "image/gif"]:
            logging.error("Error for not supported mime=%s." % (_mime))
            return None

        _file = redis_hash_to_dict(self._redis, FileInfo, _fid)
        if _file == None:
            logging.error("Error for no file in redis: %s" % _fid)
            return None

        _image = None
        try:
            # raise IOError when file not image
            _image = Image.open(_file["file_path"])
        except:
            pass
        finally:
            if _image == None:
                logging.error("PIL can not identify the file_id=%s, not image." % (_fid))
                return None

        _image_width = _image.width
        _image_height = _image.height
        _thum_width = _image.width
        _thum_height = _image.height
        
        if _image.format == "GIF":
            return {"thum":_fid, "orig":_fid, "mime":"image/gif", "orig_width": _image_width, "orig_height": _image_height, "thum_width": _thum_width, "_thum_height": _thum_height}
        
        _thum_format = "JPEG"
        if _image.format == "PNG":
            _thum_format = "PNG"

        _thum_image_info = ImageConverter.thumbnailByKeepImage(_image, _thum_format)
        _thum_data = _thum_image_info["data"]
        _thum_image = _thum_image_info["image"]
        if _thum_data == None:
            logging.error("Error for thumbnail image")
            return None

        _thum_id = create_file_with_data(self._redis, _thum_data, _mime, self._from_uuid)

        _thum_width = _thum_image.width
        _thum_height = _thum_image.height

        # where assume the _thum must be jpeg
        return {"thum":_thum_id, "orig":_fid, "mime":_mime, "orig_width": _image_width, "orig_height": _image_height, "thum_width": _thum_width, "thum_height": _thum_height}
Example #5
0
    def _parseImage(self, _body):
        _image = json.loads(_body)

        _fid = _image.get("fid")
        _mime = _image.get("mime")

        if _fid == None or _mime == None:
            logging.error("Error for message body of image message")
            return None
        
        _mime = _mime.lower()
        if _mime not in ["image/jpg", "image/jpeg", "image/png", "image/gif"]:
            logging.error("Error for not supported mime=%s." % (_mime))
            return None

        _file = redis_hash_to_dict(self._redis, FileInfo, _fid)
        if _file == None:
            logging.error("Error for no file in redis: %s" % _fid)
            return None

        _image = None
        try:
            # raise IOError when file not image
            _image = Image.open(_file["file_path"])
        except:
            pass
        finally:
            if _image == None:
                logging.error("PIL can not identify the file_id=%s, not image." % (_fid))
                return None

        _image_width, _image_height = _image.size
        _thum_width = _image_width
        _thum_height = _image_height
        
        if _image.format == "GIF":
            return {"thum":_fid, "orig":_fid, "mime":"image/gif", "orig_width": _image_width, "orig_height": _image_height, "thum_width": _thum_width, "thum_height": _thum_height}
        
        _thum_format = "JPEG"
        if _image.format == "PNG":
            _thum_format = "PNG"

        _thum_image_info = ImageConverter.thumbnailByKeepImage(_image, _thum_format)
        _thum_data = _thum_image_info["data"]
        _thum_image = _thum_image_info["image"]
        if _thum_data == None:
            logging.error("Error for thumbnail image")
            return None

        _thum_id = create_file_with_data(self._redis, _thum_data, _mime, self._from_uuid)

        _thum_width, _thum_height = _thum_image.size

        # where assume the _thum must be jpeg
        return {"thum":_thum_id, "orig":_fid, "mime":_mime, "orig_width": _image_width, "orig_height": _image_height, "thum_width": _thum_width, "thum_height": _thum_height}
Example #6
0
    def _parseAudio(self, _body):
        _redis = self._redis
        _audio = json.loads(_body)

        _duration = _audio.get("dura")
        _mime = _audio.get("mime")
        _fid = _audio.get("fid")

        if _duration == None or _fid == None or _mime == None:
            logging.error("Error parse audio message body failed.")
            return None

        # m4a is from/for iOS
        # amr is from/for android
        # mp3 is for PC
        _data = read_file(_redis, _fid)
        if _data == None:
            logging.error("Error no audio data %s." % (_fid))
            return None

        _mp3 = None

        if _mime == "audio/m4a":
            _m4a = AudioConverter.m4a2m4a(_data)
            _amr = AudioConverter.m4a2amr(_data)
            _mp3 = AudioConverter.m4a2mp3(_data)

            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a", self._from_uuid)
            _fid_amr = create_file_with_data(_redis, _amr, "audio/amr", self._from_uuid)
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3", self._from_uuid)

        if _mime == "audio/amr":
            _m4a = AudioConverter.amr2m4a(_data)
            _amr = _data
            _mp3 = AudioConverter.amr2mp3(_data)

            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a", self._from_uuid)
            _fid_amr = _fid
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3", self._from_uuid)


        if _mp3 == None:
            logging.error("Error no audio converter for mime=%s." % (_mime))
            return None

        if _fid_m4a == None:
            logging.error("Error to create m4a file with data, len=%d." % len(_m4a))
            return None

        if _fid_amr == None:
            logging.error("Error to create amr file with data, len=%d." % len(_amr))
            return None

        if _fid_mp3 == None:
            logging.error("Error to create mp3 file with data, len=%d." % len(_mp3))
            return None

        return json.dumps({
            "m4a": {"dura": _duration, "fid": _fid_m4a},
            "amr": {"dura": _duration, "fid": _fid_amr},
            "mp3": {"dura": _duration, "fid": _fid_mp3}
        })
Example #7
0
    def _parseAudio(self, _body):
        from ppmessage.core.audioconverter import AudioConverter

        _redis = self._redis

        _audio = json.loads(_body)
        if "dura" not in _audio or "fid" not in _audio or "mime" not in _audio:
            logging.error("Error parse audio message body failed.")
            return None

        _duration = _audio["dura"]
        _mime = _audio["mime"]
        _fid = _audio["fid"]

        # m4a is from/for iOS
        # amr is from/for android
        # mp3 is for PC
        _data = read_file(_redis, _fid)
        if _data == None:
            logging.error("Error no audio data %s." % (_fid))
            return None

        _mp3 = None
        _m4a = None
        _amr = None

        _fid_mp3 = None
        _fid_m4a = None
        _fid_amr = None

        if _mime == "audio/m4a" or "audio/m4a" in _mime:
            _m4a = AudioConverter.m4a2m4a(_data)
            _amr = AudioConverter.m4a2amr(_data)
            _mp3 = AudioConverter.m4a2mp3(_data)

            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a",
                                             self._from_uuid)
            _fid_amr = create_file_with_data(_redis, _amr, "audio/amr",
                                             self._from_uuid)
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3",
                                             self._from_uuid)

        if _mime == "audio/amr":
            _amr = _data
            _m4a = AudioConverter.amr2m4a(_data)
            _mp3 = AudioConverter.amr2mp3(_data)

            _fid_amr = _fid
            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a",
                                             self._from_uuid)
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3",
                                             self._from_uuid)

        if _mp3 == None:
            logging.error("Error no audio converter for mime=%s." % (_mime))
            return None

        if _fid_m4a == None:
            logging.error("Error to create m4a file with data, len=%d." %
                          len(_m4a))
            return None

        if _fid_amr == None:
            logging.error("Error to create amr file with data, len=%d." %
                          len(_amr))
            return None

        if _fid_mp3 == None:
            logging.error("Error to create mp3 file with data, len=%d." %
                          len(_mp3))
            return None

        return {
            "m4a": {
                "dura": _duration,
                "fid": _fid_m4a
            },
            "amr": {
                "dura": _duration,
                "fid": _fid_amr
            },
            "mp3": {
                "dura": _duration,
                "fid": _fid_mp3
            }
        }
Example #8
0
    def _parseAudio(self, _body):
        _redis = self._redis
        _audio = json.loads(_body)

        _duration = _audio.get("dura")
        _mime = _audio.get("mime")
        _fid = _audio.get("fid")

        if _duration == None or _fid == None or _mime == None:
            logging.error("Error parse audio message body failed.")
            return None

        # m4a is from/for iOS
        # amr is from/for android
        # mp3 is for PC
        _data = read_file(_redis, _fid)
        if _data == None:
            logging.error("Error no audio data %s." % (_fid))
            return None

        _mp3 = None

        if _mime == "audio/m4a":
            _m4a = AudioConverter.m4a2m4a(_data)
            _amr = AudioConverter.m4a2amr(_data)
            _mp3 = AudioConverter.m4a2mp3(_data)

            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a",
                                             self._from_uuid)
            _fid_amr = create_file_with_data(_redis, _amr, "audio/amr",
                                             self._from_uuid)
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3",
                                             self._from_uuid)

        if _mime == "audio/amr":
            _m4a = AudioConverter.amr2m4a(_data)
            _amr = _data
            _mp3 = AudioConverter.amr2mp3(_data)

            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a",
                                             self._from_uuid)
            _fid_amr = _fid
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3",
                                             self._from_uuid)

        if _mp3 == None:
            logging.error("Error no audio converter for mime=%s." % (_mime))
            return None

        if _fid_m4a == None:
            logging.error("Error to create m4a file with data, len=%d." %
                          len(_m4a))
            return None

        if _fid_amr == None:
            logging.error("Error to create amr file with data, len=%d." %
                          len(_amr))
            return None

        if _fid_mp3 == None:
            logging.error("Error to create mp3 file with data, len=%d." %
                          len(_mp3))
            return None

        return json.dumps({
            "m4a": {
                "dura": _duration,
                "fid": _fid_m4a
            },
            "amr": {
                "dura": _duration,
                "fid": _fid_amr
            },
            "mp3": {
                "dura": _duration,
                "fid": _fid_mp3
            }
        })
Example #9
0
    def _parseAudio(self, _body):
        from ppmessage.core.audioconverter import AudioConverter

        _redis = self._redis
        
        _audio = json.loads(_body)
        if "dura" not in _audio or "fid" not in _audio or "mime" not in _audio:
            logging.error("Error parse audio message body failed.")
            return None

        _duration = _audio["dura"]
        _mime = _audio["mime"]
        _fid = _audio["fid"]

        # m4a is from/for iOS
        # amr is from/for android
        # mp3 is for PC
        _data = read_file(_redis, _fid)
        if _data == None:
            logging.error("Error no audio data %s." % (_fid))
            return None

        _mp3 = None
        _m4a = None
        _amr = None

        _fid_mp3 = None
        _fid_m4a = None
        _fid_amr = None

        if _mime == "audio/m4a" or "audio/m4a" in _mime:
            _m4a = AudioConverter.m4a2m4a(_data)
            _amr = AudioConverter.m4a2amr(_data)
            _mp3 = AudioConverter.m4a2mp3(_data)

            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a", self._from_uuid)
            _fid_amr = create_file_with_data(_redis, _amr, "audio/amr", self._from_uuid)
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3", self._from_uuid)

        if _mime == "audio/amr":
            _amr = _data
            _m4a = AudioConverter.amr2m4a(_data)
            _mp3 = AudioConverter.amr2mp3(_data)

            _fid_amr = _fid
            _fid_m4a = create_file_with_data(_redis, _m4a, "audio/m4a", self._from_uuid)
            _fid_mp3 = create_file_with_data(_redis, _mp3, "audio/mp3", self._from_uuid)


        if _mp3 == None:
            logging.error("Error no audio converter for mime=%s." % (_mime))
            return None

        if _fid_m4a == None:
            logging.error("Error to create m4a file with data, len=%d." % len(_m4a))
            return None

        if _fid_amr == None:
            logging.error("Error to create amr file with data, len=%d." % len(_amr))
            return None

        if _fid_mp3 == None:
            logging.error("Error to create mp3 file with data, len=%d." % len(_mp3))
            return None

        return {
            "m4a": {"dura": _duration, "fid": _fid_m4a},
            "amr": {"dura": _duration, "fid": _fid_amr},
            "mp3": {"dura": _duration, "fid": _fid_mp3}
        }
Example #10
0
 def _parse_TEXT(self):
     if len(self._message_body) > MESSAGE_MAX_TEXT_LEN:
         _fid = create_file_with_data(self._redis, self._message_body, "text/plain", self._from_uuid)
         self._message_subtype = MESSAGE_SUBTYPE.TXT
         self._message_body = json.dumps({"fid": _fid})
     return True