forked from NebulousLabs/Sia
/
renter.go
178 lines (151 loc) · 5.11 KB
/
renter.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
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
package api
import (
"net/http"
"time"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"
)
const (
duration = 6000 // Duration that hosts will hold onto the file.
redundancy = 15 // Redundancy of files uploaded to the network.
)
// DownloadInfo is a helper struct for the downloadqueue API call.
type DownloadInfo struct {
StartTime time.Time
Complete bool
Filesize uint64
Received uint64
Destination string
Nickname string
}
// FileInfo is a helper struct for the files API call.
type FileInfo struct {
Available bool
UploadProgress float32
Nickname string
Repairing bool
TimeRemaining types.BlockHeight
}
// LoadedFiles lists files that were loaded into the renter.
type RenterFilesLoadResponse struct {
FilesAdded []string
}
// renterFilesDownloadHandler handles the API call to download a file.
func (srv *Server) renterFilesDownloadHandler(w http.ResponseWriter, req *http.Request) {
err := srv.renter.Download(req.FormValue("nickname"), req.FormValue("destination"))
if err != nil {
writeError(w, "Download failed: "+err.Error(), http.StatusInternalServerError)
return
}
writeSuccess(w)
}
// renterDownloadqueueHandler handles the API call to request the download
// queue.
func (srv *Server) renterDownloadqueueHandler(w http.ResponseWriter, req *http.Request) {
downloads := srv.renter.DownloadQueue()
downloadSet := make([]DownloadInfo, 0, len(downloads))
for _, dl := range downloads {
downloadSet = append(downloadSet, DownloadInfo{
StartTime: dl.StartTime(),
Complete: dl.Complete(),
Filesize: dl.Filesize(),
Received: dl.Received(),
Destination: dl.Destination(),
Nickname: dl.Nickname(),
})
}
writeJSON(w, downloadSet)
}
// renterFilesListHandler handles the API call to list all of the files.
func (srv *Server) renterFilesListHandler(w http.ResponseWriter, req *http.Request) {
files := srv.renter.FileList()
fileSet := make([]FileInfo, 0, len(files))
for _, file := range files {
fileSet = append(fileSet, FileInfo{
Available: file.Available(),
UploadProgress: file.UploadProgress(),
Nickname: file.Nickname(),
Repairing: file.Repairing(),
TimeRemaining: file.TimeRemaining(),
})
}
writeJSON(w, fileSet)
}
// renterFilesDeleteHander handles the API call to delete a file entry from the
// renter.
func (srv *Server) renterFilesDeleteHandler(w http.ResponseWriter, req *http.Request) {
err := srv.renter.DeleteFile(req.FormValue("nickname"))
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeSuccess(w)
}
// renterFilesRenameHandler handles the API call to rename a file entry in the
// renter.
func (srv *Server) renterFilesRenameHandler(w http.ResponseWriter, req *http.Request) {
err := srv.renter.RenameFile(req.FormValue("nickname"), req.FormValue("newname"))
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeSuccess(w)
}
// renterFilesLoadHandler handles the API call to load a '.sia' that
// contains filesharing information.
func (srv *Server) renterFilesLoadHandler(w http.ResponseWriter, req *http.Request) {
files, err := srv.renter.LoadSharedFile(req.FormValue("filename"))
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeJSON(w, RenterFilesLoadResponse{FilesAdded: files})
}
// renterFilesLoadAsciiHandler handles the API call to load a '.sia' file
// in ascii form.
func (srv *Server) renterFilesLoadAsciiHandler(w http.ResponseWriter, req *http.Request) {
files, err := srv.renter.LoadSharedFilesAscii(req.FormValue("file"))
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeJSON(w, RenterFilesLoadResponse{FilesAdded: files})
}
// renterFilesShareHandler handles the API call to create a '.sia' file that
// shares a file.
func (srv *Server) renterFilesShareHandler(w http.ResponseWriter, req *http.Request) {
err := srv.renter.ShareFiles([]string{req.FormValue("nickname")}, req.FormValue("filepath"))
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeSuccess(w)
}
// renterFilesShareAsciiHandler handles the API call to return a '.sia' file
// in ascii form.
func (srv *Server) renterFilesShareAsciiHandler(w http.ResponseWriter, req *http.Request) {
ascii, err := srv.renter.ShareFilesAscii([]string{req.FormValue("nickname")})
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeJSON(w, struct{ File string }{ascii})
}
// renterStatusHandler handles the API call querying the renter's status.
func (srv *Server) renterStatusHandler(w http.ResponseWriter, req *http.Request) {
writeJSON(w, srv.renter.Info())
}
// renterFilesUploadHandler handles the API call to upload a file.
func (srv *Server) renterFilesUploadHandler(w http.ResponseWriter, req *http.Request) {
err := srv.renter.Upload(modules.FileUploadParams{
Filename: req.FormValue("source"),
Duration: duration,
Nickname: req.FormValue("nickname"),
Pieces: redundancy,
})
if err != nil {
writeError(w, "Upload failed: "+err.Error(), http.StatusInternalServerError)
return
}
writeSuccess(w)
}