File type checker using file magic number












1















I have created a promise to resolve the file type on file upload.



const fileTypeChecker = file => new Promise((resolve) => {
const fileReader = new FileReader()

fileReader.onloadend = (e) => {
const arr = (new Uint8Array(e.target.result)).subarray(0, 4)
let header = ''
let type = null
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16)
}
switch (header) {
case '89504e47': // png
case 'ffd8ffe0': // following are jpeg magic numbers
case 'ffd8ffe1':
case 'ffd8ffe2':
case 'ffd8ffe3':
case 'ffd8ffe8':
type = 'IMG'
break
case 'd0cf11e0': // xls
case '504b34': // xlsx
type = 'XLS'
break
case '25504446':
type = 'PDF'
break
default:
type = 'unknown' // Or you can use the blob.type as fallback
break
}
resolve(type)
}

fileReader.readAsArrayBuffer(file.slice(0, 4))
})


I am using react-dropzone to drop files. The onDrop function is defined as



  onDrop = async (accepted, rejected) => {
const file = accepted[0] || rejected[0]
const fileType = await fileTypeChecker(file)
let error = ''
if (fileType === 'XLS') {
this.setState({
bulkUpload: {
files: file
}
})
} else {
error = 'Please select a valid XLS file'
}
if (error) {
this.setState({
error
})
}
}


Why is the file magic number 504b34 for both .xlsx and .zip files?
So even if I upload a zip file this is treated as XLS.










share|improve this question

























  • You can't get away with just reading the file extension for your use case?

    – Tholle
    Nov 13 '18 at 13:00
















1















I have created a promise to resolve the file type on file upload.



const fileTypeChecker = file => new Promise((resolve) => {
const fileReader = new FileReader()

fileReader.onloadend = (e) => {
const arr = (new Uint8Array(e.target.result)).subarray(0, 4)
let header = ''
let type = null
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16)
}
switch (header) {
case '89504e47': // png
case 'ffd8ffe0': // following are jpeg magic numbers
case 'ffd8ffe1':
case 'ffd8ffe2':
case 'ffd8ffe3':
case 'ffd8ffe8':
type = 'IMG'
break
case 'd0cf11e0': // xls
case '504b34': // xlsx
type = 'XLS'
break
case '25504446':
type = 'PDF'
break
default:
type = 'unknown' // Or you can use the blob.type as fallback
break
}
resolve(type)
}

fileReader.readAsArrayBuffer(file.slice(0, 4))
})


I am using react-dropzone to drop files. The onDrop function is defined as



  onDrop = async (accepted, rejected) => {
const file = accepted[0] || rejected[0]
const fileType = await fileTypeChecker(file)
let error = ''
if (fileType === 'XLS') {
this.setState({
bulkUpload: {
files: file
}
})
} else {
error = 'Please select a valid XLS file'
}
if (error) {
this.setState({
error
})
}
}


Why is the file magic number 504b34 for both .xlsx and .zip files?
So even if I upload a zip file this is treated as XLS.










share|improve this question

























  • You can't get away with just reading the file extension for your use case?

    – Tholle
    Nov 13 '18 at 13:00














1












1








1








I have created a promise to resolve the file type on file upload.



const fileTypeChecker = file => new Promise((resolve) => {
const fileReader = new FileReader()

fileReader.onloadend = (e) => {
const arr = (new Uint8Array(e.target.result)).subarray(0, 4)
let header = ''
let type = null
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16)
}
switch (header) {
case '89504e47': // png
case 'ffd8ffe0': // following are jpeg magic numbers
case 'ffd8ffe1':
case 'ffd8ffe2':
case 'ffd8ffe3':
case 'ffd8ffe8':
type = 'IMG'
break
case 'd0cf11e0': // xls
case '504b34': // xlsx
type = 'XLS'
break
case '25504446':
type = 'PDF'
break
default:
type = 'unknown' // Or you can use the blob.type as fallback
break
}
resolve(type)
}

fileReader.readAsArrayBuffer(file.slice(0, 4))
})


I am using react-dropzone to drop files. The onDrop function is defined as



  onDrop = async (accepted, rejected) => {
const file = accepted[0] || rejected[0]
const fileType = await fileTypeChecker(file)
let error = ''
if (fileType === 'XLS') {
this.setState({
bulkUpload: {
files: file
}
})
} else {
error = 'Please select a valid XLS file'
}
if (error) {
this.setState({
error
})
}
}


Why is the file magic number 504b34 for both .xlsx and .zip files?
So even if I upload a zip file this is treated as XLS.










share|improve this question
















I have created a promise to resolve the file type on file upload.



const fileTypeChecker = file => new Promise((resolve) => {
const fileReader = new FileReader()

fileReader.onloadend = (e) => {
const arr = (new Uint8Array(e.target.result)).subarray(0, 4)
let header = ''
let type = null
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16)
}
switch (header) {
case '89504e47': // png
case 'ffd8ffe0': // following are jpeg magic numbers
case 'ffd8ffe1':
case 'ffd8ffe2':
case 'ffd8ffe3':
case 'ffd8ffe8':
type = 'IMG'
break
case 'd0cf11e0': // xls
case '504b34': // xlsx
type = 'XLS'
break
case '25504446':
type = 'PDF'
break
default:
type = 'unknown' // Or you can use the blob.type as fallback
break
}
resolve(type)
}

fileReader.readAsArrayBuffer(file.slice(0, 4))
})


I am using react-dropzone to drop files. The onDrop function is defined as



  onDrop = async (accepted, rejected) => {
const file = accepted[0] || rejected[0]
const fileType = await fileTypeChecker(file)
let error = ''
if (fileType === 'XLS') {
this.setState({
bulkUpload: {
files: file
}
})
} else {
error = 'Please select a valid XLS file'
}
if (error) {
this.setState({
error
})
}
}


Why is the file magic number 504b34 for both .xlsx and .zip files?
So even if I upload a zip file this is treated as XLS.







javascript reactjs async-await file-type react-dropzone






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:13







Geethu Jose

















asked Nov 13 '18 at 11:39









Geethu JoseGeethu Jose

1,139722




1,139722













  • You can't get away with just reading the file extension for your use case?

    – Tholle
    Nov 13 '18 at 13:00



















  • You can't get away with just reading the file extension for your use case?

    – Tholle
    Nov 13 '18 at 13:00

















You can't get away with just reading the file extension for your use case?

– Tholle
Nov 13 '18 at 13:00





You can't get away with just reading the file extension for your use case?

– Tholle
Nov 13 '18 at 13:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280242%2ffile-type-checker-using-file-magic-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280242%2ffile-type-checker-using-file-magic-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues