作者 yangfu

upload image

1 [dev] 1 [dev]
2 #数据库相关 2 #数据库相关
3 -data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display" 3 +data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display"
4 #data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display" 4 #data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
5 5
6 6
7 #redis相关配置 7 #redis相关配置
8 -redis_add_port = "127.0.0.1:6379"  
9 -redis_auth = "123456"  
  8 +redis_add_port = "192.168.100.102:6379"
  9 +redis_auth = "123456"
  10 +
  11 +#sms相关配置
  12 +yunpian_sms_sdk_url ="https://sms.yunpian.com/v2/sms/single_send.json"
  13 +yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5"
  14 +
  15 +#存储 http://ability.fjmaimaimai.com:8080/
  16 +source_host ="http://192.168.139.131:8080/"
  17 +source_virtual_path=file/ab
  18 +source_path =/home/tiptok/www/ab
@@ -9,4 +9,9 @@ redis_auth = "123456" @@ -9,4 +9,9 @@ redis_auth = "123456"
9 9
10 #sms相关配置 10 #sms相关配置
11 yunpian_sms_sdk_url ="https://sms.yunpian.com/v2/sms/single_send.json" 11 yunpian_sms_sdk_url ="https://sms.yunpian.com/v2/sms/single_send.json"
12 -yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5"  
  12 +yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5"
  13 +
  14 +#存储 http://ability.fjmaimaimai.com:8080/
  15 +source_host ="http://192.168.139.131:8080/"
  16 +source_virtual_path=file/ab
  17 +source_path =/home/tiptok/www/ab
  1 +package v1
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go/ability/controllers"
  5 + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  6 + "gitlab.fjmaimaimai.com/mmm-go/ability/services/upload"
  7 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  8 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
  9 +)
  10 +
  11 +type UploadController struct {
  12 + controllers.BaseController
  13 +}
  14 +
  15 +//Image
  16 +func(this *UploadController)Image(){
  17 + var(
  18 + msg *mybeego.Message
  19 + err error
  20 + )
  21 + defer func(){
  22 + this.Resp(msg)
  23 + }()
  24 + var request = &protocol.ImageRequest{}
  25 + //if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
  26 + // log.Error(err)
  27 + // msg = mybeego.NewMessage(1)
  28 + // return
  29 + //}
  30 + //if b,m :=this.Valid(request);!b{
  31 + // msg = m
  32 + // return
  33 + //}
  34 + if request.Files,err =this.GetFiles("file");err!=nil{
  35 + log.Error(err)
  36 + return
  37 + }
  38 + msg = this.GenMessage(upload.Image(request))
  39 +}
  1 +package protocol
  2 +
  3 +import "mime/multipart"
  4 +
  5 +/*Image */
  6 +type ImageRequest struct {
  7 + //Xxx string`json:"xxx" valid:"Required"`
  8 + Files []*multipart.FileHeader
  9 +}
  10 +type ImageResponse struct {
  11 + Paths []string `json:"paths"`
  12 +}
  1 +package upload
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego"
  6 + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  7 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
  8 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  9 + comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
  10 + "io"
  11 + "mime/multipart"
  12 + "os"
  13 + "path"
  14 + "path/filepath"
  15 + "time"
  16 +)
  17 +
  18 +//上传图片
  19 +func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error){
  20 + var (
  21 + src multipart.File
  22 + dst *os.File
  23 + virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径
  24 + sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),"image") //真实路径
  25 + date,filename string
  26 + )
  27 + rsp =&protocol.ImageResponse{}
  28 + date =comm_time.GetTimeByYyyymmdd()
  29 + sourcePath = filepath.Join(sourcePath,date)
  30 + if _,err=os.Stat(sourcePath);err!=nil{
  31 + log.Error(err)
  32 + if err=os.MkdirAll(sourcePath,0777);err!=nil{
  33 + log.Error(err)
  34 + return
  35 + }
  36 + }
  37 + virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,"image",date)
  38 + for i:=range request.Files{
  39 + f :=request.Files[i]
  40 + subfix:=path.Ext(f.Filename)
  41 + //文件格式不符合
  42 + if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){
  43 + return
  44 + }
  45 + filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
  46 + src,err=f.Open()
  47 + defer src.Close()
  48 + if err!=nil{
  49 + log.Error(err)
  50 + return
  51 + }
  52 + dst,err =os.OpenFile(filepath.Join(sourcePath,filename), os.O_RDWR | os.O_CREATE |os.O_TRUNC,0777) //file/ab/ 静态文件目录
  53 + defer dst.Close()
  54 + if err!=nil{
  55 + log.Error(err)
  56 + return
  57 + }
  58 + if _,err =io.Copy(dst,src);err!=nil{
  59 + log.Error(err)
  60 + return
  61 + }
  62 + rsp.Paths = append(rsp.Paths,filepath.Join(virtualPath,filename))
  63 + }
  64 + return
  65 +}
  1 +package upload
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  5 + "mime/multipart"
  6 + "testing"
  7 +)
  8 +
  9 +func Test_Image(t *testing.T){
  10 + input := &protocol.ImageRequest{}
  11 + input.Files = append(input.Files,&multipart.FileHeader{
  12 + Filename:"abc.gif",
  13 +
  14 + })
  15 +}