async def unlink(file_id: objectid.ObjectId): """Unlink a file.""" coll = db.Collection('fs.files') doc = await coll.find_one_and_update(filter={'_id': file_id}, update={'$inc': {'metadata.link': -1}}, return_document=ReturnDocument.AFTER) if doc and not doc['metadata']['link']: fs = db.GridFS('fs') await fs.delete(file_id)
async def unlink(file_id: objectid.ObjectId): """Unlink a file.""" coll = db.Collection('fs.files') doc = await coll.find_and_modify(query={'_id': file_id}, update={'$inc': { 'metadata.link': -1 }}, new=True) if not doc['metadata']['link']: fs = db.GridFS('fs') await fs.delete(file_id)
async def add(): """Add a file. Returns MotorGridIn.""" fs = db.GridFS('fs') return await fs.new_file(metadata={'link': 1})
async def get(file_id): """Get a file. Returns MotorGridOut.""" fs = db.GridFS('fs') return await fs.get(file_id)
async def add(content_type): """Add a file. Returns MotorGridIn.""" fs = db.GridFS('fs') secret = pwhash.gen_secret() return await fs.new_file(content_type=content_type, metadata={'link': 1, 'secret': secret})