/
service_seaweedfs.go
43 lines (37 loc) · 1.17 KB
/
service_seaweedfs.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
package file
import (
"fmt"
"io"
"net/url"
"path/filepath"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/config"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/pkg/log"
"go.uber.org/zap"
)
type SeaweedFS struct {
log.Log
ctx *config.Context
}
func NewSeaweedFS(ctx *config.Context) *SeaweedFS {
return &SeaweedFS{
Log: log.NewTLog("SeaweedFS"),
ctx: ctx,
}
}
// UploadFile 上传文件
func (s *SeaweedFS) UploadFile(filePath string, contentType string, copyFileWriter func(io.Writer) error) (map[string]interface{}, error) {
fileDir, fileName := filepath.Split(filePath)
s.Debug("filePath->", zap.String("filePath", filePath), zap.String("fileDir", fileDir), zap.String("fileName", fileName))
newFileDir := fileDir
if !filepath.IsAbs(fileDir) {
newFileDir = fmt.Sprintf("/%s", newFileDir)
}
seaweedConfig := s.ctx.GetConfig().Seaweed
resultMap, err := uploadFile(fmt.Sprintf("%s%s", seaweedConfig.URL, newFileDir), fileName, copyFileWriter)
return resultMap, err
}
func (s *SeaweedFS) DownloadURL(path string, filename string) (string, error) {
seaweedConfig := s.ctx.GetConfig().Seaweed
rpath, _ := url.JoinPath(seaweedConfig.URL, path)
return rpath, nil
}