-
Notifications
You must be signed in to change notification settings - Fork 44
/
xfer.go
289 lines (243 loc) · 6.77 KB
/
xfer.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2013 Chris McGee <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"archive/zip"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type TransferInfo struct {
IsZip bool
OsPath string
TmpPath string
Location string
Source string
}
var (
counter int
transfers map[int]*TransferInfo
lock *sync.Mutex
)
func init() {
counter = 0
transfers = make(map[int]*TransferInfo)
lock = &sync.Mutex{}
}
func xferHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "POST" && len(pathSegs) > 2:
path := filepath.Clean(strings.Join(pathSegs[2:], "/"))
containerPath := ""
for _, srcDir := range srcDirs {
p := filepath.Join(srcDir, path)
_, err := os.Stat(p)
if err == nil {
containerPath = p
break
}
}
if containerPath == "" {
ShowError(writer, 400, "Container not found or is in GOROOT", nil)
return true
}
xferOption := req.Header.Get("X-Xfer-Options")
tmpDir, err := ioutil.TempDir("", "godev-xfer")
if err != nil {
ShowError(writer, 500, "Unable to create temporary directory", err)
return true
}
var info *TransferInfo = nil
if xferOption == "" {
// It's a zip upload
info = &TransferInfo{true, containerPath, tmpDir, path, req.URL.Query().Get("source")}
} else if xferOption == "raw" {
// It's a raw file upload
slug := req.Header.Get("Slug")
source := req.URL.Query().Get("source")
if slug == "" && source == "" {
ShowError(writer, 400, "No file name is provided", nil)
return true
}
if slug == "" {
sourceParts := strings.Split(source, "/")
slug = sourceParts[len(sourceParts)-1]
}
location := filepath.ToSlash(filepath.Join(path, slug))
info = &TransferInfo{false, filepath.Join(containerPath, slug), tmpDir, location, source}
} else {
// Unknown transfer type
ShowError(writer, 500, "Unknown transfer option "+xferOption, nil)
return true
}
// Check if content is being transferred right away
if req.Header.Get("X-Xfer-Content-Length") == "" {
defer os.RemoveAll(info.TmpPath)
return performTransfer(info, req, writer)
}
// Prepare for the content transfer in a separate request
lock.Lock()
defer lock.Unlock()
counter = counter + 1
transfers[counter] = info
counterStr := strconv.FormatInt(int64(counter), 10)
// Clear the transfer out after 10 minutes
idx := counter
go func() {
<-time.After(10 * time.Minute)
lock.Lock()
info := transfers[idx]
if info != nil {
os.RemoveAll(info.TmpPath)
}
transfers[idx] = nil
lock.Unlock()
}()
writer.Header().Set("Location", "/xfer/"+counterStr)
writer.WriteHeader(200)
return true
case req.Method == "PUT" && len(pathSegs) == 2:
idxStr := pathSegs[1]
idx, err := strconv.ParseInt(idxStr, 10, 32)
if err != nil {
ShowError(writer, 400, "Invalid index", nil)
return true
}
lock.Lock()
info := transfers[int(idx)]
transfers[int(idx)] = nil
lock.Unlock()
// Delete the temporary directory afterwards
defer os.RemoveAll(info.TmpPath)
if info == nil {
ShowError(writer, 400, "Invalid transfer", nil)
return true
}
return performTransfer(info, req, writer)
}
return false
}
func performTransfer(info *TransferInfo, req *http.Request, writer http.ResponseWriter) bool {
transferPath := filepath.Join(info.TmpPath, "transfer")
txFile, err := os.Create(transferPath)
if err != nil {
ShowError(writer, 400, "Unable to create temp file", err)
return true
}
defer txFile.Close()
// The content is in the body of this request
if info.Source == "" {
_, err = io.Copy(txFile, req.Body)
if err != nil {
ShowError(writer, 500, "Error making the transfer", err)
return true
}
// The content is provided as a URL that we download
} else {
resp, err := http.Get(info.Source)
if err != nil {
ShowError(writer, 500, "Error accessing url "+info.Source, err)
return true
}
defer resp.Body.Close()
if resp.StatusCode > 300 {
ShowError(writer, 500, "Error accessign url "+info.Source, err)
return true
}
_, err = io.Copy(txFile, resp.Body)
if err != nil {
ShowError(writer, 500, "Error copying the contents from url "+info.Source, err)
return true
}
}
txFile.Close()
// TODO handle byte range (partial) transfers
if !info.IsZip {
err = os.Rename(transferPath, info.OsPath)
if err != nil {
ShowError(writer, 500, "Error moving file into platce", err)
return true
}
} else {
rc, err := zip.OpenReader(transferPath)
if err != nil {
ShowError(writer, 400, "Zip cannot be opened or is not well-formed", err)
return true
}
defer rc.Close()
for _, zFile := range rc.File {
osPath := filepath.Join(info.OsPath, zFile.Name)
parentDir := filepath.Dir(osPath)
// Make sure that the parent directories all exist for this new file
err = os.MkdirAll(parentDir, 0700)
if err != nil {
ShowError(writer, 500, "Cannot make directories", err)
return true
}
if zFile.FileInfo().IsDir() {
_, err = os.Stat(osPath)
if err == nil {
// Already exists, proceed with the next entry
continue
}
err = os.Mkdir(osPath, 0700)
if err != nil {
ShowError(writer, 500, "Cannot make directory", err)
return true
}
continue
}
osFile, err := os.Create(filepath.Join(info.OsPath, zFile.Name))
if err != nil {
ShowError(writer, 500, "Cannot create file", err)
return true
}
defer osFile.Close()
content, err := zFile.Open()
if err != nil {
ShowError(writer, 500, "Error extracting zip", err)
return true
}
defer content.Close()
_, err = io.Copy(osFile, content)
if err != nil {
ShowError(writer, 500, "Error extracting zip", err)
return true
}
content.Close()
osFile.Close()
}
}
details := &FileDetails{}
fileinfo, err := os.Stat(info.OsPath)
if err != nil {
ShowError(writer, 500, "Error stating new file", err)
return true
}
details.Name = fileinfo.Name()
details.Id = fileinfo.Name()
details.Location = info.Location
details.Directory = fileinfo.IsDir()
details.ETag = strconv.FormatInt(fileinfo.ModTime().Unix(), 16)
details.LocalTimeStamp = fileinfo.ModTime().Unix() * 1000
details.Attributes = make(map[string]bool)
details.Attributes["ReadOnly"] = false
details.Attributes["Executable"] = (fileinfo.Mode()&os.ModePerm)&0111 != 0
// Symlink check
fileinfo, err = os.Lstat(info.OsPath)
if err != nil {
ShowError(writer, 500, "Error accessing file", err)
return true
}
details.Attributes["SymbolicLink"] = (fileinfo.Mode() & os.ModeSymlink) != 0
details.ChildrenLocation = info.Location + "?depth=1"
ShowJson(writer, 201, details)
return true
}