fix: Prefer magic bytes to determine extraction logic#1541
Conversation
Extracting files uses the filename to determine the MIME type and thus the crates to invoke for extracting the file. This causes an error if the file has an incorrect extension, e.g., `.tar` instead of `.tar.gz`. Fixing the file extension works, but it is a bad user experience, especially compared to other file managers (Dolphin, Nautilus) and utilities (tar) that succeed in the same scenario. When extracting a file, try to determine the file type by looking at its magic bytes. If that logic fails for any reason, fallback to the filename logic. Checking magic bytes is technically slower than simple path logic, but this is only invoked on a single file when the user has confirmed they want to extract it anyway. This logic is _not_ used in any other scenario, e.g. determining icons to show. Fixes pop-os#1522
|
We already have mime type detection, can that be reused? |
With some extra effort, it can! One option is to copy that snippet into the app, but it's a bit noisy because it would also require us to populate the file metadata argument. This is the reason I went with the It's not a hill I'll die on, but just for context! |
|
The reason I used xdg-mime for mime detection is that it can use external and upgradeable mime databases. |
That's fair. The I'll update the PR tonight to work with |
Quick update, I have it working with only the
I'll open an issue with them to discuss making magic byte preference easier. We'll see how it goes! |
I have updated the code to address the feedback:
I opened ebassi/xdg-mime-rs#37 with some suggestions regarding magic byte detection. This is not blocking, however; it would simply reduce the amount of code we have in our side. |
|
Merged with master. It looks like my bug on xdg-mime-rs hasn't had any interaction, so I do not think that we will get the reduced code in this PR any time soon. Still works and fixes the bug, though! |
Extracting files uses the filename to determine the MIME type and thus the crates to invoke for extracting the file. This causes an error if the file has an incorrect extension, e.g.,
.tarinstead of.tar.gz.Fixing the file extension works, but it is a bad user experience, especially compared to other file managers (Dolphin, Nautilus) and utilities (tar) that succeed in the same scenario.
When extracting a file, try to determine the file type by looking at its magic bytes. If that logic fails for any reason, fallback to the filename logic.
Checking magic bytes is technically slower than simple path logic, but this is only invoked on a single file when the user has confirmed they want to extract it anyway. This logic is not used in any other scenario, e.g. determining icons to show.
Fixes #1522