1
1
import contextlib
2
2
import datetime
3
3
import hmac
4
+ import io
4
5
import json
5
6
import os
6
7
from pathlib import Path
@@ -577,44 +578,40 @@ async def _download_file(
577
578
for _ in range (10 ):
578
579
size = 0
579
580
hash = utils .get_hash_obj (file .hash )
580
- with tempfile .NamedTemporaryFile (
581
- dir = self ._cache_dir ,
582
- delete = False
583
- ) as tmp_file :
584
- try :
585
- async with session .get (
586
- file .path
587
- ) as resp :
588
- while (data := await resp .content .read (1024 * 1024 * 16 )):
589
- tmp_file .write (data )
590
- hash .update (data )
591
- inc = len (data )
592
- size += inc
593
- self ._pbar .update (inc )
594
- pbar .update (inc )
595
- if hash .hexdigest () != file .hash or size != file .size :
596
- await anyio .sleep (50 )
597
- raise Exception (f"hash mismatch, got { hash .hexdigest ()} expected { file .hash } " )
598
- await self .upload_storage (file , tmp_file , size )
599
- self .update_success ()
600
- except Exception as e :
601
- last_error = e
602
- self ._pbar .update (- size )
603
- pbar .update (- size )
604
- self .update_failed ()
605
- continue
606
- finally :
607
- tmp_file .close ()
608
- os .remove (tmp_file .name )
609
- return None
581
+ tmp_file = io .BytesIO ()
582
+ try :
583
+ async with session .get (
584
+ file .path
585
+ ) as resp :
586
+ while (data := await resp .content .read (1024 * 1024 * 16 )):
587
+ tmp_file .write (data )
588
+ hash .update (data )
589
+ inc = len (data )
590
+ size += inc
591
+ self ._pbar .update (inc )
592
+ pbar .update (inc )
593
+ if hash .hexdigest () != file .hash or size != file .size :
594
+ await anyio .sleep (50 )
595
+ raise Exception (f"hash mismatch, got { hash .hexdigest ()} expected { file .hash } " )
596
+ await self .upload_storage (file , tmp_file , size )
597
+ self .update_success ()
598
+ except Exception as e :
599
+ last_error = e
600
+ self ._pbar .update (- size )
601
+ pbar .update (- size )
602
+ self .update_failed ()
603
+ continue
604
+ finally :
605
+ tmp_file .close ()
606
+ return None
610
607
if last_error is not None :
611
608
raise last_error
612
609
613
610
614
611
async def upload_storage (
615
612
self ,
616
613
file : BMCLAPIFile ,
617
- tmp_file : 'tempfile._TemporaryFileWrapper' ,
614
+ data : io . BytesIO ,
618
615
size : int
619
616
):
620
617
missing_storage = [
@@ -623,11 +620,11 @@ async def upload_storage(
623
620
if len (missing_storage ) == 0 :
624
621
return
625
622
for storage in missing_storage :
626
- tmp_file .seek (0 )
623
+ data .seek (0 )
627
624
retries = 0
628
625
while 1 :
629
626
try :
630
- await storage .storage .upload_download_file (f"{ file .hash [:2 ]} /{ file .hash } " , tmp_file , size )
627
+ await storage .storage .upload_download_file (f"{ file .hash [:2 ]} /{ file .hash } " , data , size )
631
628
break
632
629
except :
633
630
if retries >= 10 :
0 commit comments