🍉 check file size when open file
This commit is contained in:
parent
04807e032d
commit
c5d451a398
|
@ -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)
|
||||
|
|
15
util/file.go
15
util/file.go
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue