file-type npm module returns null 尽管是用缓冲区调用的,我做错了什么?
file-type npm module returns null despite being called with buffer, what am I doing wrong?
我正在开发一个网站,用户可以在其中上传要存储在 MongoDB 中的视频文件。在文件上传和存储之前,我想检查并验证文件的 mimetype。我想在 npm 模块的帮助下做到这一点,我尝试使用文件类型没有成功。
Link 到文件类型 npm 模块:https://www.npmjs.com/package/file-type
我调用带有上传文件缓冲区的模块(使用 mp4 文件测试),但它 returns 为空。这是我的上传路线代码:
'use strict';
const router = require('express').Router();
const VideoInfo = require('../../models/VideoInfo');
const VideoAmount = require('../../models/VideoAmount');
const path = require('path');
const Lib = require('../../lib/Lib');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const fileType = require('file-type');
// Defines storage of files with validation
const storage = new GridFsStorage({
url: process.env.dbURL,
file: (req, file) => {
const data = [];
req.on('data', chunk => {
data.push(chunk);
});
req.on('end', () => {
const buffer = Buffer.concat(data);
const fType = fileType(buffer);
return new Promise((resolve, reject) => {
if (fType === null) {
return reject(new Error('Unsupported file format'));
}
if (fType.mime !== 'video/mp4' ||
fType.mime !== 'video/webm' ||
fType.mime !== 'video/ogg') {
return reject(new Error('Unsupported file format'));
}
if (!req.session.username) {
return reject(new Error('Unauthorized file upload attempt'));
}
// changes the file name before storing
const fileName =
Lib.make.randomString() + path.extname(file.originalname);
const fileInfo = {
filename: fileName,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
router.route('/upload')
.get((req, res) => {
// renders upload form, not really relevant
})
.post(upload.single('video'), async (req, res) => {
// file gets saved to DB with upload.single-function
});
module.exports = router;
我做错了什么?
问题是我没有获取视频文件缓冲区。
解决问题的方法是在我的应用程序中包含 busboy-body-parser:
const busboyBodyParser = require('busboy-body-parser')
app.use(busboyBodyParser({
limit: '120mb'
}))
然后我可以从请求中获取缓冲区:
const fileContent = req.files.video
const buffer = fileContent.data
然后我可以通过使用缓冲区调用文件类型来获取文件的文件类型。
我正在开发一个网站,用户可以在其中上传要存储在 MongoDB 中的视频文件。在文件上传和存储之前,我想检查并验证文件的 mimetype。我想在 npm 模块的帮助下做到这一点,我尝试使用文件类型没有成功。
Link 到文件类型 npm 模块:https://www.npmjs.com/package/file-type
我调用带有上传文件缓冲区的模块(使用 mp4 文件测试),但它 returns 为空。这是我的上传路线代码:
'use strict';
const router = require('express').Router();
const VideoInfo = require('../../models/VideoInfo');
const VideoAmount = require('../../models/VideoAmount');
const path = require('path');
const Lib = require('../../lib/Lib');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const fileType = require('file-type');
// Defines storage of files with validation
const storage = new GridFsStorage({
url: process.env.dbURL,
file: (req, file) => {
const data = [];
req.on('data', chunk => {
data.push(chunk);
});
req.on('end', () => {
const buffer = Buffer.concat(data);
const fType = fileType(buffer);
return new Promise((resolve, reject) => {
if (fType === null) {
return reject(new Error('Unsupported file format'));
}
if (fType.mime !== 'video/mp4' ||
fType.mime !== 'video/webm' ||
fType.mime !== 'video/ogg') {
return reject(new Error('Unsupported file format'));
}
if (!req.session.username) {
return reject(new Error('Unauthorized file upload attempt'));
}
// changes the file name before storing
const fileName =
Lib.make.randomString() + path.extname(file.originalname);
const fileInfo = {
filename: fileName,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
router.route('/upload')
.get((req, res) => {
// renders upload form, not really relevant
})
.post(upload.single('video'), async (req, res) => {
// file gets saved to DB with upload.single-function
});
module.exports = router;
我做错了什么?
问题是我没有获取视频文件缓冲区。
解决问题的方法是在我的应用程序中包含 busboy-body-parser:
const busboyBodyParser = require('busboy-body-parser')
app.use(busboyBodyParser({
limit: '120mb'
}))
然后我可以从请求中获取缓冲区:
const fileContent = req.files.video
const buffer = fileContent.data
然后我可以通过使用缓冲区调用文件类型来获取文件的文件类型。