Skip to content

Commit 6f1eba5

Browse files
authored
fix: use a temp filename to save files with download_file func (#398)
1 parent 9612b06 commit 6f1eba5

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

openfoodfacts/utils/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import gzip
33
import json
44
import logging
5+
import random
56
import shutil
7+
import string
68
import time
79
from io import BytesIO
810
from pathlib import Path
@@ -189,7 +191,9 @@ def download_file(url: str, output_path: Path):
189191
r = http_session.get(url, stream=True)
190192
etag = r.headers.get("ETag", "").strip("'\"")
191193

192-
tmp_output_path = output_path.with_name(output_path.name + ".part")
194+
# add a random string to the output path to avoid concurrent writes
195+
suffix = "".join(random.choices(string.ascii_letters, k=8))
196+
tmp_output_path = output_path.with_name(output_path.name + f"-{suffix}.part")
193197
with (
194198
tmp_output_path.open("wb") as f,
195199
tqdm.tqdm(

0 commit comments

Comments
 (0)