Exemplo n.º 1
0
    def delete_addon(self, url, params, request, response):
        """ delete addon file.

        [Arguments]
            url[0] : string.
                Addon version of file to remove.
        """
        # API args
        if len(url):
            version = url[0]
        else:
            raise RequestError(ERR_REQUEST_ARG, str_data=('version error.'))

        db_inst = PGClient(db='file', timeout=False)
        # check data exist
        cur, rowcount = db_inst.query(
            table='addons',
            fields=['version'],
            condition=[u'version=%(version)s', {
                'version': version
            }])
        if not rowcount:
            raise RequestError(ERR_REQUEST_ARG,
                               str_data=('data does not exist.'))

        db_inst.remove(
            table='addons',
            condition=[u'version=%(version)s', {
                'version': version
            }])
        db_inst.commit()

        return Response({'version': version})
Exemplo n.º 2
0
    def delete_addon(self, url, params, request, response):
        """ delete addon file.

        [Arguments]
            url[0] : string.
                Addon version of file to remove.
        """
        # API args
        if len(url):
            version = url[0]
        else:
            raise RequestError(ERR_REQUEST_ARG, str_data=('version error.'))

        db_inst = PGClient(db='file', timeout=False)
        # check data exist
        cur, rowcount = db_inst.query(
            table='addons', fields=['version'], condition=[u'version=%(version)s', {'version': version}]
        )
        if not rowcount:
            raise RequestError(ERR_REQUEST_ARG, str_data=('data does not exist.'))

        db_inst.remove(table='addons', condition=[u'version=%(version)s', {'version': version}])
        db_inst.commit()

        return Response({
            'version': version
        })
Exemplo n.º 3
0
    def upload_addon(self, url, params, request, response):
        """ Upload addon file.

        [Arguments]
            url[0] : string.
                Addon version of this file.
            md5sum : string. (opt)
                md5sum of this file to check data is complete.
        """
        # API args
        if len(url):
            version = url[0]
        else:
            raise RequestError(ERR_REQUEST_ARG, str_data=('version error.'))

        if not params.has_key('file'):
            raise RequestError(ERR_REQUEST_ARG, str_data=('file error.'))

        file_md5sum = params.get_string('md5sum', None)

        # check data exist
        db_inst = PGClient(db='file', timeout=False)
        cur, rowcount = db_inst.query(
            table='addons',
            fields=['version'],
            condition=[u'version=%(version)s', {
                'version': version
            }])
        if rowcount:
            raise RequestError(ERR_REQUEST_ARG, str_data=('data existed.'))

        # handle updating file
        upload_file = params['file']
        tmp_file_path = '%s/%s' % (TMP_PATH, upload_file.filename)

        try:
            # save file
            md5 = hashlib.md5()
            with open(tmp_file_path, mode='wb') as fp:
                while True:
                    data = upload_file.file.read(8192)
                    if data:
                        fp.write(data)
                        md5.update(data)
                    else:
                        break

            # compare md5sum
            md5sum = md5.hexdigest()
            if file_md5sum and md5sum != file_md5sum:
                raise RequestError(ERR_REQUEST_ARG,
                                   str_data=('md5sum check error.'))

            # insert data to db
            with open(tmp_file_path, mode='rb') as f:
                db_inst.insert(table='addons',
                               data={
                                   'version': version,
                                   'name': upload_file.filename,
                                   'data': Binary(f.read()),
                                   'md5sum': md5sum,
                                   'udate': datetime.utcnow().isoformat()
                               })

            # remove redundant data
            addons, rowcount = db_inst.query(table='addons',
                                             fields=['version'],
                                             ret_type='all',
                                             else_sql='ORDER BY version DESC')
            if rowcount > MAX_NUM_ADDONS:
                versions_to_remove = [
                    addons[-1 * index]['version']
                    for index in xrange(1, rowcount - MAX_NUM_ADDONS + 1)
                ]
                db_inst.remove(table='addons',
                               condition=[
                                   'version IN %(versions)s', {
                                       'versions': tuple(versions_to_remove)
                                   }
                               ])

            db_inst.commit()

            return Response({'version': version, 'md5sum': md5sum})
        finally:
            os.remove(tmp_file_path)
Exemplo n.º 4
0
    def upload_addon(self, url, params, request, response):
        """ Upload addon file.

        [Arguments]
            url[0] : string.
                Addon version of this file.
            md5sum : string. (opt)
                md5sum of this file to check data is complete.
        """
        # API args
        if len(url):
            version = url[0]
        else:
            raise RequestError(ERR_REQUEST_ARG, str_data=('version error.'))

        if not params.has_key('file'):
            raise RequestError(ERR_REQUEST_ARG, str_data=('file error.'))

        file_md5sum = params.get_string('md5sum', None)

        # check data exist
        db_inst = PGClient(db='file', timeout=False)
        cur, rowcount = db_inst.query(
            table='addons', fields=['version'], condition=[u'version=%(version)s', {'version': version}]
        )
        if rowcount:
            raise RequestError(ERR_REQUEST_ARG, str_data=('data existed.'))

        # handle updating file
        upload_file = params['file']
        tmp_file_path = '%s/%s' % (TMP_PATH, upload_file.filename)

        try:
            # save file
            md5 = hashlib.md5()
            with open(tmp_file_path, mode='wb') as fp:
                while True:
                    data = upload_file.file.read(8192)
                    if data:
                        fp.write(data)
                        md5.update(data)
                    else:
                        break

            # compare md5sum
            md5sum = md5.hexdigest()
            if file_md5sum and md5sum != file_md5sum:
                raise RequestError(ERR_REQUEST_ARG, str_data=('md5sum check error.'))

            # insert data to db
            with open(tmp_file_path, mode='rb') as f:
                db_inst.insert(table='addons', data={
                    'version': version,
                    'name': upload_file.filename,
                    'data': Binary(f.read()),
                    'md5sum': md5sum,
                    'udate': datetime.utcnow().isoformat()
                })

            # remove redundant data
            addons, rowcount = db_inst.query(
                table='addons', fields=['version'], ret_type='all', else_sql='ORDER BY version DESC'
            )
            if rowcount > MAX_NUM_ADDONS:
                versions_to_remove = [addons[-1*index]['version'] for index in xrange(1, rowcount-MAX_NUM_ADDONS+1)]
                db_inst.remove(
                    table='addons', condition=['version IN %(versions)s', {'versions': tuple(versions_to_remove)}]
                )

            db_inst.commit()

            return Response({
                'version': version,
                'md5sum': md5sum
            })
        finally:
            os.remove(tmp_file_path)