whiskyechobravo / kerkoapp

A web application that provides a faceted search interface for bibliographies managed with Zotero.
https://whiskyechobravo.github.io/kerko/
BSD 3-Clause "New" or "Revised" License
68 stars 24 forks source link

Accessing PDF attachments of publications cause HTTP 500 #11

Closed drm-nz closed 3 years ago

drm-nz commented 4 years ago

Hi There,

We have just noticed that if a publication has a PDF file attachment (presumably any attachment) then the file link returns an HTTP 500. When I reproduce the issue on the Web Server itself, I get the following error instead of the default HTTP 500 page:

Error occurred:

Traceback (most recent call last):
  File "D:\Websites\KerkoBibliography\wfastcgi.py", line 847, in main
    result = handler(record.params, response.start)
  File "C:\Python38\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python38\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python38\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python38\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Python38\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python38\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python38\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python38\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Python38\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python38\lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python38\lib\site-packages\kerko\views.py", line 154, in item_attachment_download
    return send_from_directory(
  File "C:\Python38\lib\site-packages\flask\helpers.py", line 771, in send_from_directory
    return send_file(filename, **options)
  File "C:\Python38\lib\site-packages\flask\helpers.py", line 647, in send_file
    rv.last_modified = last_modified
  File "C:\Python38\lib\site-packages\werkzeug\_internal.py", line 259, in __set__
    value = self.dump_func(value)
  File "C:\Python38\lib\site-packages\werkzeug\http.py", line 872, in http_date
    return _dump_date(timestamp, " ")
  File "C:\Python38\lib\site-packages\werkzeug\http.py", line 819, in _dump_date
    d = gmtime(d)
OSError: [Errno 22] Invalid argument

StdOut: 

StdErr: 

It appears to me that, wherever parameter "d" is coming from, it is not in the expected format or of the expected value type. Changing line 819 in "C:\Python38\lib\site-packages\werkzeug\http.py" from "d = gmdate(d)" to "d = gmdate()" makes it immediately working. This makes me believe that either an additional check is missing from the if else block or the check "isinstance(d, (integer_types, float))" is not suitable to determine how to handle parameter "d".

Thanks, Laszlo

davidlesieur commented 4 years ago

PDF attachments usually work fine in Kerko, so you must have stumbled on something new or unusual. So just by changing the gmtime() call, the file downloads normally? Could you tell me what is the version of Werkzeug in your environment, and the type and value of d when the error happens?

drm-nz commented 4 years ago

That is correct, changing it to gmtime() fixes it immediately. I guess the only downside of this is that the last modified time of the downloaded file will change every single time you download it. It will be the time of the download instead of the actual time when the file was last modified. This is with kerkoapp v0.6, I believe werkzeug is 0.16.0. Not sure what the value is but it's one of the integer_types (int or long). Cheers.

davidlesieur commented 4 years ago

I'm wondering about the actual value of d because it might tell a bit more about what's happening (the problem may not be in Werkzeug, but upstream in Kerko or Flask). I'm not able to reproduce the problem. Have you tried the latest Werkzeug and Flask versions, in case it is a known issue that might have been fixed?

davidlesieur commented 3 years ago

Closing this for inactivity. Please report back if the issue persists with the latest package versions. A couple of bug fixes related to attachments were made in Kerko 0.7.