/
fileUpload.go
53 lines (45 loc) · 1.04 KB
/
fileUpload.go
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
package utils
import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
)
// return the hash value of data. user MD5
func BytesDataHash(data []byte) string {
h := md5.New()
h.Write(data)
hashValue := hex.EncodeToString(h.Sum(nil))
return hashValue
}
// return the hash value of an file which recv in Gin
func GinFormFileHash(file *multipart.FileHeader) (string, []byte, error) {
// read data from file
buff := new(bytes.Buffer)
fp, err := file.Open()
defer fp.Close()
if nil != err {
return "", nil, err
}
n, err := io.Copy(buff, fp)
if nil != err || n == 0 {
msg := fmt.Sprintf("read data from file error:%v", err)
return "", nil, errors.New(msg)
}
return BytesDataHash(buff.Bytes()), buff.Bytes(), nil
}
// upload the file to local dir
func UploadFileToLocal(data []byte, path string) error {
if err := ioutil.WriteFile(path, data, 0644); err != nil {
return err
}
return nil
}
// todo upload file data to oss or other static file server
func UploadDataToCloud() error {
return nil
}