init
This commit is contained in:
36
libs/file.go
Normal file
36
libs/file.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user