🍉 check file size when open file

This commit is contained in:
Liang Ding 2014-11-21 18:03:48 +08:00
parent 04807e032d
commit c5d451a398
2 changed files with 24 additions and 0 deletions

View File

@ -118,6 +118,15 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
}
path := args["path"].(string)
size := util.File.GetFileSize(path)
if size > 5242880 { // 5M
data["succ"] = false
data["msg"] = "This file is too large to open :("
return
}
buf, _ := ioutil.ReadFile(path)
extension := filepath.Ext(path)

View File

@ -12,6 +12,21 @@ type myfile struct{}
// File utilities.
var File = myfile{}
// GetFileSize get the length in bytes of file of the specified path.
func (*myfile) GetFileSize(path string) int64 {
f, err := os.Open(path)
if nil != err {
return -1
}
fi, err := f.Stat()
if nil != err {
return -1
}
return fi.Size()
}
// IsExist determines whether the file spcified by the given path is exists.
func (*myfile) IsExist(path string) bool {
_, err := os.Stat(path)