コード例 #1
0
def check_files_status(db):
    for file in os.listdir(config['DATA_DIR']):
        if file.endswith(tuple(config["ALLOWED_IMAGE_EXTENSIONS"])):
            if db.search_by_ipfs_id(file.split(".")[0]) is None:
                os.remove(os.path.join(config['DATA_DIR'], file))
                logger.info(
                    'COMMAND %s Failed %s: %s' %
                    ('Sync', 'Memechain',
                     "Remove file from data folder (not exist in db)  %s." %
                     file))

    for meme in db.get_all_memes():
        if not os.path.isfile(
                os.path.join(config['DATA_DIR'],
                             meme["ipfs_id"] + "." + meme["imgformat"])):
            logger.info('COMMAND %s Failed %s: %s' %
                        ('Sync', 'Memechain',
                         "File not  found in data folder (exist in db)  %s." %
                         meme["ipfs_id"]))
            ipfs = IPFSTools()
            ipfs.get_meme(meme["ipfs_id"], config['DATA_DIR'])
            logger.info('COMMAND %s Info %s: %s' %
                        ('Sync', 'Memechain',
                         "Try downloading missing file with id  %s." %
                         meme["ipfs_id"]))
コード例 #2
0
def sync_block(db, block):
    parser = MemechainParser(block)
    parser.collect_memetxs()

    memetxs = parser.return_memetxs()

    if memetxs:
        prev_block_memes = db.get_prev_block_memes()

        for meme in memetxs:
            memetx = MemeTx(meme['ipfs_id'])
            memetx.generate_hashlink(prev_block_memes)
            memetx.txid = meme['txid']

            Validate(memetx,
                     db=db,
                     ipfs_dir=config['DATA_DIR'],
                     prev_block_memes=prev_block_memes,
                     sync=True)

            if memetx.is_meme_valid():
                meme_filepath = IPFSTools().get_meme(meme['ipfs_id'],
                                                     config['DATA_DIR'])
                ext = meme_filepath.split(".")[-1]

                if ext in ["jpg", "png", "gif"]:
                    db.add_meme(
                        **{
                            "ipfs_id": meme['ipfs_id'],
                            "hashlink": meme['hashlink'],
                            "txid": meme['txid'],
                            "author": meme['author'],
                            "block": block,
                            "imgformat": ext
                        })

                    logger.info(
                        'COMMAND %s Success %s: %s' %
                        ('Sync', 'Memechain',
                         "Meme %s added to database." % meme['ipfs_id']))

                else:
                    # Delete invalid Meme
                    os.remove(meme_filepath)

                    logger.info('COMMAND %s Failed %s: %s' %
                                ('Sync', 'Memechain',
                                 "Invalid Meme image extension %s." % ext))

            else:
                logger.info('COMMAND %s Failed %s: %s' %
                            ('Sync', 'Memechain',
                             "Invalid MemeTx %s." % meme['ipfs_id']))

    else:
        logger.info(
            'COMMAND %s Failed %s: %s' %
            ('Sync', 'Memechain', "No Meme TXs found in block %s." % block))
