123456789101112131415161718192021222324252627282930313233343536373839404142 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package utils
- import (
- "bytes"
- "cp-system-management/errors"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "strconv"
- "time"
- )
- const FileTmpSavePath = ""
- func SaveFileToLocal(uri string) (string, error) {
- tmp := FileTmpSavePath
- _ = os.MkdirAll(tmp, 0755)
- dst := tmp + strconv.Itoa(time.Now().Nanosecond()) + ".png"
- res, err := http.Get(uri)
- if err != nil {
- return "", errors.VendorError
- }
- defer res.Body.Close()
- body, _ := ioutil.ReadAll(res.Body)
- out, err := os.Create(dst)
- if err != nil {
- return "", errors.SystemError
- }
- defer out.Close()
- _, err = io.Copy(out, bytes.NewReader(body))
- if err != nil {
- return "", errors.SystemError
- }
- return dst, nil
- }
|