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

File type detection now looks inside the file to determine the type, in addition to using the file extension #3542

Merged
merged 4 commits into from
Jan 31, 2022

Conversation

eug3nix
Copy link
Contributor

@eug3nix eug3nix commented Jan 13, 2022

Hi.
This is my attempt at addressing the issue mentioned in bug #3481

I have used a pure-python library for file type detection instead of libmagic binding in order to not introduce extra complexity to the build process and possible issues with other platforms (although libmagic is far superior in its robustness and the amount of supported formats).

Here are some notes on the implementation:
1 - The guess_media_type function was changed so that it tries to open the file from the provided path
2 - If it is able to open the file and detect its type it checks if the extension parsed from the path (extA) is not matching the detected extension (extB)
3 - Then it checks if the extA is a synonym of extB by looking at the synonyms_map, if extB is NOT a synonym, then extA is replaced.

The rest of the code stays the same.
Note0: The extra logic described in step 3 is to handle edge cases when the internals format of the file has an extension which differs from the current file extension but still the current extension is legit. This allows the new code to keep formats such as .cbz (internally a .zip) or cbr (internally a .rar) from being overridden.

Note1: This function may potentially fail with relative file paths in case if the CWD of lbrynet process is not the same as the user's working dir (this is my guess, I have not tested this tbh.)

Please let me know what you think, does this solution is potentially worth of merging, should I add tests or do some other improvements?

Cheers,
Eugene

@kauffj
Copy link
Member

kauffj commented Jan 17, 2022

Thanks @eug3nix, review coming shortly!

@kauffj kauffj requested a review from eukreign January 17, 2022 16:36
@lbry-bot lbry-bot assigned eukreign and unassigned eukreign Jan 17, 2022
Copy link
Member

@eukreign eukreign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR.

Two overall comments:

  1. this definitely will need tests
  2. avoid redundant comments. use comments judiciously, only in cases where the code cannot be written clearer without a noticeable performance penalty. comments are not a replacement for writing good, legible, code. use clearer variable names if necessary.

I've also added some inline feedback.

lbry/schema/mime_types.py Outdated Show resolved Hide resolved
lbry/schema/mime_types.py Outdated Show resolved Hide resolved
lbry/schema/mime_types.py Show resolved Hide resolved
@lbry-bot lbry-bot assigned eukreign and unassigned eug3nix Jan 29, 2022
removes unnecessary comments
@eug3nix
Copy link
Contributor Author

eug3nix commented Jan 29, 2022

I've also added a number of tests to cover all cases I could imagine, please take a look @eukreign
Thanks!

@lbry-bot lbry-bot assigned eukreign and unassigned eukreign Jan 31, 2022
@eukreign eukreign assigned eug3nix and unassigned eukreign Jan 31, 2022
@eukreign eukreign changed the title Gh 3481 file type detection file type detection now looks inside the file to determine the type, in addition to using the file extension Jan 31, 2022
@eukreign eukreign assigned eukreign and unassigned eug3nix Jan 31, 2022
@eukreign eukreign added area: files type: improvement Existing (or partially existing) functionality needs to be changed labels Jan 31, 2022
@eukreign eukreign merged commit a1abd94 into lbryio:master Jan 31, 2022
@jackrobison jackrobison changed the title file type detection now looks inside the file to determine the type, in addition to using the file extension File type detection now looks inside the file to determine the type, in addition to using the file extension Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: files type: improvement Existing (or partially existing) functionality needs to be changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants