/
server_files.go
103 lines (96 loc) · 2.38 KB
/
server_files.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
package server
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const fileNumberLimit = 1000
type fsNode struct {
Name string
Size int64
Modified time.Time
Children []*fsNode
}
func (s *Server) listFiles() *fsNode {
rootDir := s.state.Config.DownloadDirectory
root := &fsNode{}
if info, err := os.Stat(rootDir); err == nil {
if err := list(rootDir, info, root, new(int)); err != nil {
log.Printf("File listing failed: %s", err)
}
}
return root
}
func (s *Server) serveFiles(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/download/") {
url := strings.TrimPrefix(r.URL.Path, "/download/")
//dldir is absolute
dldir := s.state.Config.DownloadDirectory
file := filepath.Join(dldir, url)
//only allow fetches/deletes inside the dl dir
if !strings.HasPrefix(file, dldir) || dldir == file {
http.Error(w, "Nice try\n"+dldir+"\n"+file, http.StatusBadRequest)
return
}
info, err := os.Stat(file)
if err != nil {
http.Error(w, "File stat error: "+err.Error(), http.StatusBadRequest)
return
}
switch r.Method {
case "GET":
f, err := os.Open(file)
if err != nil {
http.Error(w, "File open error: "+err.Error(), http.StatusBadRequest)
return
}
http.ServeContent(w, r, info.Name(), info.ModTime(), f)
f.Close()
case "DELETE":
if err := os.RemoveAll(file); err != nil {
http.Error(w, "Delete failed: "+err.Error(), http.StatusInternalServerError)
}
default:
http.Error(w, "Not allowed", http.StatusMethodNotAllowed)
}
return
}
s.static.ServeHTTP(w, r)
}
//custom directory walk
func list(path string, info os.FileInfo, node *fsNode, n *int) error {
if (!info.IsDir() && !info.Mode().IsRegular()) || strings.HasPrefix(info.Name(), ".") {
return errors.New("Non-regular file")
}
(*n)++
if (*n) > fileNumberLimit {
return errors.New("Over file limit") //limit number of files walked
}
node.Name = info.Name()
node.Size = info.Size()
node.Modified = info.ModTime()
if !info.IsDir() {
return nil
}
children, err := ioutil.ReadDir(path)
if err != nil {
return fmt.Errorf("Failed to list files")
}
node.Size = 0
for _, i := range children {
c := &fsNode{}
p := filepath.Join(path, i.Name())
if err := list(p, i, c, n); err != nil {
continue
}
node.Size += c.Size
node.Children = append(node.Children, c)
}
return nil
}