Repository: ocr-d/core
Issue: resmgr download fails if server does not provide content-length

https://github.com/OCR-D/core/blob/71d295ac1fccbeb4164e230bd584e1920b9ab3c8/ocrd/ocrd/cli/resmgr.py#L132

This fails with [main.tar.gz](https://github.com/OCR-D/ocrd_tesserocr/blob/0427f6f31070bdc782a27e71cd6ac88142da9924/ocrd_tesserocr/ocrd-tool.json#L282-L289) in ocrd-tesserocr-recognize.

(In this case, the `size` is already known by the registered resource, but it should even work without that. So we have a bug and another one behind it.)

Changed files:
ocrd/ocrd/resource_manager.py, ocrd/ocrd/cli/resmgr.py

diff --git a/ocrd/ocrd/cli/resmgr.py b/ocrd/ocrd/cli/resmgr.py
index 5849d451..a2ddddf4 100644
--- a/ocrd/ocrd/cli/resmgr.py
+++ b/ocrd/ocrd/cli/resmgr.py
@@ -128,8 +128,9 @@ def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstal
                 continue
             if resdict['url'].startswith('https://') or resdict['url'].startswith('http://'):
                 log.info("Downloading %s resource '%s' (%s)", registered, resdict['name'], resdict['url'])
-                with requests.get(resdict['url'], stream=True) as r:
-                    resdict['size'] = int(r.headers.get('content-length'))
+                if 'size' not in resdict:
+                    with requests.head(resdict['url']) as r:
+                        resdict['size'] = int(r.headers.get('content-length', 0))
             else:
                 log.info("Copying %s resource '%s' (%s)", registered, resdict['name'], resdict['url'])
                 urlpath = Path(resdict['url'])
@@ -158,7 +159,6 @@ def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstal
                     resource_type=resdict.get('type', resource_type),
                     path_in_archive=resdict.get('path_in_archive', path_in_archive),
                     overwrite=overwrite,
-                    size=resdict['size'],
                     no_subdir=location in ['cwd', 'module'],
                     basedir=basedir,
                     progress_cb=lambda delta: bar.update(delta)
diff --git a/ocrd/ocrd/resource_manager.py b/ocrd/ocrd/resource_manager.py
index 6fb14000..27c968dc 100644
--- a/ocrd/ocrd/resource_manager.py
+++ b/ocrd/ocrd/resource_manager.py
@@ -222,12 +222,11 @@ class OcrdResourceManager():
             return Path(name).stem
         raise ValueError("No such usage '%s'" % usage)
 
-    def _download_impl(self, url, filename, progress_cb=None, size=None):
+    def _download_impl(self, url, filename, progress_cb=None):
         log = getLogger('ocrd.resource_manager._download_impl')
         log.info("Downloading %s to %s" % (url, filename))
         with open(filename, 'wb') as f:
             with requests.get(url, stream=True) as r:
-                total = size if size else int(r.headers.get('content-length'))
                 for data in r.iter_content(chunk_size=4096):
                     if progress_cb:
                         progress_cb(len(data))
@@ -273,7 +272,6 @@ class OcrdResourceManager():
         resource_type='file',
         path_in_archive='.',
         progress_cb=None,
-        size=None,
     ):
         """
         Download a resource by URL
@@ -297,7 +295,7 @@ class OcrdResourceManager():
         elif resource_type == 'archive':
             with pushd_popd(tempdir=True) as tempdir:
                 if is_url:
-                    self._download_impl(url, 'download.tar.xx', progress_cb, size)
+                    self._download_impl(url, 'download.tar.xx', progress_cb)
                 else:
                     self._copy_impl(url, 'download.tar.xx', progress_cb)
                 Path('out').mkdir()