var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
var type = file.mimetype.split("/")[0];
switch(type){
case "image":
cb(null, './images');
break;
case "audio":
case "video":
cb(null, './movies');
break;
default:
cb(null, './uploads');
}
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage
});
// api
app.post("/api/upload", upload.single('file'), Admincontroller.postupload);
// postupload
exports.postupload = function (req, res) {
// 解决跨域问题
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "*")
res.header("Access-Control-Allow-Headers", "*")
res.header("Access-Control-Allow-Credentials", "true")
res.header("Cache-Control", "no-cache")
res.header("content-type", "application/json;charset=UTF_8")
if ("OPTIONS" == req.method) {
console.log("===> options reqest");
res.body = 200;
return;
}
var file = req.file;
console.log("===> upload file: " + JSON.stringify(file))
var body = req.body;
console.log("===> upload body: " + JSON.stringify(body))
......
// 处理音视频类型的文件
case 0: // 视频
case 1: // 音频
default: //视频 音频
{
var des = "./movies/";
var trans = body.dtranscode;
var taskId = body.id;
file.path = des + filename;
file.originalname = filename;
var filearr = filename.split(".");
filearr.pop();
var path = filearr.join('.');
var tmppath = des + path;
var exitst = fs.existsSync(tmppath);
if (!exitst) {
fs.mkdirSync(tmppath);
}
var newfilename = filename + body.dzchunkindex;
fs.renameSync(file.path, tmppath + "/" + newfilename);
if (body.dzchunkindex * 1 + 1 == body.dztotalchunkcount * 1) {
var files = fs.readdirSync(tmppath);
for (var i = 0; i < files.length; i++) {
fs.appendFileSync(file.path + "", fs.readFileSync(tmppath + "/" + filename + i));
fs.unlinkSync(tmppath + "/" + filename + i);
}
fs.rmdirSync(tmppath);
}
return res.json({
code: 0,
success: 1
});
}
评论 (1 条评论)