For a file upload function, you must restrict the format of the file to be uploaded. We usually use the extension name for restrictions. Status quo: Redundancy
In web development, do we often use different programming languages to implement the same functions?
For a file upload function, you must restrict the format of the file to be uploaded. We usually use the extension name for restrictions.
Front end
For the user experience, the user will judge the selected files on the page, so that the user can upload the files only after they are valid.
The Code is as follows:
Function is_filetype (filename, types ){
Types = types. split (',');
Var pattern = '\.(';
For (var I = 0; I If (0! = I ){
Pattern + = '| ';
}
Pattern + = types [I]. trim ();
}
Pattern + = ') $ ';
Return new RegExp (pattern, 'I'). test (filename );
};
// N lines of code are omitted here
If (! Is_filetype ($ ('# uploadfile'). val (), 'doc, pdf, txt, wps, odf, md, png, gif, jpg ')){
Can_submit = false; // upload is not allowed
$ ('# Uploadfile'). val ('');
Alert ('upload only allowed: '+ constant. RESUME_FILETYPES );
}
// N lines of code are omitted here
Backend
Due to fear of malicious uploads, it is unavoidable to re-judge the files uploaded by users. So I used python to write a logic for determining the file suffix.
The Code is as follows:
Import re
Def is_filetype (filename, types ):
Types = types. split (',')
Pattern = '\. (' + '|'. join ([t. strip () for t in types]) + ') $ ';
Return re. search (pattern, filename, re. I )! = None
# Web request handler
# N lines of code are omitted here
Why does this repetitive work occur?
1. The front-end will never be trusted;
2. The frontend and backend use different programming languages;
What are the costs of such redundancy?
1. Modify the business logic twice. If the docx file type is suddenly not supported, you must modify the javascript code and python code at the same time.
2. added the cost of ensuring that the javascript code and python code business logic are consistent. You need to write two types of tests respectively, and the unit test is doubled.
Nodejs era: DRY
Use nodejs no more DRY!
One piece of code, and the front-end and back-end run simultaneously
The Code is as follows:
// Constant. js
(Function (exports ){
Exports. RESUME_FILETYPES = 'doc, docx, pdf, txt, wps, odf, md, png, gif, jpg ';
}) (Function (){
If (typeof exports = 'undefined '){
Window. constant = {};
Return window. constant;
} Else {
Return exports;
}
})());
// Util. js
(Function (exports ){
/**
* Remove white spaces at both ends of the string.
*
* @ Return {String}
* @ Api public
*/
String. prototype. trim = function (){
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
};
/**
* Determine whether a custom file is used.
*
* @ Param {String} filename
* @ Param {String} types. Multiple types are separated by commas (,), such as doc, docx, and txt.
* @ Return {Boolean} true or false
* @ Api public
*/
Var is_filetype = exports. is_filetype = function (filename, types ){
Types = types. split (',');
Var pattern = '\.(';
For (var I = 0; I If (0! = I ){
Pattern + = '| ';
}
Pattern + = types [I]. trim ();
}
Pattern + = ') $ ';
Return new RegExp (pattern, 'I'). test (filename );
};
}) (Function (){
If (typeof exports = 'undefined '){
Window. util = {};
Return window. util;
} Else {
Return exports;
}
})());
Front end
The Code is as follows: