liuxiaolong
2019-06-15 1efc7ae174f57e6a0a96ccfe4efadc9390589b5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package controllers
 
import (
    "bytes"
    "encoding/base64"
    "fmt"
    "github.com/gin-gonic/gin"
    "webserver/extend/code"
    "webserver/extend/esutil"
    "webserver/extend/util"
    "webserver/models"
    "github.com/satori/go.uuid"
    "image"
    "image/jpeg"
    "log"
    "mime/multipart"
    "net/http"
)
 
type FileController struct {
}
 
// weedfs 访问路径
//var picIp = "http://192.168.1.182:6080/"
var picIp = "http://"
 
var picUrlField = "fileUrl"
 
//var picUrlField = "fid"
/**上传方法**/
//var weedfsUri = "http://192.168.1.182:6333/submit"
 
var weedfsUri = "http://192.168.1.182:9500/submit"
 
// @Summary 依据图片添加底库人员
// @Description  依据图片添加底库返回数据人员
// @Accept  mpfd
// @Produce json
// @Tags dbperson 底库人员
// @Param file formData file true "底库人员图片"
// @Param tableId formData string false "底库id,有id 则加入底库,无则只上传图片"
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false}"
// @Router /data/api-v/dbperson/fileupload [POST]
func (fc FileController) Fileupload(c *gin.Context) {
    //得到上传的文件
    file, header, err := c.Request.FormFile("file") //image这个是uplaodify参数定义中的   'fileObjName':'image'
    tableId := c.Request.FormValue("tableId")
    if err != nil {
        c.String(http.StatusBadRequest, "Bad request")
        return
    }
    //文件的名称
    filename := header.Filename
    fmt.Println(file, err, filename)
    if err != nil {
        log.Fatal(err)
        filename = uuid.NewV4().String()
    }
    // fastdfs
    {
        //fileSuffix := path.Ext(filename)[1:] //获取文件后缀
        //fmt.Println("文件后缀 =", fileSuffix)
        //data, _ := ioutil.ReadAll(file)
        // fastdfs 上传  暂废弃
        //field := fdfsclient.UploadFileByBuffer(data, fileSuffix)
    }
    field, result, err := uploadFileReturnAddr(file, filename, tableId)
    if err != nil {
        if err.Error() == "NotFeatureFindError" {
            util.ResponseFormat(c, code.NotFeatureFindError, filename)
        } else {
            util.ResponseFormat(c, code.ServiceInsideError, err.Error())
        }
        return
    }
    if tableId != "" { //  上传
        if result["success"].(bool) {
            util.ResponseFormat(c, code.Success, result["data"])
        } else {
            util.ResponseFormat(c, code.ServiceInsideError, result["data"])
        }
    } else {
        util.ResponseFormat(c, code.Success, field)
    }
}
 
/*// 对上面的编码结果进行base64解码
decodeBytes, err := base64.StdEncoding.DecodeString(encodeString)
if err != nil {
log.Fatalln(err)
}*/
 
func uploadFileReturnAddr(file multipart.File, filename string, tableId string) (string, map[string]interface{}, error) {
 
    defer file.Close()
    field := ""
    // weedfs 上传
    {
        fileInfo, e := esutil.PostFormData(weedfsUri, filename, "file", file)
        if e != nil {
            fmt.Println(e.Error())
            return "", nil, e
        } else {
            field = fileInfo[picUrlField].(string) // 文件路径
        }
    }
    if tableId != "" && field != "" {
        // 返回特征值  fileInfo["point"] = v.RcFace  fileInfo["feature"] = feat
        /*features := gorun.GetSimpleFaceDetect(picIp+field)  // 特征值     // linux
            if len(features) == 0 {                                               // linux
                return field,nil,errors.New("NotFeatureFindError")  // linux
            }else if len(features) > 1 {                                            // linux
                return field,nil,errors.New("TooManyFeatureFindError")  // linux
            }                                                                  // linux
            feat := features[0]["feature"].([]byte)   // linux
        if len(feat) != 2560 {                                // linux
            return field,nil,errors.New("NotFeatureFindError")
        }    */                     // linux
        feat := []byte("hello world") // windows 测试放开
        dbperson := new(models.Dbtablepersons)
        dbperson.FaceUrl = picIp+field       //  图片路经
        dbperson.TableId = tableId  //
        dbperson.PersonName = filename // 图片名
        // 演示base64编码
        encodeString := base64.StdEncoding.EncodeToString(feat)
        dbperson.Feature = encodeString // 特征值base64 码
        result := addDbPerson(dbperson)
        return field, result, nil
    } else {
        return field, nil, nil
    }
}
 
