Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zeekaran opened this issue Jul 7, 2024 · 12 comments
Closed

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

zeekaran opened this issue Jul 7, 2024 · 12 comments

Comments

@zeekaran
Copy link

zeekaran commented Jul 7, 2024

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

  • OS: Linux 5.10.0-28-amd64 Unable to create Admin user #1 SMP Debian 5.10.209-2 (2024-01-31) x86_64 x86_64
  • Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
  • Calibre-Web version: 0.6.22 - e9bccc8 - 2024-07-06T13:29:17+02:00
  • Docker container: lscr.io/linuxserver/calibre-web:latest
  • Browser: Any (Chrome, Firefox)

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
Copy link

enaloha commented Jul 7, 2024

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

@zeekaran
Copy link
Author

zeekaran commented Jul 8, 2024

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
Copy link

enaloha commented Jul 8, 2024

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
Copy link
Author

zeekaran commented Jul 8, 2024

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
Copy link

enaloha commented Jul 8, 2024

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
Copy link
Collaborator

@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: ahupp/python-magic#238

@OzzieIsaacs
Copy link
Collaborator

@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)

@OzzieIsaacs OzzieIsaacs added the bug label Jul 8, 2024
@OzzieIsaacs OzzieIsaacs changed the title Upload file = 500 Internal Server Error Upload file = NameError: name 'magic' is not defined on MacOS Jul 8, 2024
@enaloha
Copy link

enaloha commented Jul 8, 2024

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
Copy link
Collaborator

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
Copy link

enaloha commented Jul 8, 2024

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

@ianfights
Copy link

ianfights commented Jul 12, 2024

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants