@@ -242,25 +242,28 @@ async def file_response(scope, send, file_path: Path) -> None:
242242 """Send a file in chunks."""
243243
244244 # Make sure the file exists
245- if not os .path .exists ( file_path ):
245+ if not await asyncio . to_thread ( os .path .exists , file_path ):
246246 await simple_response (send , 404 , "File not found." )
247247 return
248248
249249 # Make sure it's a file
250- if not os .path .isfile ( file_path ):
250+ if not await asyncio . to_thread ( os .path .isfile , file_path ):
251251 await simple_response (send , 400 , "Not a file." )
252252 return
253253
254254 # Check if the file is already cached by the client
255255 modified_since = await get_val_from_header (scope , b"if-modified-since" )
256- if modified_since and modified_since > os .path .getmtime (file_path ):
256+ if modified_since and modified_since > await asyncio .to_thread (
257+ os .path .getmtime , file_path
258+ ):
257259 await simple_response (send , 304 , "Not modified." )
258260 return
259261
260262 # Get the file's MIME type
261263 mime_type = (
262- DEFAULT_MIME_TYPES .get (file_path .rsplit ("." )[1 ], None )
263- or mimetypes .guess_type (file_path , strict = False )[0 ]
264+ MIME_TYPES .get (file_path .rsplit ("." )[1 ], None )
265+ # Fallback to guess_type to allow for the user to define custom MIME types on their system
266+ or (await asyncio .to_thread (mimetypes .guess_type , file_path , strict = False ))[0 ]
264267 )
265268 if mime_type is None :
266269 mime_type = "text/plain"
0 commit comments