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

stream does not work properly on opened small files #96

Closed
FelipeLema opened this issue Jul 30, 2024 · 2 comments
Closed

stream does not work properly on opened small files #96

FelipeLema opened this issue Jul 30, 2024 · 2 comments

Comments

@FelipeLema
Copy link

FelipeLema commented Jul 30, 2024

using the following image (icon-32.png)
icon-32

the following code fails

import puremagic

with open(r"icon-32.png", "rb") as file:
    print(puremagic.magic_stream(file))

here's the error

Traceback (most recent call last):
  File "/home/…/./playing-with-puremagic", line 4, in <module>
    print(puremagic.magic_stream(file))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/…/.venv/lib/python3.12/site-packages/puremagic/main.py", line 351, in magic_stream
    head, foot = _stream_details(stream)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/…/.venv/lib/python3.12/site-packages/puremagic/main.py", line 229, in _stream_details
    stream.seek(-max_foot, os.SEEK_END)
OSError: [Errno 22] Invalid argument

not sure why, but maybe it's because it's trying to seek() to a position that's bigger than its size?

@FelipeLema FelipeLema changed the title stream does not work properly stream does not work properly on opened small files Jul 30, 2024
@NebularNerd
Copy link
Contributor

NebularNerd commented Aug 5, 2024

I think this is related to my issue on #86

Converting your image to a .jpg works, converting it to a .webp produces the same errors. It seems to be an issue with certain formats, especially small file sizes. Doing a basic:

with open(r"icon-32.png", "rb") as file:
    print(file.read())

Shows the file can be read and displayed, so as you guessed it's like it keeps trying to read something not there.

JPG and WEBP.zip

EDIT
OK, this looks like an issue when calculating the footer, the .png is 198 bytes and the .webp is 280, while the .jpg is 815 bytes which is longer than the longest footer record of 516 bytes.

The culprit is an entry I added for MAC .dmg format in the footer which is 512 bytes, removing that (mostly) solves the problem. So, the footer calculator needs some better logic to handle short files. Time to break out the thinking grenades....

YknCuX

EDIT 2
I have a fix which should do the trick, PR incoming...

@cdgriffith cdgriffith mentioned this issue Aug 8, 2024
cdgriffith added a commit that referenced this issue Aug 8, 2024
- Adding new verbose output to command line with `-v` or `--verbose`
- Adding #92 include py.typed in sdist (thanks to Nicholas Bollweg - bollwyvl)
- Adding #93 Improve PDF file detection, fix json description (thanks to Péter - peterekepeter)
- Fixing #96 #86 stream does not work properly on opened small files (thanks to Felipe Lema and Andy - NebularNerd)
- Removing expected invalid WinZip signature

---------

Co-authored-by: Nicholas Bollweg <[email protected]>
Co-authored-by: Péter <[email protected]>
Co-authored-by: Andy <[email protected]>
@cdgriffith
Copy link
Owner

Thanks for the fixes, addressed in 1.27!

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

No branches or pull requests

3 participants