Beispiel #1
0
def delete_file(attr, value):
    """ Delete files in GridFS

    Args:
        attr (str): The attribute to retrieve.
        vale (str): Value of the attribute.

    Returns:
        success - True
        not found - False

    Raises:
        Exception

    >>> delete_file('name', 'test')
    """
    try:
        gridfs = connect_gridfs()
    except Exception as e:
        logger.debug(e)
        raise
    else:
        files = gridfs.find({attr: value})
        if files.count() > 0:
            for f in files:
                gridfs.delete(f._id)
            return True
        else:
            return False
Beispiel #2
0
def get_compressed_file(attr, value):
    """ Get a compressed file cursor that attribute equals specific value from GridFS

    Args:
        attr (str): The attribute to retrieve.
        vale (str): Value of the attribute.
 
    Returns:
        success - an instance of StringIO
        not found - None
 
    Raises:
        Exception

    >>> get_compressed_file('name', 'test')
    """
    try:
        gridfs = connect_gridfs()
    except Exception as e:
        logger.debug(e)
        raise
    else:
        files = gridfs.find({attr: value})
        if files.count() > 0:
            zipdata = StringIO()
            zipfp = zipfile.ZipFile(zipdata, mode='w')
            if files[0].filename:
                filename = files[0].filename
            else:
                filename = getattr(files[0], 'sha256', files[0].md5)
            zipfp.writestr(filename, files[0].read())
            zipfp.close()
            zipdata.seek(0)
            return zipdata
        else:
            return None