37 lines
592 B
Go
37 lines
592 B
Go
package libs
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
type File struct {
|
|
}
|
|
|
|
// 下载图片到临时文件
|
|
func (*File) DownloadImage(url string) (string, error) {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 创建临时文件
|
|
tempDir := os.TempDir()
|
|
tempFile := filepath.Join(tempDir, fmt.Sprintf("upload-%d.jpg", time.Now().UnixNano()))
|
|
|
|
out, err := os.Create(tempFile)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer out.Close()
|
|
|
|
// 复制文件内容
|
|
_, err = io.Copy(out, resp.Body)
|
|
return tempFile, err
|
|
}
|