Skip to content

Commit 09e0732

Browse files
author
Krawabbel
committed
more efficient handling of downloaded data
1 parent fc97e4f commit 09e0732

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

cps/editbooks.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from .usermanagement import user_login_required, login_required_if_no_ano
5050
from .string_helper import strip_whitespaces
5151

52-
import tempfile
52+
import io
5353
import requests
5454
from werkzeug.datastructures import FileStorage
5555

@@ -566,23 +566,20 @@ def do_upload_url(url: str) -> FileStorage:
566566
filename_sanitized = "".join(c for c in url_end if c.isalnum() or c in "._-") # sanitize filename
567567
filename = filename_sanitized if len(filename_sanitized) > 0 else "uploaded_file"
568568

569-
with tempfile.NamedTemporaryFile(delete=False) as tmp:
570-
for chunk in r.iter_content():
571-
tmp.write(chunk)
572-
tmp.flush()
573-
tmp_name = tmp.name
569+
file_buffer = io.BytesIO()
570+
for chunk in r.iter_content():
571+
file_buffer.write(chunk)
572+
file_buffer.seek(0)
573+
574+
return do_upload_file_list([FileStorage(file_buffer, filename=filename)])
575+
574576

575577
except Exception as e:
576578
log.error_or_exception("File download error: {}".format(e))
577579
flash(_("Oops! Download failed: %(error)s.", error=e.orig if hasattr(e, "orig") else e),
578580
category="error")
579581
return make_response(jsonify(location=url_for("web.index")))
580582

581-
with open(tmp_name, 'rb') as f:
582-
file_storage = FileStorage(f, filename=filename)
583-
resp = do_upload_file_list([file_storage])
584-
585-
return resp
586583

587584
def do_upload_file_list(file_list: list[FileStorage]):
588585
for requested_file in file_list:

0 commit comments

Comments
 (0)