-
Notifications
You must be signed in to change notification settings - Fork 279
/
uploader_qiniu.go
60 lines (51 loc) · 1.28 KB
/
uploader_qiniu.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
52
53
54
55
56
57
58
59
60
package controllers
import (
"context"
"mime/multipart"
"os"
"github.com/qiniu/api.v7/auth/qbox"
"github.com/qiniu/api.v7/storage"
"github.com/wangsongyan/wblog/system"
)
// 获取文件大小的接口
type Size interface {
Size() int64
}
// 获取文件信息的接口
type Stat interface {
Stat() (os.FileInfo, error)
}
// 构造返回值字段
type PutRet struct {
Hash string `json:"hash"`
Key string `json:"key"`
}
type QiniuUploader struct {
}
func (u QiniuUploader) upload(file multipart.File, fileHeader *multipart.FileHeader) (url string, err error) {
var (
ret PutRet
size int64
)
if statInterface, ok := file.(Stat); ok {
fileInfo, _ := statInterface.Stat()
size = fileInfo.Size()
}
if sizeInterface, ok := file.(Size); ok {
size = sizeInterface.Size()
}
putPolicy := storage.PutPolicy{
Scope: system.GetConfiguration().QiniuBucket,
}
mac := qbox.NewMac(system.GetConfiguration().QiniuAccessKey, system.GetConfiguration().QiniuSecretKey)
token := putPolicy.UploadToken(mac)
cfg := storage.Config{}
uploader := storage.NewFormUploader(&cfg)
putExtra := storage.PutExtra{}
err = uploader.PutWithoutKey(context.Background(), &ret, token, file, size, &putExtra)
if err != nil {
return
}
url = system.GetConfiguration().QiniuFileServer + ret.Key
return
}