123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package lxCommon
- import (
- "encoding/base64"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
- "runtime"
- "time"
- )
- // 二进制流文件保存到本地 name png pdf
- func SaveFile(url, path, name string) (filePath string, err error) {
- c := NewClient() //忽略证书
- _dir := AddNewFile(path)
- filePath = _dir + name
- resp, err := c.Get(url)
- if err != nil {
- fmt.Println(err)
- return
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println(err)
- return
- }
- err = ioutil.WriteFile(filePath, body, 0644)
- if err != nil {
- fmt.Println(err)
- return
- }
- return
- }
- func AddNewFile(folder string) string {
- oldPath := folder
- if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
- oldPath = "./pdf/"
- }
- _dir, _ := CreateDateDir(oldPath)
- return _dir
- }
- func Copy(src, dst string) (int64, error) {
- sourceFileStat, err := os.Stat(src)
- if err != nil {
- return 0, err
- }
- if !sourceFileStat.Mode().IsRegular() {
- return 0, fmt.Errorf("%s is not a regular file", src)
- }
- source, err := os.Open(src)
- if err != nil {
- return 0, err
- }
- defer source.Close()
- destination, err := os.Create(dst)
- if err != nil {
- return 0, err
- }
- defer destination.Close()
- nBytes, err := io.Copy(destination, source)
- return nBytes, err
- }
- func GetFileSize(filename string) (int64, error) {
- fi, err := os.Stat(filename)
- if err != nil {
- return 0, err
- }
- return fi.Size() / 1024 / 1024, nil
- }
- func CreateDateDir(basePath string) (dirPath, dataString string) {
- folderName := time.Now().Format("2006-01-02")
- folderPath := filepath.Join(basePath, folderName)
- if _, err := os.Stat(folderPath); os.IsNotExist(err) {
- // 必须分成两步
- // 先创建文件夹
- os.MkdirAll(folderPath, 0777)
- // 再修改权限
- os.Chmod(folderPath, 0777)
- }
- return folderPath + "/", folderName
- }
- // 判断文件夹/文件是否存在
- func PathIsExists(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 FileToBase64(filePath string) (imgBase64str string, err error) {
- //file, err := c.FormFile("file")
- //if err != nil {
- // common.SendErrJSON("文件不存在", c)
- // return
- //}
- //imgFile, err := file.Open()
- imgFile, err := os.Open(filePath)
- if err != nil {
- return
- }
- defer imgFile.Close()
- buf, err := io.ReadAll(imgFile)
- if err != nil {
- return
- }
- imgBase64str = base64.StdEncoding.EncodeToString(buf)
- return
- }
- func Base64ToFile(imgBase64str string, name string) (err error) {
- data, err := base64.StdEncoding.DecodeString(imgBase64str)
- if err != nil {
- fmt.Println("base64 decode error:", err)
- return
- }
- // 将解码后的字符串写入文件
- err = os.WriteFile(name, data, 0644)
- if err != nil {
- fmt.Println("write file error:", err)
- return
- }
- return
- }
|