Skip to content

Commit

Permalink
对于文件类型错误的,直接在服务器端拦截,避免无效请求浪费贷款
Browse files Browse the repository at this point in the history
  • Loading branch information
tywei90 committed Nov 24, 2017
1 parent c00fba9 commit dcb677d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
8 changes: 4 additions & 4 deletions public/main.js

Large diffs are not rendered by default.

42 changes: 24 additions & 18 deletions server/common/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function getTmpFilePath(filename, mimetype) {
//检测文件类型: img / doc / page / media / unknown
function getFileType(filename) {
var name = filename.toLowerCase();
return /\.(?:png|gif|jpg|jpeg|svg)$/.test(name) ? "img" : /\.(?:mp3|mid|mp4|wav)$/.test(name) ? "media" : /\.(?:txt|doc|docx|md)$/.test(name) ? "doc" : /\.(?:js|css|json|html|htm)$/.test(name) ? "page" : "unknown";
return /\.(?:png|gif|jpg|jpeg|svg)$/.test(name) ? "img" : /\.(?:mp3|mid|mp4|wav|wma)$/.test(name) ? "media" : /\.(?:txt|doc|docx|md)$/.test(name) ? "doc" : /\.(?:js|css|json|html|htm)$/.test(name) ? "page" : "unknown";
}

//不同类型文件最大尺寸限制,单位 M
Expand Down Expand Up @@ -91,7 +91,6 @@ module.exports = function(req, res) {
lossless: req.query.lossless || "", //是否无损压缩png
waterMark: req.query.waterMark || "" //是否为图片添加水印
};

//创建上传组件
var busboy = new Busboy({
headers: req.headers
Expand All @@ -106,22 +105,29 @@ module.exports = function(req, res) {
maxLen = (maxLenForType[type] || 1),
overflow = false;
//检测文件名,只允许多媒体类型上传
if (type == "unknown" || !filename) {
if (filename) {
ret[fieldname] = {
ok: false,
err: 1,
des: "错误的文件类型(" + filename.replace(/^.+\./, ".") + ")."
};
} else {
ret[fieldname] = {
ok: false,
err: 2,
des: "nothing here."
};
}
file.resume();
return;
if(!filename){
ret[fieldname] = {
ok: false,
err: 1,
des: "nothing here."
};
return taskComplete(res, ret);
}
if(type == "unknown"){
ret[fieldname] = {
ok: false,
err: 2,
des: "不支持的文件类型(" + filename.replace(/^.+\./, ".") + ")."
};
return taskComplete(res, ret);
}
if(type != req.query.type){
ret[fieldname] = {
ok: false,
err: 3,
des: "错误的文件类型(" + filename.replace(/^.+\./, ".") + ")."
};
return taskComplete(res, ret);
}
//文件传输中
file.on('data', function(data) {
Expand Down
8 changes: 4 additions & 4 deletions src/components/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ class Content extends React.Component {
const { errTip, visible, confirmLoading } = this.state;
const uploadProps = {
name: 'file',
action: '/upload',
action: '/upload?type=page',
accept: '.json',
headers: {
authorization: 'authorization-text',
},
onChange: this.handleChange.bind(this)
};
if(screen.width < 800){
uploadProps.accept = '.json,.txt,.js';
}
// if(screen.width < 800){
// uploadProps.accept = '.json,.txt,.js';
// }
return (
<section className="m-content f-fl">
<Modal title="导入配置"
Expand Down
2 changes: 1 addition & 1 deletion src/components/units/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UnitAudio extends React.Component {
this.state = {
uploadProps : {
name: 'file',
action: '/upload',
action: '/upload?type=media',
accept: 'audio/mp3,audio/wav',
headers: {
authorization: 'authorization-text',
Expand Down
2 changes: 1 addition & 1 deletion src/components/units/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UnitButton extends React.Component {
this.state = {
uploadProps : {
name: 'file',
action: '/upload',
action: '/upload?type=img',
accept: 'image/*',
headers: {
authorization: 'authorization-text',
Expand Down
2 changes: 1 addition & 1 deletion src/components/units/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UnitImage extends React.Component {
this.state = {
uploadProps : {
name: 'file',
action: '/upload',
action: '/upload?type=img',
accept: 'image/*',
headers: {
authorization: 'authorization-text',
Expand Down
2 changes: 1 addition & 1 deletion views/common/docHead.pug
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mixin docHead(config)
meta(name='renderer' content='webkit')
if config.showProgressBar
link(rel='stylesheet' href='https://cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-loading-bar.min.css')
script(async src='https://cdn.bootcss.com/pace/1.0.2/pace.min.js')
script(src='https://cdn.bootcss.com/pace/1.0.2/pace.min.js')
if config.keywords
meta(name='keywords' content=config.keywords)
if config.description
Expand Down

0 comments on commit dcb677d

Please sign in to comment.