|
|
package upload
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
|
|
|
"io"
|
|
|
"mime/multipart"
|
|
|
"os"
|
|
|
"path"
|
|
|
"path/filepath"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
//上传图片
|
|
|
func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error){
|
|
|
var (
|
|
|
src multipart.File
|
|
|
dst *os.File
|
|
|
virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径
|
|
|
sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),"image") //真实路径
|
|
|
date,filename string
|
|
|
)
|
|
|
rsp =&protocol.ImageResponse{}
|
|
|
date =comm_time.GetTimeByYyyymmdd()
|
|
|
sourcePath = filepath.Join(sourcePath,date)
|
|
|
if _,err=os.Stat(sourcePath);err!=nil{
|
|
|
log.Error(err)
|
|
|
if err=os.MkdirAll(sourcePath,0777);err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,"image",date)
|
|
|
for i:=range request.Files{
|
|
|
f :=request.Files[i]
|
|
|
subfix:=path.Ext(f.Filename)
|
|
|
//文件格式不符合
|
|
|
if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){
|
|
|
return
|
|
|
}
|
|
|
filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
|
|
|
src,err=f.Open()
|
|
|
defer src.Close()
|
|
|
if err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
dst,err =os.OpenFile(filepath.Join(sourcePath,filename), os.O_RDWR | os.O_CREATE |os.O_TRUNC,0777) //file/ab/ 静态文件目录
|
|
|
defer dst.Close()
|
|
|
if err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if _,err =io.Copy(dst,src);err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
rsp.Paths = append(rsp.Paths,filepath.Join(virtualPath,filename))
|
|
|
}
|
|
|
return
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|