file.go 698 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "ordonnance/logger"
  9. "os"
  10. )
  11. func ReadFile(fileName string) (data []byte, err error) {
  12. f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
  13. defer f.Close()
  14. if err != nil {
  15. return
  16. } else {
  17. data, err = ioutil.ReadAll(f)
  18. return
  19. }
  20. }
  21. // 下载头像
  22. func DownLoadHeadImg(uid int, path string) {
  23. resp, err := http.Get(path)
  24. if !logger.LogErr(err, "DownLoadHeadImg %d", uid) {
  25. return
  26. }
  27. body, _ := ioutil.ReadAll(resp.Body)
  28. destPath := fmt.Sprintf("./uploads/head/%d.jpg", uid)
  29. out, err1 := os.Create(destPath)
  30. if !logger.LogErr(err1, "create file") {
  31. return
  32. }
  33. io.Copy(out, bytes.NewReader(body))
  34. out.Close()
  35. }