file.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package lxCommon
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "time"
  11. )
  12. // 二进制流文件保存到本地 name png pdf
  13. func SaveFile(url, path, name string) (filePath string, err error) {
  14. c := NewClient() //忽略证书
  15. _dir := AddNewFile(path)
  16. filePath = _dir + name
  17. resp, err := c.Get(url)
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. defer resp.Body.Close()
  23. body, err := ioutil.ReadAll(resp.Body)
  24. if err != nil {
  25. fmt.Println(err)
  26. return
  27. }
  28. err = ioutil.WriteFile(filePath, body, 0644)
  29. if err != nil {
  30. fmt.Println(err)
  31. return
  32. }
  33. return
  34. }
  35. func AddNewFile(folder string) string {
  36. oldPath := folder
  37. if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
  38. oldPath = "./pdf/"
  39. }
  40. _dir, _ := CreateDateDir(oldPath)
  41. return _dir
  42. }
  43. func Copy(src, dst string) (int64, error) {
  44. sourceFileStat, err := os.Stat(src)
  45. if err != nil {
  46. return 0, err
  47. }
  48. if !sourceFileStat.Mode().IsRegular() {
  49. return 0, fmt.Errorf("%s is not a regular file", src)
  50. }
  51. source, err := os.Open(src)
  52. if err != nil {
  53. return 0, err
  54. }
  55. defer source.Close()
  56. destination, err := os.Create(dst)
  57. if err != nil {
  58. return 0, err
  59. }
  60. defer destination.Close()
  61. nBytes, err := io.Copy(destination, source)
  62. return nBytes, err
  63. }
  64. func GetFileSize(filename string) (int64, error) {
  65. fi, err := os.Stat(filename)
  66. if err != nil {
  67. return 0, err
  68. }
  69. return fi.Size() / 1024 / 1024, nil
  70. }
  71. func CreateDateDir(basePath string) (dirPath, dataString string) {
  72. folderName := time.Now().Format("2006-01-02")
  73. folderPath := filepath.Join(basePath, folderName)
  74. if _, err := os.Stat(folderPath); os.IsNotExist(err) {
  75. // 必须分成两步
  76. // 先创建文件夹
  77. os.MkdirAll(folderPath, 0777)
  78. // 再修改权限
  79. os.Chmod(folderPath, 0777)
  80. }
  81. return folderPath + "/", folderName
  82. }
  83. // 判断文件夹/文件是否存在
  84. func PathIsExists(path string) (bool, error) {
  85. _, err := os.Stat(path)
  86. if err == nil {
  87. return true, nil
  88. }
  89. if os.IsNotExist(err) {
  90. return false, nil
  91. }
  92. return false, err
  93. }
  94. func FileToBase64(filePath string) (imgBase64str string, err error) {
  95. //file, err := c.FormFile("file")
  96. //if err != nil {
  97. // common.SendErrJSON("文件不存在", c)
  98. // return
  99. //}
  100. //imgFile, err := file.Open()
  101. imgFile, err := os.Open(filePath)
  102. if err != nil {
  103. return
  104. }
  105. defer imgFile.Close()
  106. buf, err := io.ReadAll(imgFile)
  107. if err != nil {
  108. return
  109. }
  110. imgBase64str = base64.StdEncoding.EncodeToString(buf)
  111. return
  112. }
  113. func Base64ToFile(imgBase64str string, name string) (err error) {
  114. data, err := base64.StdEncoding.DecodeString(imgBase64str)
  115. if err != nil {
  116. fmt.Println("base64 decode error:", err)
  117. return
  118. }
  119. // 将解码后的字符串写入文件
  120. err = os.WriteFile(name, data, 0644)
  121. if err != nil {
  122. fmt.Println("write file error:", err)
  123. return
  124. }
  125. return
  126. }