add download image from url
This commit is contained in:
parent
8d3c9ec832
commit
885e9e9727
@ -5,10 +5,12 @@ import (
|
|||||||
"catface/app/global/errcode"
|
"catface/app/global/errcode"
|
||||||
"catface/app/global/variable"
|
"catface/app/global/variable"
|
||||||
"catface/app/model"
|
"catface/app/model"
|
||||||
|
"catface/app/service/upload_file"
|
||||||
"catface/app/service/users/curd"
|
"catface/app/service/users/curd"
|
||||||
userstoken "catface/app/service/users/token"
|
userstoken "catface/app/service/users/token"
|
||||||
"catface/app/service/weixin"
|
"catface/app/service/weixin"
|
||||||
"catface/app/utils/response"
|
"catface/app/utils/response"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -155,6 +157,13 @@ func (u *Users) WeixinLogin(context *gin.Context) {
|
|||||||
userName := context.GetString(consts.ValidatorPrefix + "user_name")
|
userName := context.GetString(consts.ValidatorPrefix + "user_name")
|
||||||
userIp := context.ClientIP() // INFO 通过上下文获取 IP 信息。
|
userIp := context.ClientIP() // INFO 通过上下文获取 IP 信息。
|
||||||
|
|
||||||
|
// 0. 保存 user Avatar
|
||||||
|
if err := upload_file.DownloadImage(userAvatar, filepath.Join(variable.BasePath, "userAvatar")); err != nil {
|
||||||
|
// UPDATE 感觉这里需要更好的处理方式
|
||||||
|
response.Fail(context, consts.FilesUploadFailCode, consts.FilesUploadFailMsg, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 访问 微信 API 获取 openid
|
// 1. 访问 微信 API 获取 openid
|
||||||
weixinRes, err := weixin.Code2Session(code)
|
weixinRes, err := weixin.Code2Session(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
package upload_file
|
package upload_file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"catface/app/global/my_errors"
|
||||||
"catface/app/global/variable"
|
"catface/app/global/variable"
|
||||||
|
"catface/app/utils/md5_encrypt"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -56,3 +62,40 @@ func ResizeImage(srcPath string, dstPath string, targetWidth int) (targetHeight
|
|||||||
err = imaging.Save(dstImg, dstPath)
|
err = imaging.Save(dstImg, dstPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DownloadImage(imageUrl, dstPath string) error {
|
||||||
|
resp, err := http.Get(imageUrl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// 检查响应状态码
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("图片下载失败,状态码: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
if sequence := variable.SnowFlake.GetId(); sequence > 0 {
|
||||||
|
saveFileName := fmt.Sprintf("%d%s", sequence, filepath.Base(imageUrl))
|
||||||
|
saveFileName = md5_encrypt.MD5(saveFileName) + ".jpg"
|
||||||
|
|
||||||
|
fullSavePath := filepath.Join(dstPath, saveFileName)
|
||||||
|
file, err := os.Create(fullSavePath)
|
||||||
|
if err != nil {
|
||||||
|
variable.ZapLog.Error("文件保存出错:" + err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(file, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
variable.ZapLog.Error("文件写入出错:" + err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := errors.New(my_errors.ErrorsSnowflakeGetIdFail)
|
||||||
|
variable.ZapLog.Error("文件保存出错:" + err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -44,7 +44,7 @@ func Upload(context *gin.Context, savePath string) (r bool, finnalSavePath inter
|
|||||||
// 文件上传可以设置按照 xxx年-xx月 格式存储
|
// 文件上传可以设置按照 xxx年-xx月 格式存储
|
||||||
func generateYearMonthPath(savePathPre string) (string, string) {
|
func generateYearMonthPath(savePathPre string) (string, string) {
|
||||||
returnPath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileReturnPath")
|
returnPath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileReturnPath")
|
||||||
curYearMonth := time.Now().Format("2006_01")
|
curYearMonth := time.Now().Format("2004_04")
|
||||||
newSavePathPre := savePathPre + curYearMonth
|
newSavePathPre := savePathPre + curYearMonth
|
||||||
newReturnPathPre := returnPath + curYearMonth
|
newReturnPathPre := returnPath + curYearMonth
|
||||||
// 相关路径不存在,创建目录
|
// 相关路径不存在,创建目录
|
||||||
|
Loading…
x
Reference in New Issue
Block a user