コード例 #3
0
def sync_block(db, block):
    parser = MemechainParser(block)
    parser.collect_memetxs()
    memetxs = parser.return_memetxs()

    if memetxs:
        prev_block_memes = db.get_prev_block_memes()

        for meme in memetxs:
            memetx = MemeTx(meme['ipfs_id'])
            memetx.generate_hashlink(prev_block_memes)
            memetx.txid = meme['txid']
            Validate(memetx,
                     db=db,
                     ipfs_dir=config['DATA_DIR'],
                     prev_block_memes=prev_block_memes,
                     sync=True)
            valid_state = memetx.is_meme_valid()
            if valid_state == -1:
                meme_filepath = IPFSTools().get_meme(meme['ipfs_id'],
                                                     config['DATA_DIR'])
                ext = meme_filepath.split(".")[-1]

                if db.search_by_ipfs_id(meme['ipfs_id']):
                    db.update_meme(meme['ipfs_id'], block)
                    logger.info(
                        'COMMAND %s Success %s: %s' %
                        ('Sync', 'Memechain',
                         "Meme %s update in database." % meme['ipfs_id']))
                else:
                    db.add_meme(
                        **{
                            "ipfs_id": meme['ipfs_id'],
                            "hashlink": meme['hashlink'],
                            "txid": meme['txid'],
                            "author": meme['author'],
                            "block": block,
                            "imgformat": ext,
                            "status": "confirm"
                        })
                    logger.info(
                        'COMMAND %s Success %s: %s' %
                        ('Sync', 'Memechain',
                         "Meme %s added to database." % meme['ipfs_id']))
            elif valid_state != 3:
                meme_filepath = IPFSTools().get_meme(meme['ipfs_id'],
                                                     config['DATA_DIR'])
                os.remove(meme_filepath)
                logger.info('COMMAND %s Failed %s: %s' %
                            ('Sync', 'Memechain',
                             "Invalid MemeTx %s." % meme['ipfs_id']))

    else:
        if config["ENABLE_LOG_MEMTX_NOT_FOUND"]:
            logger.info('COMMAND %s Failed %s: %s' %
                        ('Sync', 'Memechain',
                         "No Meme TXs found in block %s." % block))
コード例 #4
0
    def on_post(self, req, resp):
        logger.info('COMMAND %s Received' % self.__class__.__name__)
        db = MemeChainDB(os.path.join(config['DATA_DIR'], 'memechain.json'))

        while check_running():
            time.sleep(3)
            logger.info('Waiting for the synchronization process to complete add_meme')


        # Generate random placeholder img name
        img_placeholder_name = str(random.random()).split(".")[1]

        ext = mimetypes.guess_extension(req.content_type)
        if ext == '.jpe':
            ext = '.jpg'


        name = '{img_name}{ext}'.format(img_name=img_placeholder_name, ext=ext)
        image_path = os.path.join(config['DATA_DIR'], name)


        # Write image to local storage
        with io.open(image_path, 'wb') as image_file:
            while True:
                chunk = req.stream.read(self._CHUNK_SIZE_BYTES)
                if not chunk:
                    break

                image_file.write(chunk)


        if magic.from_file(image_path).lower().find(mimetypes.guess_extension(req.content_type)[1:]) < 0:
            raise falcon.HTTPBadRequest( "Memechain error",
                                   "Meme has not passed validation, file extension is wrong.")


        # Check file size
        meme_filesize = os.path.getsize(image_path) * 0.000001 # in MB

        if meme_filesize > 10:
            os.remove(image_path)
            
            logger.error('COMMAND %s Failed %s: %s'
                         % (self.__class__.__name__, 'Upload Error',
                            "Meme filesize too large."))
            raise falcon.HTTPError(falcon.HTTP_400, "Upload error",
                                   "Meme filesize too large.")

        # Add image to ipfs
        ipfs_id = IPFSTools().add_meme(image_path)['Hash']
        # Rename local img file to ipfs_id for easy reference
        new_name = '{img_name}{ext}'.format(img_name=ipfs_id, ext=ext)
        os.rename(image_path, os.path.join(config['DATA_DIR'], new_name))
        # Add to Kekcoin chain
        memetx = MemeTx(ipfs_id)
        prev_block_memes = db.get_prev_block_memes()

        if prev_block_memes:
            memetx.generate_hashlink(prev_block_memes)
            try:
                Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'],
                         prev_block_memes=prev_block_memes)
            
            except TypeError as e:
                # Delete invalid Meme
                os.remove(os.path.join(config['DATA_DIR'], new_name))

                logger.error('COMMAND %s Failed %s: %s'
                             % (self.__class__.__name__, 'Memechain Error',
                                "Meme has not passed memechain validation, file extension not supported.%s") )
                raise falcon.HTTPError(falcon.HTTP_400, "Memechain error",
                                       "Meme has not passed validation, file extension not supported.%s" % e )

            if memetx.is_meme_valid():


                memetx.blockchain_write()

                db.add_meme(**{"ipfs_id": ipfs_id, "hashlink": memetx.get_hashlink(),
                               "txid": memetx.get_txid(), "author": memetx.get_author(), "block": 0, "imgformat": ext[1:],
                               "status": "unconfirm"})
                resp.status = falcon.HTTP_201
                resp.set_header('Powered-By', 'Memechain')

                logger.info('COMMAND %s Success' % self.__class__.__name__)
                resp.body = json.dumps({
                    'success': True,
                    'result': {'ipfs_id' : ipfs_id, 'txid' : memetx.get_txid(), 'hashlink' : memetx.get_hashlink(), 'author' : memetx.get_author()}})

            else:
                # Delete invalid Meme
                os.remove(os.path.join(config['DATA_DIR'], new_name))

                logger.error('COMMAND %s Failed %s: %s'
                             % (self.__class__.__name__, 'Memechain Error',
                                "Meme has not passed memechain validation: "))
                raise falcon.HTTPError(falcon.HTTP_400, "Memechain error",
                                       "Meme has not passed validation: ")
        else:
            # Genesis block logic
            memetx = MemeTx(ipfs_id)
            memetx.generate_genesis_hashlink()

            memetx.blockchain_write()

            resp.status = falcon.HTTP_201
            resp.set_header('Powered-By', 'Memechain')

            logger.info('COMMAND %s Success' % self.__class__.__name__)
            resp.body = json.dumps({
                'success': True,
                'result': {'ipfs_id' : ipfs_id, 'txid' : memetx.get_txid(), 'hashlink' : memetx.get_hashlink(), 'author' : memetx.get_author()}})
