Skip to content

Commit d3d1273

Browse files
committed
allow url_resolve_cb to run without dl_lock
1 parent 4949adb commit d3d1273

File tree

1 file changed

+56
-57
lines changed

1 file changed

+56
-57
lines changed

pyloader.py

+56-57
Original file line numberDiff line numberDiff line change
@@ -698,70 +698,69 @@ def _is_http_status_ok(req, progress):
698698
except FileExistsError:
699699
logger.warning('Directory already exists: {}'.format(_dir))
700700

701-
try:
702-
if self._url_resolve_cb is not None and dlable.resolve_url:
703-
# If we get a invalid url (or nothing), the requests
704-
# module will fail with a proper error-message.
705-
dlable = self.url_resolve_cb(dlable)
706-
707-
# Create requests object as stream
708-
req = _MyRequest(url=dlable.url,
709-
allow_redirects=dlable.allow_redirects,
710-
verify=dlable.verify_ssl,
711-
cookies=dlable.cookies,
712-
headers=dlable.headers)
713-
714-
except Exception:
715-
progress.status = Status.FAILED
716-
progress.error = traceback.format_exc()
717-
_propagate(progress)
701+
try:
702+
if self._url_resolve_cb is not None and dlable.resolve_url:
703+
# If we get a invalid url (or nothing), the requests
704+
# module will fail with a proper error-message.
705+
dlable = self.url_resolve_cb(dlable)
718706

719-
_finish(dlable, progress)
720-
return
707+
# Create requests object as stream
708+
req = _MyRequest(url=dlable.url,
709+
allow_redirects=dlable.allow_redirects,
710+
verify=dlable.verify_ssl, cookies=dlable.cookies,
711+
headers=dlable.headers)
721712

722-
progress.http_status = req.status_code
713+
except Exception:
714+
progress.status = Status.FAILED
715+
progress.error = traceback.format_exc()
716+
_propagate(progress)
723717

724-
if not _is_http_status_ok(req, progress):
725-
return
718+
_finish(dlable, progress)
719+
return
726720

727-
# Try to extract filename from headers if none was specified
728-
if not _file:
729-
_file = req.filename
730-
731-
# Try to get a filename from the url itself
732-
# We only use this approach if a file-extension is given to make
733-
# sure it is actually the filename
734-
if not _file:
735-
_tmp_name = unquote(os.path.basename(req.url))
736-
if os.path.splitext(_tmp_name)[1]:
737-
_file = _tmp_name
738-
739-
# Set filename to the downloadables uid as last resort
740-
if not _file:
741-
_file = dlable.uid
742-
743-
# Full path to the file we'll be writing to
744-
target = os.path.join(_dir, _file)
745-
746-
# Try and get the files content-length to calculate
747-
# a progress
748-
if dlable.content_length:
749-
content_length = dlable.content_length
750-
else:
751-
content_length = req.content_length
721+
progress.http_status = req.status_code
752722

753-
if content_length:
754-
content_length = int(content_length)
755-
progress.mb_total = content_length / 1024 / 1024
723+
if not _is_http_status_ok(req, progress):
724+
return
756725

757-
# Check if the same file already exists and skip if it does
758-
if (os.path.exists(target)
759-
and os.path.getsize(target) == content_length):
760-
progress.status = Status.EXISTED
761-
_propagate(progress)
726+
# Try to extract filename from headers if none was specified
727+
if not _file:
728+
_file = req.filename
729+
730+
# Try to get a filename from the url itself
731+
# We only use this approach if a file-extension is given to make
732+
# sure it is actually the filename
733+
if not _file:
734+
_tmp_name = unquote(os.path.basename(req.url))
735+
if os.path.splitext(_tmp_name)[1]:
736+
_file = _tmp_name
737+
738+
# Set filename to the downloadables uid as last resort
739+
if not _file:
740+
_file = dlable.uid
741+
742+
# Full path to the file we'll be writing to
743+
target = os.path.join(_dir, _file)
744+
745+
# Try and get the files content-length to calculate
746+
# a progress
747+
if dlable.content_length:
748+
content_length = dlable.content_length
749+
else:
750+
content_length = req.content_length
762751

763-
_finish(dlable, progress)
764-
return
752+
if content_length:
753+
content_length = int(content_length)
754+
progress.mb_total = content_length / 1024 / 1024
755+
756+
# Check if the same file already exists and skip if it does
757+
if (os.path.exists(target)
758+
and os.path.getsize(target) == content_length):
759+
progress.status = Status.EXISTED
760+
_propagate(progress)
761+
762+
_finish(dlable, progress)
763+
return
765764

766765
try:
767766
cancel = False

0 commit comments

Comments
 (0)