Skip to content

Commit

Permalink
Revert newer implementation of dms_api_blob_get (the newer one fails …
Browse files Browse the repository at this point in the history
…the tests)
  • Loading branch information
eudoxos committed Dec 13, 2024
1 parent 5df39e5 commit ba171ea
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions mupifDB/api/edm/dms3.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,12 +659,6 @@ def _handle_link(*,obj,index,key=key,path=path):
return _new_object(type,data,path=[],tracker=_ObjectTracker())


async def get_temp_dir():
tdir = tempfile.TemporaryDirectory(dir="/tmp", prefix='mupifDB')
try:
yield tdir.name
finally:
del tdir


@router.post('/{db}/blob/upload')
Expand All @@ -674,23 +668,35 @@ def dms_api_blob_upload(db: str, blob: fastapi.UploadFile) -> str:
return str(fs.put(blob.file, filename=blob.filename))


@router.get('/{db}/blob/{uid}')
def dms_api_blob_get(db: str, uid: str, tdir=Depends(get_temp_dir)):
fs = gridfs.GridFS(GG.db_get(db))
foundfile = fs.get(bson.objectid.ObjectId(uid))
wfile = io.BytesIO(foundfile.read())
fn = foundfile.filename
fullpath = tdir + '/' + fn
with open(fullpath, "wb") as f:
f.write(wfile.read())
f.close()
return fastapi.responses.FileResponse(path=fullpath, headers={"Content-Disposition": "attachment; filename=" + fn})

# 'Streaming blob download'
# fs=gridfs.GridFS(GG.db_get(db))
# def iterfile():
# yield from fs.get(bson.objectid.ObjectId(id))
# return fastapi.responses.StreamingResponse(iterfile(),media_type="application/octet-stream")
if 0:
# newer Stanislav's implementation, which is failing in unit tests
# I assume the tempdir gets destroyed when FileResponse is being returned, and it cannot find the file when attempting to stream it.
async def get_temp_dir():
tdir = tempfile.TemporaryDirectory(dir="/tmp", prefix='mupifDB')
try:
yield tdir.name
finally:
del tdir
@router.get('/{db}/blob/{uid}')
def dms_api_blob_get(db: str, uid: str, tdir=Depends(get_temp_dir)):
fs = gridfs.GridFS(GG.db_get(db))
foundfile = fs.get(bson.objectid.ObjectId(uid))
wfile = io.BytesIO(foundfile.read())
fn = foundfile.filename
fullpath = tdir + '/' + fn
with open(fullpath, "wb") as f:
f.write(wfile.read())
f.close()
return fastapi.responses.FileResponse(path=fullpath, headers={"Content-Disposition": "attachment; filename=" + fn})
else:
# this is the old implementation which passes the tests
@router.get('/{db}/blob/{uid}')
def dms_api_blob_get(db: str, uid: str):
'Streaming blob download'
fs=gridfs.GridFS(GG.db_get(db))
def iterfile():
yield from fs.get(bson.objectid.ObjectId(uid))
return fastapi.responses.StreamingResponse(iterfile(),media_type="application/octet-stream")


#
Expand Down

0 comments on commit ba171ea

Please sign in to comment.