Reputation: 3514
I am using JMimeMagic lib to validate CSV file upload.
For CSV and every other text file (txt, JSP etc) it gives me text/plain mime type.
logger.debug("Checking magic content");
MagicMatch match;
match = Magic.getMagicMatch(getPromotionOptIn().getUpload(),false);
logger.debug("Actual file mimetype=" + match.getMimeType());
Should not I get text/csv for CSV file? (See all list of mime types).
Or it's fine for it that I put my validation on text/plain thinking its a valid CSV file.
Upvotes: 3
Views: 12386
Reputation: 11108
My guess is that JMimeMagic uses the first few bytes of the file to determine the type. This is possible for many different file types as they have very standard headers. Some text files, like HTML, will have the text <html
somewhere near the beginning, thus giving you a good guess as to what type of file it is.
This sort of deduction is not possible for CSV files. They do not have standard headers. It is difficult to programmatically tell a CSV file from a shopping list with commas in it. It does give you a correct answer of text/plain, as all CSV files are.
Upvotes: 1
Reputation: 88707
Since CSV files can have multiple different separators I suspect the csv file is just recognized as a text file (which is true).
If you see a text file how do you know for sure it is a CSV file? If there are commas, semi colons etc. in the text? What if those belong to an entry and the separator is something else (like |, #, @, etc.)?
You'll have difficulties with telling for sure without more information and JMimeMagic will have the same problems. Thus it will only return what it is sure about: the file is a text file. Thus you "only" get "text/plain".
I don't know that library but from the documentation/source it seems like you could give a hint that *.csv
files have text/csv
mime type using Magic.addHint("csv", someMatcher)
. Note that you might have to pass true
for the second parameters as otherwise those hints might be ignored (seems so from looking at the sources).
That would still depend on the file extension to be correct, i.e. if someone uploaded a .csv file that contains something else you'll get the wrong mime type.
However, it seems like JMimeMagic
would not do much content checking anyways. At least I didn't find much in the sources I found at sourceforge/github. There's only a text file detector so you might have to add your own content detectors for other mime types and file formats.
Upvotes: 3