/**上传方法**/
 
 
// @Summary 批量添加底库人员
// @Description  依据图片批量添加底库人员
// @Accept  mpfd
// @Produce json
// @Tags dbperson 底库人员
// @Param files formData file[] true "多个底库人员图片"
// @Param tableId formData string false "底库id,有id 则加入底库,无则只上传图片"
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true,data:{}}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false,data:null}"
// @Router /data/api-v/dbperson/moreFileUpload [POST]
func (fc FileController) MoreFileUpload(c *gin.Context) {
    //得到上传的文件
    //image这个是uplaodify参数定义中的   'fileObjName':'image'
    form, err := c.MultipartForm()
    tableId := c.Request.FormValue("tableId")
    fileHeaders := form.File["files"]
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    extNames := make([]string, 0)
    isAllFiald := true
    addResult := make(map[string]interface{}, 0)
    details := make([]string, 0)
    for _, head := range fileHeaders {
        //文件的名称
        filename := head.Filename
        file, err := head.Open()
        fmt.Println(file, err, filename)
        if err != nil {
            log.Fatal(err)
        }
        //data, _ := ioutil.ReadAll(file)
        // weedfs 上传
        field, _, err1 := uploadFileReturnAddr(file, filename, tableId)
        extNames = append(extNames, field)
        if isAllFiald && err1 == nil {
            isAllFiald = false
        }
        if err1 != nil {
            if field == "" {
                details = append(details, filename+"上传失败,"+err1.Error())
            } else {
                details = append(details, filename+"加入底库失败,"+err1.Error())
            }
        }
    }
    addResult["isAllFiald"] = isAllFiald
    addResult["fields"] = extNames
    addResult["detail"] = details
    //fields := fdfsclient.UploadFileByBuffer100(bytess, extNames)     //  fastdfs 上传
    if !isAllFiald {
        util.ResponseFormat(c, code.Success, addResult)
    } else {
        util.ResponseFormat(c, code.ServiceInsideError, err.Error())
    }
}
 
// @Summary 上传图片 并切图
// @Description  上传图片 并切图
// @Accept  mpfd
// @Produce json
// @Tags dbperson 底库人员
// @Param file formData file true "底库人员图片"
// @Param tableId formData string false "底库id,有id 则加入底库,无则只上传图片"
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false}"
// @Router /data/api-v/es/ImageUploadReturnPics [POST]
func (fc FileController) ImageUploadReturnPics(c *gin.Context) {
    //得到上传的文件
    file, header, err := c.Request.FormFile("file") //image这个是uplaodify参数定义中的   'fileObjName':'image'
    if err != nil {
        log.Fatal(err)
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    //文件的名称
    filename := header.Filename
    fmt.Println(file, err, filename)
    defer file.Close()
    field := ""
    // weedfs 上传
    uploadData := make([]byte, header.Size)
    i, err2 := file.ReadAt(uploadData, 0)
    if err2 != nil {
        fmt.Println("上传返回i:", i)
        util.ResponseFormat(c, code.UploadFileError, err2.Error())
        return
    }
    {
        uri := weedfsUri
        fileInfo, e := esutil.PostFormBufferData(uri, filename, "file", uploadData)
        if e != nil {
            fmt.Println(e.Error())
            util.ResponseFormat(c, code.UploadFileError, e.Error())
            return // 上传失败
        } else {
            field = fileInfo[picUrlField].(string) // 文件路径
        }
    }
    // fileInfo["point"] = v.RcFace   // 返回特征值
    //    fileInfo["feature"] = feat
    //features := gorun.GetSimpleFaceDetect(picIp + "/" + field) // 特征值  只在linux 下
    //feat := []byte("hello world")           // windows 测试放开
    result := make(map[string]interface{}, 0)
    result["uploadImage"] = picIp+field
    smUrl := make([]string, 0)
    //for _, feature := range features {         // linux
    // 获取图片
    //point := feature["point"].(gosdk.CRECT)    // linux
    buffer := bytes.NewBuffer(uploadData)
    m, _, _ := image.Decode(buffer) // 图片文件解码
    rgbImg := m.(*image.YCbCr)
    //subImg := rgbImg.SubImage(image.Rect(int(point.Left),int(point.Bottom),int(point.Right),int(point.Top))).(*image.YCbCr) //图片裁剪x0 y0 x1 y1  linux
    subImg := rgbImg.SubImage(image.Rect(30, 130, 170, 20)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1                           windows
    emptyBuff := bytes.NewBuffer(nil)                                      //开辟一个新的空buff
    jpeg.Encode(emptyBuff, subImg, nil)                                    //img写入到buff
    bytes := emptyBuff.Bytes()
    fileInfo, e := esutil.PostFormBufferData(weedfsUri, filename, "file", bytes)
    if e != nil {
        fmt.Println(e.Error())
        util.ResponseFormat(c, code.UploadFileError, e.Error())
        return // 上传失败
        smUrl = append(smUrl, "一组获取失败")
    } else {
        field = fileInfo[picUrlField].(string) // 文件路径
        smUrl = append(smUrl, picIp+field)
    }
    //}                                    // linux
    result["smImage"] = smUrl
    util.ResponseFormat(c, code.Success, result)
}
 
/**下载方法**/
func Filedown(c *gin.Context) {
    //暂时没有提供方法
 
}