Working on streaming download files and or range selection for file downloads

This commit is contained in:
Scott Idem
2023-08-17 20:16:39 -04:00
parent 925760b13d
commit bea4975e7e
2 changed files with 117 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
import datetime, os, pathlib, time
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, StreamingResponse
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
@@ -240,6 +240,7 @@ async def event_file_lookup(
async def download_event_file(
event_file_id: str = Query(..., min_length=11, max_length=22),
filename: str = Query(None, min_length=4, max_length=100),
streaming: bool = False,
commons: Common_Route_Params = Depends(common_route_params),
):
@@ -280,10 +281,17 @@ async def download_event_file(
if os.path.exists(file_path_w_subdir):
log.info('Hosted file found on server.')
return FileResponse(file_path_w_subdir, filename=filename)
if streaming:
log.warning('Streaming!!!')
def iterfile(): #
with open(file_path_w_subdir, mode="rb") as file_like: #
yield from file_like #
return StreamingResponse(iterfile(), media_type='video/mp4')
else:
return FileResponse(file_path_w_subdir, filename=filename)
else:
log.error(f'The hosted file was not found on the server. Hash: {hash_sha256}')
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f'The hosted file was not found on the server. Hash: {hash_sha256}') # Not Found
return mk_resp(data=None, status_code=404, response=commons.response, status_message=f'The hosted file was not found on the server. Hash: {hash_sha256}') # Not Found
# ### END ### API Event File ### download_event_file() ###