コード例 #5
0
ファイル: api.py プロジェクト: dynielsen/memechain-api
    def on_post(self, req, resp):
        logger.info('COMMAND %s Received' % self.__class__.__name__)
        db = MemeChainDB(os.path.join(config['DATA_DIR'], 'memechain.json'))

        # Generate random placeholder img name
        img_placeholder_name = random.random().split(".")[1]
        ext = mimetypes.guess_extension(req.content_type)
        name = '{img_name}{ext}'.format(img_name=img_placeholder_name, ext=ext)
        image_path = os.path.join(config['DATA_DIR'], name)

        # Write image to local storage
        with io.open(image_path, 'wb') as image_file:
            while True:
                chunk = req.stream.read(self._CHUNK_SIZE_BYTES)
                if not chunk:
                    break

                image_file.write(chunk)

        # Add image to ipfs
        ipfs_id = IPFSTools().add_meme(imgage_path)['Hash']

        # Rename local img file to ipfs_id for easy reference
        new_name = '{img_name}{ext}'.format(img_name=ipfs_id, ext=ext)
        os.rename(image_path, os.path.join(config['DATA_DIR'], new_name))

        # Add to Kekcoin chain
        memetx = MemeTx(ipfs_id)
        prev_block_memes = db.get_prev_block_memes()

        if prev_block_memes:
            memetx.generate_hashlink(prev_block_memes)

            Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'],
                     prev_block_memes=prev_block_memes)

            if memetx.is_meme_valid():
                memetx.blockchain_write()

                resp.status = falcon.HTTP_201
                resp.set_header('Powered-By', 'Memechain')

                logger.info('COMMAND %s Success' % self.__class__.__name__)
                resp.body = json.dumps({
                    'success': True,
                    'result': ipfs_id})

            else:
                logger.error('COMMAND %s Failed %s: %s'
                             % (self.__class__.__name__, 'Memechain Error',
                                "Meme has not passed memechain validation."))
                raise falcon.HTTPError(falcon.HTTP_400, "Memechain error",
                                       "Meme has not passed " +
                                       " validation.")

        else:
            # Genesis block logic
            memetx = MemeTx(ipfs_id)
            meme.generate_genesis_hashlink()

            memetx.blockchain_write()

            resp.status = falcon.HTTP_201
            resp.set_header('Powered-By', 'Memechain')

            logger.info('COMMAND %s Success' % self.__class__.__name__)
            resp.body = json.dumps({
                'success': True,
                'result': ipfs_id})