2 Star 0 Fork 0

TeamsHub/backend-gopkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file_utils.go 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
HCY 提交于 2024-05-10 12:41 +08:00 . c
// Copyright 2019 chnykn@gmail.com All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package toolfunc
import (
"fmt"
"path"
"strings"
)
var illegalChars = [10]string{"/", "\n", "*", "\\", "<", ">", "|", "\"", ":", "?"}
func containsIllegalChar(value string) bool {
for _, c := range illegalChars {
n := strings.Count(value, c)
if n > 0 {
return true
}
}
return false
}
func getSuffix(fileName string) string {
var suffix string
suffix = path.Ext(fileName)
suffix = strings.TrimLeft(suffix, ".")
return suffix
}
//CheckFileName ***
func CheckFileName(fileName string) error {
if fileName == "" {
return fmt.Errorf("file name must not be empty")
}
if len(fileName) > 256 {
return fmt.Errorf("file name too long, no more than 256 characters")
}
suffix := getSuffix(fileName)
if suffix == "" {
return fmt.Errorf("file name has no suffix")
}
if containsIllegalChar(fileName) {
return fmt.Errorf("file name contains illegal character")
}
return nil
}
func CheckName(fileName string) error {
if fileName == "" {
return fmt.Errorf("file name must not be empty")
}
if len(fileName) > 150 {
return fmt.Errorf("file name too long, no more than 40 characters")
}
if containsIllegalChar(fileName) {
return fmt.Errorf("file name contains illegal character")
}
return nil
}
//CheckFileType ***
func CheckFileType(allSupportedType []string, fileName string) error {
suffix := strings.ToLower(getSuffix(fileName))
for _, typ := range allSupportedType {
if suffix == strings.ToLower(typ) {
return fmt.Errorf("file type not supported")
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.6.28

搜索帮助