@@ -250,9 +250,7 @@ def _get_file_path_stat_encoding(
250
250
251
251
# Fallback to the uncompressed file
252
252
st = file_path .stat ()
253
- if not S_ISREG (st .st_mode ):
254
- return None , st , None
255
- return file_path , st , None
253
+ return file_path if S_ISREG (st .st_mode ) else None , st , None
256
254
257
255
async def prepare (self , request : "BaseRequest" ) -> Optional [AbstractStreamWriter ]:
258
256
loop = asyncio .get_running_loop ()
@@ -307,12 +305,12 @@ async def _prepare_open_file(
307
305
file_encoding : Optional [str ],
308
306
) -> Optional [AbstractStreamWriter ]:
309
307
status = self ._status
310
- file_size = st .st_size
311
- count = file_size
312
- start = None
308
+ file_size : int = st .st_size
309
+ file_mtime : float = st .st_mtime
310
+ count : int = file_size
311
+ start : Optional [int ] = None
313
312
314
- ifrange = request .if_range
315
- if ifrange is None or st .st_mtime <= ifrange .timestamp ():
313
+ if (ifrange := request .if_range ) is None or file_mtime <= ifrange .timestamp ():
316
314
# If-Range header check:
317
315
# condition = cached date >= last modification date
318
316
# return 206 if True else 200.
@@ -323,7 +321,7 @@ async def _prepare_open_file(
323
321
try :
324
322
rng = request .http_range
325
323
start = rng .start
326
- end = rng .stop
324
+ end : Optional [ int ] = rng .stop
327
325
except ValueError :
328
326
# https://tools.ietf.org/html/rfc7233:
329
327
# A server generating a 416 (Range Not Satisfiable) response to
@@ -340,7 +338,7 @@ async def _prepare_open_file(
340
338
341
339
# If a range request has been made, convert start, end slice
342
340
# notation into file pointer offset and count
343
- if start is not None or end is not None :
341
+ if start is not None :
344
342
if start < 0 and end is None : # return tail of file
345
343
start += file_size
346
344
if start < 0 :
@@ -398,25 +396,23 @@ async def _prepare_open_file(
398
396
self ._compression = False
399
397
400
398
self .etag = f"{ st .st_mtime_ns :x} -{ st .st_size :x} " # type: ignore[assignment]
401
- self .last_modified = st . st_mtime # type: ignore[assignment]
399
+ self .last_modified = file_mtime # type: ignore[assignment]
402
400
self .content_length = count
403
401
404
402
self ._headers [hdrs .ACCEPT_RANGES ] = "bytes"
405
403
406
- real_start = cast (int , start )
407
-
408
404
if status == HTTPPartialContent .status_code :
405
+ real_start = start
406
+ assert real_start is not None
409
407
self ._headers [hdrs .CONTENT_RANGE ] = "bytes {}-{}/{}" .format (
410
408
real_start , real_start + count - 1 , file_size
411
409
)
412
410
413
411
# If we are sending 0 bytes calling sendfile() will throw a ValueError
414
- if count == 0 or must_be_empty_body (request .method , self . status ):
412
+ if count == 0 or must_be_empty_body (request .method , status ):
415
413
return await super ().prepare (request )
416
414
417
- if start : # be aware that start could be None or int=0 here.
418
- offset = start
419
- else :
420
- offset = 0
415
+ # be aware that start could be None or int=0 here.
416
+ offset = start or 0
421
417
422
418
return await self ._sendfile (request , fobj , offset , count )
0 commit comments