-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_upload.go
97 lines (88 loc) · 2.33 KB
/
http_upload.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
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
package server
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/httplib"
"github.com/google/uuid"
"github.com/koihuang/speedfs/config"
"io"
"mime/multipart"
"net/http"
"os"
gopath "path"
"strings"
"time"
)
func (server *Server) Upload(w http.ResponseWriter, r *http.Request) {
filepath, err := server.upload(r)
if err != nil {
log.Error(fmt.Sprintf("upload err: %s", err.Error()))
writeFailRes(w, systemErr, err.Error())
} else {
writeSuccessRes(w, UploadRes{Filepath: filepath, DownloadUrl: fmt.Sprintf("http://%s/%s", config.SelfPeer(), filepath)})
}
}
func (server *Server) upload(r *http.Request) (string, error) {
var (
err error
filename string
uploadFile multipart.File
uploadHeader *multipart.FileHeader
fullpath string
)
if uploadFile, uploadHeader, err = r.FormFile("file"); err != nil {
fmt.Println(err)
}
_, filename = gopath.Split(uploadHeader.Filename)
currentTime := time.Now()
formattedTime := currentTime.Format("150405")
uniqueID := formattedTime + "_" + strings.ReplaceAll(uuid.New().String(), "-", "")
folder := time.Now().Format("20060102")
var relativePath string
if filename == "" {
relativePath = gopath.Join(folder, uniqueID)
} else {
relativePath = gopath.Join(folder, uniqueID+"_"+filename)
}
fullpath = gopath.Join(server.fileRootDir, relativePath)
err = os.MkdirAll(gopath.Dir(fullpath), 0775)
if err != nil {
return "", err
}
file, err := os.OpenFile(fullpath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return "", err
}
_, err = io.Copy(file, uploadFile)
if err != nil {
return "", err
}
stat, err := os.Stat(fullpath)
if err != nil {
return "", err
}
server.notifyPeersToSync(SyncFileInfoReq{
FilePath: relativePath,
FromPeer: config.GloableConfig.AdvertiseIP,
Size: stat.Size(),
})
return gopath.Clean("/" + relativePath), nil
}
func (server *Server) notifyPeersToSync(fileInfo SyncFileInfoReq) {
peers := config.OtherPeers()
data, err := json.Marshal(fileInfo)
if err != nil {
log.Error(err)
return
}
for _, peer := range peers {
go func(p string) {
req := httplib.Post(fmt.Sprintf("http://%s/sync", p))
req.Param("fileInfo", string(data))
_, err = req.Response()
if err != nil {
log.Errorf("fail to sync file to peer, peer:%s, path:%s, err: %s", p, fileInfo.FilePath, err.Error())
}
}(peer)
}
}