janeczku / calibre-web

:books: Web app for browsing, reading and downloading eBooks stored in a Calibre database
GNU General Public License v3.0
13.19k stars 1.41k forks source link

Upload file = NameError: name 'magic' is not defined on MacOS #3098

Closed zeekaran closed 3 months ago

zeekaran commented 4 months ago

Attempting to upload a file immediately causes an error.

Steps to reproduce the behavior:

  1. Click upload button
  2. Select file
  3. Click open (or double click the file in previous step)
  4. Get redirected to 500 error page

Traceback (most recent call last):

File "/lsiopy/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app response = self.full_dispatch_request() File "/lsiopy/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request rv = self.handle_user_exception(e) File "/lsiopy/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() File "/lsiopy/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(view_args) File "/app/calibre-web/cps/usermanagement.py", line 35, in decorated_view return login_required(func)(*args, *kwargs) File "/lsiopy/lib/python3.10/site-packages/flask_login/utils.py", line 290, in decorated_view return current_app.ensure_sync(func)(args, kwargs) File "/app/calibre-web/cps/editbooks.py", line 59, in inner return f(*args, **kwargs) File "/app/calibre-web/cps/editbooks.py", line 249, in upload meta, error = file_handling_on_upload(requested_file) File "/app/calibre-web/cps/editbooks.py", line 755, in file_handling_on_upload if not validate_mime_type(requested_file, allowed_extensions): File "/app/calibre-web/cps/file_helper.py", line 49, in validate_mime_type mime = magic.Magic(mime=True)

NameError: name 'magic' is not defined

I'm using Nginx as a reverse proxy, but that probably isn't relevant since it does the same thing from localhost:port.

The last time I uploaded a file was May 28th, just over a month ago.

enaloha commented 4 months ago

You beat me to it. Same error here without reverse proxy. Running on MacOS Sonoma, everything latest version.

zeekaran commented 4 months ago

You beat me to it. Same error here without reverse proxy. Running on MacOS Sonoma, everything latest version.

Release notes have this at the top:

!Attention! For this update regex, bleach and python-magic has to be installed !Attention!

So that's almost certainly why. I don't remember updating, but since I'm on a version that came out two days ago, clearly I did. A mystery.

enaloha commented 4 months ago

Thank you for the quick reply. I thought I did everything right, but I think I might need help. I actually did install all 3 modules using (pip install regex), (pip install bleach), and (pip install python-magic). These are taken staright from the Terminal: Successfully installed regex-2024.5.15 Successfully installed bleach-6.1.0 webencodings-0.5.1 Successfully installed python-magic-0.4.27 I restarted the Mac for good measure and Calibre-Web runs just fine, except that I still get the dreaded 500 Internal Error. Is there anything else I may have missed?

zeekaran commented 4 months ago

Thank you for the quick reply. I thought I did everything right, but I think I might need help. I actually did install all 3 modules using (pip install regex), (pip install bleach), and (pip install python-magic). These are taken staright from the Terminal: Successfully installed regex-2024.5.15 Successfully installed bleach-6.1.0 webencodings-0.5.1 Successfully installed python-magic-0.4.27 I restarted the Mac for good measure and Calibre-Web runs just fine, except that I still get the dreaded 500 Internal Error. Is there anything else I may have missed?

I am about to be gone for a week and needed it working asap, so I decided to be extra lazy and fix it by pointing to 0.6.21 instead of latest and installing extra stuff on my server. Your guess is as good as mine, best of luck!

enaloha commented 4 months ago

Not sure if this helps, but here is the error trace upon attempting to upload a file:

Calibre-Web

500 Internal Server Error

The server encountered an internal error and was unable to complete your request. There is an error in the application.

Traceback (most recent call last):

  File "/opt/calibre-web/venv/lib/python3.11/site-packages/flask/app.py", line 2073, in wsgi_app

    response = self.full_dispatch_request()

                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/venv/lib/python3.11/site-packages/flask/app.py", line 1519, in full_dispatch_request

    rv = self.handle_user_exception(e)

         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/venv/lib/python3.11/site-packages/flask/app.py", line 1517, in full_dispatch_request

    rv = self.dispatch_request()

         ^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/venv/lib/python3.11/site-packages/flask/app.py", line 1503, in dispatch_request

    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/cps/usermanagement.py", line 35, in decorated_view

    return login_required(func)(*args, **kwargs)

            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/venv/lib/python3.11/site-packages/flask_login/utils.py", line 290, in decorated_view

    return current_app.ensure_sync(func)(*args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/cps/editbooks.py", line 59, in inner

    return f(*args, **kwargs)

           ^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/cps/editbooks.py", line 249, in upload

    meta, error = file_handling_on_upload(requested_file)

                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/cps/editbooks.py", line 755, in file_handling_on_upload

    if not validate_mime_type(requested_file, allowed_extensions):

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/calibre-web/cps/file_helper.py", line 49, in validate_mime_type

    mime = magic.Magic(mime=True)

          ^^^^^

NameError: name 'magic' is not defined
OzzieIsaacs commented 4 months ago

@enaloha: Could you please update to the newest nightly version and check your calibre-web.log. I guess there will be an entry: "Cannot import python-magic, checking uploaded file metadata will not work: failed to find libmagic. Check your installation"

The easy workaround is to deactivate "Check if file extensions matches file content on upload" in admin -> basic configuration -> security settings". But I would really appreciate it if you could help me to fix the problem. It's most likely related to this one: https://github.com/ahupp/python-magic/issues/238

OzzieIsaacs commented 4 months ago

@enaloha: If I',m right then the solution could be this one: https://github.com/ahupp/python-magic/blob/master/README.md#osx

(In this case I would add a message in the log pointing to the solution)

enaloha commented 4 months ago

I switched to nightly builds (current version is now 7/8/24 3:04 AM. The weblog does NOT include "Cannot import python-magic, checking uploaded file metadata will not work: failed to find libmagic. Check your installation". The only text related to magic is pasted below:

File "/opt/calibre-web/cps/file_helper.py", line 49, in validate_mime_type

mime = magic.Magic(mime=True)
       ^^^^^        

NameError: name 'magic' is not defined

OzzieIsaacs commented 4 months ago

I think I know why, the message is logged before the logging routine is properly set up. could you please check if this works for you: https://github.com/ahupp/python-magic/blob/master/README.md#osx ( the environment variable tip)

enaloha commented 4 months ago

I just reinstalled libmagic via brew and that fixed the issue. I am now able to upload books. Thanks for your help.

ianfights commented 4 months ago

A windows solution can be found here #2875 for those still having this issue

(edit) adding this because this is the top google result for this error

String0Fellow commented 4 months ago

I ran into this issue on Windows 10. Switching to Python 3.9 and installing the DLLs for libmagic with pip install python-magic-bin fixed it.