123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package utils
- import (
- "bufio"
- "encoding/base64"
- "fmt"
- "image"
- "image/png"
- "io"
- "net/http"
- "os"
- "path"
- )
- // SaveToFile
- // @Description: 将字节内容保存到本地文件
- // @author: Roc
- // @datetime 2024-09-05 18:12:55
- // @param content []byte
- // @param fileName string
- // @return error
- func SaveToFile(content []byte, fileName string) error {
- file, err := os.Create(fileName)
- if err != nil {
- return err
- }
- defer func() {
- _ = file.Close()
- }()
- _, err = file.Write(content)
- if err != nil {
- return err
- }
- return nil
- }
- // FileIsExist
- // @Description: 判断文件是否存在
- // @author: Roc
- // @datetime 2024-09-05 18:12:49
- // @param filePath string
- // @return bool
- func FileIsExist(filePath string) bool {
- _, err := os.Stat(filePath)
- return err == nil || os.IsExist(err)
- }
- // GetImgExt
- // @Description: 获取图片扩展名
- // @author: Roc
- // @datetime 2024-09-05 18:12:44
- // @param file string
- // @return ext string
- // @return err error
- func GetImgExt(file string) (ext string, err error) {
- var headerByte []byte
- headerByte = make([]byte, 8)
- fd, err := os.Open(file)
- if err != nil {
- return "", err
- }
- defer fd.Close()
- _, err = fd.Read(headerByte)
- if err != nil {
- return "", err
- }
- xStr := fmt.Sprintf("%x", headerByte)
- switch {
- case xStr == "89504e470d0a1a0a":
- ext = ".png"
- case xStr == "0000010001002020":
- ext = ".ico"
- case xStr == "0000020001002020":
- ext = ".cur"
- case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
- ext = ".gif"
- case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
- ext = ".tga"
- case xStr[:8] == "464f524d":
- ext = ".iff"
- case xStr[:8] == "52494646":
- ext = ".ani"
- case xStr[:4] == "4d4d" || xStr[:4] == "4949":
- ext = ".tiff"
- case xStr[:4] == "424d":
- ext = ".bmp"
- case xStr[:4] == "ffd8":
- ext = ".jpg"
- case xStr[:2] == "0a":
- ext = ".pcx"
- default:
- ext = ""
- }
- return ext, nil
- }
- // SaveImage 保存图片
- // SaveImage
- // @Description: 保存图片
- // @author: Roc
- // @datetime 2024-09-05 18:09:02
- // @param img image.Image
- // @param path string
- // @return err error
- func SaveImage(img image.Image, path string) (err error) {
- //需要保持的文件
- imgFile, err := os.Create(path)
- defer func() {
- _ = imgFile.Close()
- }()
- // 以PNG格式保存文件
- err = png.Encode(imgFile, img)
- return err
- }
- // DownloadImage
- // @Description: 下载图片
- // @author: Roc
- // @datetime 2024-09-05 18:10:37
- // @param imgUrl string
- // @return filePath string
- // @return err error
- func DownloadImage(imgUrl string) (filePath string, err error) {
- imgPath := "./static/imgs/"
- fileName := path.Base(imgUrl)
- res, err := http.Get(imgUrl)
- if err != nil {
- fmt.Println("A error occurred!")
- return
- }
- defer res.Body.Close()
- // 获得get请求响应的reader对象
- reader := bufio.NewReaderSize(res.Body, 32*1024)
- filePath = imgPath + fileName
- file, err := os.Create(filePath)
- if err != nil {
- return
- }
- // 获得文件的writer对象
- writer := bufio.NewWriter(file)
- _, err = io.Copy(writer, reader)
- //fmt.Printf("Total length: %d \n", written)
- return
- }
- // SaveBase64ToFile
- // @Description: 保存base64数据为文件
- // @author: Roc
- // @datetime 2024-09-05 18:11:47
- // @param content string
- // @param path string
- // @return error
- func SaveBase64ToFile(content, path string) error {
- data, err := base64.StdEncoding.DecodeString(content)
- if err != nil {
- return err
- }
- f, err := os.Create(path)
- defer func() {
- _ = f.Close()
- }()
- if err != nil {
- return err
- }
- _, err = f.Write(data)
- return nil
- }
- // SaveBase64ToFileBySeek
- func SaveBase64ToFileBySeek(content, path string) (err error) {
- data, err := base64.StdEncoding.DecodeString(content)
- exist, err := PathExists(path)
- if err != nil {
- return
- }
- if !exist {
- f, err := os.Create(path)
- if err != nil {
- return err
- }
- n, _ := f.Seek(0, 2)
- // 从末尾的偏移量开始写入内容
- _, err = f.WriteAt([]byte(data), n)
- defer f.Close()
- } else {
- f, err := os.OpenFile(path, os.O_WRONLY, 0644)
- if err != nil {
- return err
- }
- n, _ := f.Seek(0, 2)
- // 从末尾的偏移量开始写入内容
- _, err = f.WriteAt([]byte(data), n)
- defer f.Close()
- }
- return nil
- }
|