-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhfiles.go
More file actions
334 lines (283 loc) · 7.51 KB
/
Copy pathhfiles.go
File metadata and controls
334 lines (283 loc) · 7.51 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"html/template"
"io"
"log"
"net/http"
"path"
"sort"
"strconv"
"strings"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
func filesHandler(w http.ResponseWriter, r *http.Request) {
ctx, err := getRepoContext(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pathValue := r.PathValue("path")
commit, err := ctx.GitRepo.CommitObject(ctx.Hash)
if err != nil {
http.Error(w, fmt.Sprintf("Error getting commit: %v", err), http.StatusInternalServerError)
return
}
tree, err := commit.Tree()
if err != nil {
http.Error(w, fmt.Sprintf("Error getting tree: %v", err), http.StatusInternalServerError)
return
}
if pathValue != "" {
tree, err = tree.Tree(pathValue)
if err != nil {
http.NotFound(w, r)
return
}
}
var entries []*TreeEntry
for _, entry := range tree.Entries {
fullPath := entry.Name
if pathValue != "" {
fullPath = pathValue + "/" + entry.Name
}
isDir := entry.Mode.IsFile() == false
var size int64
if !isDir {
obj, _ := ctx.GitRepo.Object(plumbing.AnyObject, entry.Hash)
if blob, ok := obj.(*object.Blob); ok {
size = blob.Size
}
}
entries = append(entries, &TreeEntry{
Name: entry.Name,
Path: fullPath,
IsDir: isDir,
Size: size,
Mode: entry.Mode.String(),
})
}
sort.Slice(entries, func(i, j int) bool {
if entries[i].IsDir != entries[j].IsDir {
return entries[i].IsDir
}
return entries[i].Name < entries[j].Name
})
var allEntries []*TreeEntry
fullTree, err := commit.Tree()
if err == nil {
allEntries = getTreeEntries(fullTree, "", 0)
}
data := struct {
*RepoContext
Entries []*TreeEntry
AllEntries []*TreeEntry
Path string
View string
}{
RepoContext: ctx,
Entries: entries,
AllEntries: allEntries,
Path: pathValue,
View: "files",
}
err = templates.ExecuteTemplate(w, "files.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func blobHandler(w http.ResponseWriter, r *http.Request) {
ctx, err := getRepoContext(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pathValue := r.PathValue("path")
commit, err := ctx.GitRepo.CommitObject(ctx.Hash)
if err != nil {
http.Error(w, fmt.Sprintf("Error getting commit: %v", err), http.StatusInternalServerError)
return
}
file, err := commit.File(pathValue)
if err != nil {
http.NotFound(w, r)
return
}
isBinary, _ := file.IsBinary()
if isBinary {
http.Redirect(w, r, fmt.Sprintf("/r/%s/raw/%s?ref=%s", ctx.Repo.Name, pathValue, ctx.CurrentRef), http.StatusFound)
return
}
content, err := file.Contents()
if err != nil {
http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
return
}
var allEntries []*TreeEntry
tree, err := commit.Tree()
if err == nil {
allEntries = getTreeEntries(tree, "", 0)
}
data := struct {
*RepoContext
Path string
Content template.HTML
AllEntries []*TreeEntry
}{
RepoContext: ctx,
Path: pathValue,
Content: highlight(pathValue, content),
AllEntries: allEntries,
}
err = templates.ExecuteTemplate(w, "blob.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func rawHandler(w http.ResponseWriter, r *http.Request) {
ctx, err := getRepoContext(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pathValue := r.PathValue("path")
commit, err := ctx.GitRepo.CommitObject(ctx.Hash)
if err != nil {
http.Error(w, fmt.Sprintf("Error getting commit: %v", err), http.StatusInternalServerError)
return
}
file, err := commit.File(pathValue)
if err != nil {
http.NotFound(w, r)
return
}
reader, err := file.Reader()
if err != nil {
http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
return
}
defer reader.Close()
// Read first 512 bytes to detect content type
data := make([]byte, 512)
n, err := io.ReadFull(reader, data)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
return
}
data = data[:n]
contentType := http.DetectContentType(data)
// Fallback for some common types if DetectContentType returns octet-stream or text/plain
if contentType == "application/octet-stream" || contentType == "text/plain; charset=utf-8" {
ext := strings.ToLower(path.Ext(pathValue))
switch ext {
case ".go", ".ts", ".js", ".md", ".txt", ".json", ".yaml", ".yml", ".sh", ".c", ".h", ".cpp", ".hpp", ".css", ".html":
contentType = "text/plain; charset=utf-8"
case ".pdf":
contentType = "application/pdf"
case ".svg":
contentType = "image/svg+xml"
}
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatInt(file.Size, 10))
// Decide if it should be inline or attachment
disposition := "inline"
if contentType == "application/octet-stream" {
disposition = "attachment"
}
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, path.Base(pathValue)))
// Write the first bytes we read
w.Write(data)
// Then copy the rest
io.Copy(w, reader)
}
func archiveHandler(w http.ResponseWriter, r *http.Request) {
ctx, err := getRepoContext(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pathValue := r.PathValue("path")
commit, err := ctx.GitRepo.CommitObject(ctx.Hash)
if err != nil {
http.Error(w, fmt.Sprintf("Error getting commit: %v", err), http.StatusInternalServerError)
return
}
tree, err := commit.Tree()
if err != nil {
http.Error(w, fmt.Sprintf("Error getting tree: %v", err), http.StatusInternalServerError)
return
}
if pathValue != "" {
tree, err = tree.Tree(pathValue)
if err != nil {
http.NotFound(w, r)
return
}
}
filename := ctx.Repo.Name
if pathValue != "" {
filename = path.Base(pathValue)
}
filename = fmt.Sprintf("%s-%s.tar.gz", filename, ctx.CurrentRef)
w.Header().Set("Content-Type", "application/gzip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
gw := gzip.NewWriter(w)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
err = tree.Files().ForEach(func(f *object.File) error {
hdr := &tar.Header{
Name: strings.TrimPrefix(strings.TrimPrefix(f.Name, pathValue), "/"),
Mode: int64(f.Mode),
Size: f.Size,
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
reader, err := f.Reader()
if err != nil {
return err
}
defer reader.Close()
_, err = io.Copy(tw, reader)
return err
})
if err != nil {
log.Printf("Error creating archive: %v", err)
}
}
func getTreeEntries(tree *object.Tree, prefix string, depth int) []*TreeEntry {
var entries []*TreeEntry
for _, entry := range tree.Entries {
fullPath := entry.Name
if prefix != "" {
fullPath = prefix + "/" + entry.Name
}
isDir := entry.Mode.IsFile() == false
te := &TreeEntry{
Name: entry.Name,
Path: fullPath,
IsDir: isDir,
Mode: entry.Mode.String(),
Depth: depth,
}
if isDir {
subTree, err := tree.Tree(entry.Name)
if err == nil {
te.Children = getTreeEntries(subTree, fullPath, depth+1)
}
}
entries = append(entries, te)
}
sort.Slice(entries, func(i, j int) bool {
if entries[i].IsDir != entries[j].IsDir {
return entries[i].IsDir
}
return entries[i].Name < entries[j].Name
})
return entries
}