package service
import (
"io/ioutil"
"mall/conf"
"mime/multipart"
"os"
"strconv"
)
func UploadAvatarToLocalStatic(file multipart.File, userId uint, userName string) (filePath string, err error) {
bId := strconv.Itoa(int(userId)) //路径拼接
basePath := "." + conf.AvatarPath + "user" + bId + "/"
if !DirExistOrNot(basePath) {
CreateDir(basePath)
}
//创建图片路径
avatarPath := basePath + userName + ".jpg" //todo:改进把file的后缀提取出来
content, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
err = ioutil.WriteFile(avatarPath, content, 0666)
if err != nil {
return
}
//返回相对路径文件名
return "user" + bId + "/" + userName + ".jpg", nil
}
// UploadProductToLocalStatic
func UploadProductToLocalStatic(file multipart.File, userId uint, productName string) (filePath string, err error) {
bId := strconv.Itoa(int(userId)) //路径拼接
basePath := "." + conf.ProductPath + "boss" + bId + "/"
if !DirExistOrNot(basePath) {
CreateDir(basePath)
}
//创建图片路径
productPath := basePath + productName + ".jpg" //todo:改进把file的后缀提取出来
content, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
err = ioutil.WriteFile(productPath, content, 0666)
if err != nil {
return
}
//返回相对路径文件名
return "boss" + bId + "/" + productName + ".jpg", nil
}