My current method is this:
我目前的方法是:
var request = require('request');
var mime = require('mime');
var fs = require('fs');
var uri = 'http://www.sweetslyrics.com/images/img_gal/25646_christina-perri-213968.jpg';
request({
'method':'GET',
'uri': uri
},function(err, response,body){
var tmp_path = '/tmp/123456';
fs.writeFile(tmp_path, body, function(err) {
console.log(mime.lookup(tmp_path)); //application/octet-stream ?????
});
});
The image is obviously a picture, but node-mime
says it's application/octet-stream
. Why?
图像显然是一张图片,但node-mime表示它是应用程序/八进制流。为什么?
Note: - I do not want to rely on the Response Headers content-type, because based on my experience, sometimes those response headers are set incorrectly...and they do not determine the true file type. (that's why I save it to a file, and then have node-mime determine it for me!)
注意:-我不想依赖于响应头内容类型,因为根据我的经验,有时候这些响应头设置不正确……而且它们不确定真正的文件类型。(这就是为什么我将它保存到一个文件中,然后让node-mime为我确定它!)
I want to know the best way to determine if a file is an image, with 0 margin of error.
我想知道确定一个文件是否是一个图像的最好方法,它的误差范围为0。
Edit: I just realized that node-mime isn't "magic". It just checks for the extension :( ...
编辑:我刚刚意识到node-mime不是“魔法”。它只是检查分机:(……)
Edit2: I found this: https://github.com/SaltwaterC/mime-magic
我找到了这个:https://github.com/SaltwaterC/mime-magic
5 个解决方案
#1
29
Just read the first bytes of the stream, and check it for the so called "magic number".
只要读取流的第一个字节,并检查它是否有所谓的“神奇数字”。
Magic numbers are the first bits of a file which uniquely identify the type of file.
魔术数字是文件的第一个比特,它唯一地识别文件的类型。
For example:
-Every JPEG file begins with ff d8
(hex).
-Every png file begins with a 89 50 4e 47
.
-There is a full table of magic numbers here
例如:-每个JPEG文件都以ff d8(十六进制)开头。-每个png文件都以89 50 4e 47开头。-这里有一整张神奇数字的表格
This way even if you have a file without extension you can still detect its type.
Hope this helps.
这样,即使您有一个没有扩展名的文件,您仍然可以检测它的类型。希望这个有帮助。
#2
6
This code shows a working solution for the magic numbers approach (summary of the existing answers and information on https://github.com/request/request).
这段代码显示了神奇数字方法的有效解决方案(对https://github.com/request/request上现有答案和信息的总结)。
var request = require('request');
var url = "http://www.somedomain.com/somepicture.jpg";
var magic = {
jpg: 'ffd8ffe0',
png: '89504e47',
gif: '47494638'
};
var options = {
method: 'GET',
url: url,
encoding: null // keeps the body as buffer
};
request(options, function (err, response, body) {
if(!err && response.statusCode == 200){
var magigNumberInBody = body.toString('hex',0,4);
if (magigNumberInBody == magic.jpg ||
magigNumberInBody == magic.png ||
magigNumberInBody == magic.gif) {
// do something
}
}
});
#3
3
There are two modules that can help you achieve this:
有两个模块可以帮助您实现这一点:
https://github.com/SaltwaterC/mime-magic
https://github.com/SaltwaterC/mime-magic
https://github.com/bentomas/node-mime
https://github.com/bentomas/node-mime
#4
1
In the intervening time since this question was first asked, mime-magic has become unsupported and its author recommends the use of mmmagic. I don't know what happened to node-mime, the link above is a 404. I found the following article which discusses the topic as well: https://nodejsmodules.org/tags/mime
在第一次被问到这个问题后的那段时间里,“me-magic”变得不受支持,它的作者建议使用mmmagic。我不知道node-mime上面的链接是404。我找到了下面的文章,它也讨论了这个主题:https://nodejsmodules.org/tags/mime
#5
0
i developped this code and i test it and it work for me you can use it
我开发了这段代码并进行了测试,它对我有用你可以使用它
var express = require('express')
var app = express()
var http = require('http').Server(app).listen(80)
var upload = require('express-fileupload')
app.use(upload())
app.get("/",(req,res)=>{
res.sendFile(__dirname+"/file.html")
})
app.post('/',(req,res)=>{
var options = {
method: 'GET',
url: req.files.filename,
encoding: null
}
if (req.files) {
if (req.files.filename.data.toString('hex',0,4) == '89504e47' || req.files.filename.data.toString('hex',0,4) == 'ffd8ffe0' || req.files.filename.data.toString('hex',0,4) == '47494638' ) {
var file = req.files.filename
filename = file.name
file.mv('./upload/'+filename,(err)=>{
if (err) {
console.log('small err')
} else {
res.send('DONE')
}
})
} else {
console.log('it not an image')
}
}
})
#1
29
Just read the first bytes of the stream, and check it for the so called "magic number".
只要读取流的第一个字节,并检查它是否有所谓的“神奇数字”。
Magic numbers are the first bits of a file which uniquely identify the type of file.
魔术数字是文件的第一个比特,它唯一地识别文件的类型。
For example:
-Every JPEG file begins with ff d8
(hex).
-Every png file begins with a 89 50 4e 47
.
-There is a full table of magic numbers here
例如:-每个JPEG文件都以ff d8(十六进制)开头。-每个png文件都以89 50 4e 47开头。-这里有一整张神奇数字的表格
This way even if you have a file without extension you can still detect its type.
Hope this helps.
这样,即使您有一个没有扩展名的文件,您仍然可以检测它的类型。希望这个有帮助。
#2
6
This code shows a working solution for the magic numbers approach (summary of the existing answers and information on https://github.com/request/request).
这段代码显示了神奇数字方法的有效解决方案(对https://github.com/request/request上现有答案和信息的总结)。
var request = require('request');
var url = "http://www.somedomain.com/somepicture.jpg";
var magic = {
jpg: 'ffd8ffe0',
png: '89504e47',
gif: '47494638'
};
var options = {
method: 'GET',
url: url,
encoding: null // keeps the body as buffer
};
request(options, function (err, response, body) {
if(!err && response.statusCode == 200){
var magigNumberInBody = body.toString('hex',0,4);
if (magigNumberInBody == magic.jpg ||
magigNumberInBody == magic.png ||
magigNumberInBody == magic.gif) {
// do something
}
}
});
#3
3
There are two modules that can help you achieve this:
有两个模块可以帮助您实现这一点:
https://github.com/SaltwaterC/mime-magic
https://github.com/SaltwaterC/mime-magic
https://github.com/bentomas/node-mime
https://github.com/bentomas/node-mime
#4
1
In the intervening time since this question was first asked, mime-magic has become unsupported and its author recommends the use of mmmagic. I don't know what happened to node-mime, the link above is a 404. I found the following article which discusses the topic as well: https://nodejsmodules.org/tags/mime
在第一次被问到这个问题后的那段时间里,“me-magic”变得不受支持,它的作者建议使用mmmagic。我不知道node-mime上面的链接是404。我找到了下面的文章,它也讨论了这个主题:https://nodejsmodules.org/tags/mime
#5
0
i developped this code and i test it and it work for me you can use it
我开发了这段代码并进行了测试,它对我有用你可以使用它
var express = require('express')
var app = express()
var http = require('http').Server(app).listen(80)
var upload = require('express-fileupload')
app.use(upload())
app.get("/",(req,res)=>{
res.sendFile(__dirname+"/file.html")
})
app.post('/',(req,res)=>{
var options = {
method: 'GET',
url: req.files.filename,
encoding: null
}
if (req.files) {
if (req.files.filename.data.toString('hex',0,4) == '89504e47' || req.files.filename.data.toString('hex',0,4) == 'ffd8ffe0' || req.files.filename.data.toString('hex',0,4) == '47494638' ) {
var file = req.files.filename
filename = file.name
file.mv('./upload/'+filename,(err)=>{
if (err) {
console.log('small err')
} else {
res.send('DONE')
}
})
} else {
console.log('it not an image')
}
}
})