You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
1.6 KiB

9 months ago
package utils
import (
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
)
func LimitFileType(fileName string) bool {
var limits = []string{".html", ".htm", ".php", ".xml", ".asp", ".sthm", ".sthml", ".asp", ".jsp", ".js", ".aspx"}
for i := 0; i < len(limits); i++ {
if strings.HasSuffix(fileName, limits[i]) {
return true
}
}
return false
}
func Tofile(file *multipart.File, path string) error {
(*file).Seek(0, 0)
defer (*file).Seek(0, 0)
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, *file)
return err
}
func IsFileE(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// 判断所给路径是否为文件
func IsFile(path string) bool {
is, _ := IsFileE(path)
return is
}
func Mkdir(path string) error {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
return nil
}
func Download(url string, filename string) error {
// 发送 HTTP 请求获取文件内容
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 创建并打开文件
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
// 将 HTTP 响应体内容写入文件
_, err = io.Copy(file, resp.Body)
if err != nil {
panic(err)
}
return nil
}
func Delete(filePath ...string) {
for _, v := range filePath {
err := os.Remove(v)
if err != nil {
fmt.Println(err)
continue
}
}
return
}