Search code examples
pythoneve

getting gridfs file in insert hook


I am making a file indexing service using eve.

All my collections are of the type:

DOMAIN = {
"file_collection" : {
    ...
    'schema' : {
        'file': {'type': 'media'},
        'meta': {'type': 'dict',
            schema: {...whatever it is I want to index...}
            }
        }
    }, 
}

I would like to use an on_insert hook to open the file , extract some data and populate this document's meta dictionary.

I have not been able to access or find any gridFS object in the eve application object: How can I access the document contents?

Thanks!


Solution

  • Ok, this is what I learnt during my subsequent research:

    from eve import Eve
    from flask import current_app 
    from eve.io.mongo.media import GridFSMediaStorage
    
    
    def file_collection_on_insert(documents):
        gridfs = GridFSMediaStorage(app=current_app)
        #do something with gridfs: eg: gridfs.get(file_id)
    
    app = Eve()
    app.on_insert += file_collection_on_insert
    app.run()
    

    ===================

    Edit:

    Event quicker and easier access to media:

    from flask import current_app as app
    
    def file_collection_on_insert(documents):
        #get the file_id
        app.media.get(file_id)
    

    Voilà!