file.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package utils
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "fmt"
  6. "image"
  7. "image/png"
  8. "io"
  9. "net/http"
  10. "os"
  11. "path"
  12. )
  13. // SaveToFile
  14. // @Description: 将字节内容保存到本地文件
  15. // @author: Roc
  16. // @datetime 2024-09-05 18:12:55
  17. // @param content []byte
  18. // @param fileName string
  19. // @return error
  20. func SaveToFile(content []byte, fileName string) error {
  21. file, err := os.Create(fileName)
  22. if err != nil {
  23. return err
  24. }
  25. defer func() {
  26. _ = file.Close()
  27. }()
  28. _, err = file.Write(content)
  29. if err != nil {
  30. return err
  31. }
  32. return nil
  33. }
  34. // FileIsExist
  35. // @Description: 判断文件是否存在
  36. // @author: Roc
  37. // @datetime 2024-09-05 18:12:49
  38. // @param filePath string
  39. // @return bool
  40. func FileIsExist(filePath string) bool {
  41. _, err := os.Stat(filePath)
  42. return err == nil || os.IsExist(err)
  43. }
  44. // GetImgExt
  45. // @Description: 获取图片扩展名
  46. // @author: Roc
  47. // @datetime 2024-09-05 18:12:44
  48. // @param file string
  49. // @return ext string
  50. // @return err error
  51. func GetImgExt(file string) (ext string, err error) {
  52. var headerByte []byte
  53. headerByte = make([]byte, 8)
  54. fd, err := os.Open(file)
  55. if err != nil {
  56. return "", err
  57. }
  58. defer fd.Close()
  59. _, err = fd.Read(headerByte)
  60. if err != nil {
  61. return "", err
  62. }
  63. xStr := fmt.Sprintf("%x", headerByte)
  64. switch {
  65. case xStr == "89504e470d0a1a0a":
  66. ext = ".png"
  67. case xStr == "0000010001002020":
  68. ext = ".ico"
  69. case xStr == "0000020001002020":
  70. ext = ".cur"
  71. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  72. ext = ".gif"
  73. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  74. ext = ".tga"
  75. case xStr[:8] == "464f524d":
  76. ext = ".iff"
  77. case xStr[:8] == "52494646":
  78. ext = ".ani"
  79. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  80. ext = ".tiff"
  81. case xStr[:4] == "424d":
  82. ext = ".bmp"
  83. case xStr[:4] == "ffd8":
  84. ext = ".jpg"
  85. case xStr[:2] == "0a":
  86. ext = ".pcx"
  87. default:
  88. ext = ""
  89. }
  90. return ext, nil
  91. }
  92. // SaveImage 保存图片
  93. // SaveImage
  94. // @Description: 保存图片
  95. // @author: Roc
  96. // @datetime 2024-09-05 18:09:02
  97. // @param img image.Image
  98. // @param path string
  99. // @return err error
  100. func SaveImage(img image.Image, path string) (err error) {
  101. //需要保持的文件
  102. imgFile, err := os.Create(path)
  103. defer func() {
  104. _ = imgFile.Close()
  105. }()
  106. // 以PNG格式保存文件
  107. err = png.Encode(imgFile, img)
  108. return err
  109. }
  110. // DownloadImage
  111. // @Description: 下载图片
  112. // @author: Roc
  113. // @datetime 2024-09-05 18:10:37
  114. // @param imgUrl string
  115. // @return filePath string
  116. // @return err error
  117. func DownloadImage(imgUrl string) (filePath string, err error) {
  118. imgPath := "./static/imgs/"
  119. fileName := path.Base(imgUrl)
  120. res, err := http.Get(imgUrl)
  121. if err != nil {
  122. fmt.Println("A error occurred!")
  123. return
  124. }
  125. defer res.Body.Close()
  126. // 获得get请求响应的reader对象
  127. reader := bufio.NewReaderSize(res.Body, 32*1024)
  128. filePath = imgPath + fileName
  129. file, err := os.Create(filePath)
  130. if err != nil {
  131. return
  132. }
  133. // 获得文件的writer对象
  134. writer := bufio.NewWriter(file)
  135. _, err = io.Copy(writer, reader)
  136. //fmt.Printf("Total length: %d \n", written)
  137. return
  138. }
  139. // SaveBase64ToFile
  140. // @Description: 保存base64数据为文件
  141. // @author: Roc
  142. // @datetime 2024-09-05 18:11:47
  143. // @param content string
  144. // @param path string
  145. // @return error
  146. func SaveBase64ToFile(content, path string) error {
  147. data, err := base64.StdEncoding.DecodeString(content)
  148. if err != nil {
  149. return err
  150. }
  151. f, err := os.Create(path)
  152. defer func() {
  153. _ = f.Close()
  154. }()
  155. if err != nil {
  156. return err
  157. }
  158. _, err = f.Write(data)
  159. return nil
  160. }
  161. // SaveBase64ToFileBySeek
  162. func SaveBase64ToFileBySeek(content, path string) (err error) {
  163. data, err := base64.StdEncoding.DecodeString(content)
  164. exist, err := PathExists(path)
  165. if err != nil {
  166. return
  167. }
  168. if !exist {
  169. f, err := os.Create(path)
  170. if err != nil {
  171. return err
  172. }
  173. n, _ := f.Seek(0, 2)
  174. // 从末尾的偏移量开始写入内容
  175. _, err = f.WriteAt([]byte(data), n)
  176. defer f.Close()
  177. } else {
  178. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  179. if err != nil {
  180. return err
  181. }
  182. n, _ := f.Seek(0, 2)
  183. // 从末尾的偏移量开始写入内容
  184. _, err = f.WriteAt([]byte(data), n)
  185. defer f.Close()
  186. }
  187. return nil
  188. }