codeacme17 / examor

For students, scholars, interviewees and lifelong learners. Let LLMs assist you in learning 🎓
GNU Affero General Public License v3.0
1.07k stars 71 forks source link

[BUG]: upload note return server internal error #24

Closed zephyrpathsofglory closed 1 year ago

zephyrpathsofglory commented 1 year ago

Description of the bug

I uploaded a md file but the server returned 500, and the following is the log of server:

INFO:     192.168.228.4:48302 - "POST /note HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 289, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 273, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 190, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/main.py", line 63, in add_note
    return await _apis_.note.add_note(language, questionType, noteName, files, notionId)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/apis/note.py", line 62, in add_note
    if (len(files) > 3):
        ^^^^^^^^^^
TypeError: object of type 'NoneType' has no len()

Environment:

Macos 14 Beta, M1 chip
OpenAI Key with API_BASE setup
codeacme17 commented 1 year ago

@zephyrpathsofglory, Have you made sure you passed in the file? If possible, can you send me the request body so that I can better locate the error. Thanks your issue!

zephyrpathsofglory commented 1 year ago

@zephyrpathsofglory, Have you made sure you passed in the file? If possible, can you send me the request body so that I can better locate the error. Thanks your issue!

This is the request body:

Screenshot 2023-09-02 at 16 36 02

It seems that my file has not uploaded?

zephyrpathsofglory commented 1 year ago
Screenshot 2023-09-02 at 16 40 30

It is weird that it shows only .md type files are allowed to be uploaded but what I uploaded is a md file.

codeacme17 commented 1 year ago

This is the request body: Screenshot 2023-09-02 at 16 36 02

It seems that my file has not uploaded?

Yes you are not passing file in your request body. I'm curious how this happens, because in the front-end form, the upload button cannot be clicked without selecting a file.

codeacme17 commented 1 year ago

You can try to pull the latest code, and then try to upload note to see if this happens again. it's really weird.

zephyrpathsofglory commented 1 year ago

I have pulled the latest code, rebuilt the docker images, and restarted the docker-compose servers. But still got the same error.

codeacme17 commented 1 year ago

I have pulled the latest code, rebuilt the docker images, and restarted the docker-compose servers. But still got the same error.

okay, I think the problem should be in the frontend, give me some time to find where the problem is.

codeacme17 commented 1 year ago

@zephyrpathsofglory, hi, I should know what the problem is, is the MIME type of the file you uploaded text/markdown or text/x-markdown? It will report an error if you don't upload the exact file type. Of course I'm sorry for not terminating subsequent operations when uploading the wrong file.

zephyrpathsofglory commented 1 year ago

The uploaded file is a README file with md extension downloaded from github. The mime type of this file is text/html(get by executing file --mime-type -b $file)

codeacme17 commented 1 year ago

Yeah its not allowed. detectLegalFile You can see the logic in this code. I have modified the logic of not terminating the upload after reporting an error and will update it immediately.

zephyrpathsofglory commented 1 year ago

Yeah its not allowed. detectLegalFile You can see the logic in this code. I have modified the logic of not terminating the upload after reporting an error and will update it immediately.

Why not check file type by file extensions?

codeacme17 commented 1 year ago

Yeah its not allowed. detectLegalFile You can see the logic in this code. I have modified the logic of not terminating the upload after reporting an error and will update it immediately.

Why not check file type by file extensions?

Because MIME types provide more accurate information about the file's content, are more secure, have better cross-platform compatibility, and are less susceptible to manipulation by malicious users.

zephyrpathsofglory commented 1 year ago

Yeah its not allowed. detectLegalFile You can see the logic in this code. I have modified the logic of not terminating the upload after reporting an error and will update it immediately.

Why not check file type by file extensions?

Because MIME types provide more accurate information about the file's content, are more secure, have better cross-platform compatibility, and are less susceptible to manipulation by malicious users.

Thank you, but not all md file is text/markdown. It will fail the upload with a good chance.

codeacme17 commented 1 year ago

What you said is correct. I will add more type judgments after trying it later. Thank you very much for your issue and follow! If you have any suggestions in the future, you can also submit an issue or PR ~