作者 yangfu

upload image

[dev]
#数据库相关
data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display"
data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display"
#data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
#redis相关配置
redis_add_port = "127.0.0.1:6379"
redis_add_port = "192.168.100.102:6379"
redis_auth = "123456"
#sms相关配置
yunpian_sms_sdk_url ="https://sms.yunpian.com/v2/sms/single_send.json"
yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5"
#存储 http://ability.fjmaimaimai.com:8080/
source_host ="http://192.168.139.131:8080/"
source_virtual_path=file/ab
source_path =/home/tiptok/www/ab
\ No newline at end of file
... ...
... ... @@ -10,3 +10,8 @@ redis_auth = "123456"
#sms相关配置
yunpian_sms_sdk_url ="https://sms.yunpian.com/v2/sms/single_send.json"
yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5"
#存储 http://ability.fjmaimaimai.com:8080/
source_host ="http://192.168.139.131:8080/"
source_virtual_path=file/ab
source_path =/home/tiptok/www/ab
\ No newline at end of file
... ...
package v1
import (
"gitlab.fjmaimaimai.com/mmm-go/ability/controllers"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/ability/services/upload"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
)
type UploadController struct {
controllers.BaseController
}
//Image
func(this *UploadController)Image(){
var(
msg *mybeego.Message
err error
)
defer func(){
this.Resp(msg)
}()
var request = &protocol.ImageRequest{}
//if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
// log.Error(err)
// msg = mybeego.NewMessage(1)
// return
//}
//if b,m :=this.Valid(request);!b{
// msg = m
// return
//}
if request.Files,err =this.GetFiles("file");err!=nil{
log.Error(err)
return
}
msg = this.GenMessage(upload.Image(request))
}
... ...
package protocol
import "mime/multipart"
/*Image */
type ImageRequest struct {
//Xxx string`json:"xxx" valid:"Required"`
Files []*multipart.FileHeader
}
type ImageResponse struct {
Paths []string `json:"paths"`
}
\ No newline at end of file
... ...
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
... ...
package upload
import (
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"mime/multipart"
"testing"
)
func Test_Image(t *testing.T){
input := &protocol.ImageRequest{}
input.Files = append(input.Files,&multipart.FileHeader{
Filename:"abc.gif",
})
}
... ...