file.go 774 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "bytes"
  6. "property-log/errors"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "strconv"
  12. "time"
  13. )
  14. const FileTmpSavePath = ""
  15. func SaveFileToLocal(uri string) (string, error) {
  16. tmp := FileTmpSavePath
  17. _ = os.MkdirAll(tmp, 0755)
  18. dst := tmp + strconv.Itoa(time.Now().Nanosecond()) + ".png"
  19. res, err := http.Get(uri)
  20. if err != nil {
  21. return "", errors.VendorError
  22. }
  23. defer res.Body.Close()
  24. body, _ := ioutil.ReadAll(res.Body)
  25. out, err := os.Create(dst)
  26. if err != nil {
  27. return "", errors.SystemError
  28. }
  29. defer out.Close()
  30. _, err = io.Copy(out, bytes.NewReader(body))
  31. if err != nil {
  32. return "", errors.SystemError
  33. }
  34. return dst, nil
  35. }