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

Using custom file type with same extension but with different magic bytes throws InvalidOperationException on FileTypeValidator.GetFileType() #10

Closed
jim-craane opened this issue Nov 24, 2020 · 6 comments
Assignees
Labels
can-not-reproduce Reported bug that cant be reproduce from supporting developer

Comments

@jim-craane
Copy link

I added a custom file type for mp3 with different magic bytes '0xFF, 0xE3' then the already existing mp3 file type. When I try to get the file type via FileTypeValidator.GetFileType() for this custom file type, I get the following exception:

System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at FileTypeChecker.FileTypeValidator.GetFileType(Stream fileContent)

Its failing on line 74 of FileTypeValidator.cs
image

Suggestion:
Support custom file types with same extensions, but with different magic bytes.

@AJMitev AJMitev self-assigned this Nov 24, 2020
@AJMitev
Copy link
Owner

AJMitev commented Nov 24, 2020

@jim-craane The problem is self defined into the error message. There are two file types that match the particular stream content. This is not wanted behavior. Please be sure that your custom class does not contains the bytes array from library mp3 filetype.

@jim-craane
Copy link
Author

It does not contain the bytes from the already existing mp3 filetype:
image
image

@AJMitev
Copy link
Owner

AJMitev commented Nov 24, 2020

There is something wrong here. I am already implemented this file extension into my project and using the sample you provided into issue #9 all current and new tests are passed. Cant reproduce this behavior. New version including this file type and some others will be released today.

@AJMitev AJMitev closed this as completed Nov 24, 2020
@AJMitev AJMitev added the can-not-reproduce Reported bug that cant be reproduce from supporting developer label Nov 24, 2020
@jim-craane
Copy link
Author

Yeah you are right, I was implementing my workaround for issue #9, and with that my issue is resolved. Many thanks for the speed.

But I can imagine that it can happen for any other filetype with different magic bytes you didn't implement and someone tries to create a custom implementation for it to get it done. So that's what's this bug is about. It's not only about the mp3 filetype, but for any other file type that misses magic bytes that are also valid for that extension.

@AJMitev
Copy link
Owner

AJMitev commented Nov 24, 2020

I don't think your implementation is workaround. Its an absolute valid scenario where the filetype you want to identify is not implemented by design. I think of it as a feature. The purpouse of IFileType and FileType abstraction is to provide that feature.

There is many types that I am not implemented because there are rare to met.

The main goal of this library is to allow programmer to easily adding new types if its nessecery. I am trying to do my best when somebody require an specific type - to be implemented.

@jim-craane
Copy link
Author

Ah ok! Then you are right!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can-not-reproduce Reported bug that cant be reproduce from supporting developer
Projects
None yet
Development

No branches or pull requests

2 participants