@@ -2,18 +2,25 @@ package main
2
2
3
3
import (
4
4
"flag"
5
- "http"
6
- "json"
5
+ "net/ http"
6
+ "encoding/ json"
7
7
"os"
8
+ "log"
8
9
)
9
10
11
+ type Entry struct {
12
+ Name string // name of the object
13
+ IsDir bool
14
+ Mode os.32 FileMode
15
+ }
16
+
10
17
const (
11
18
filePrefix = "/f/"
12
19
)
13
20
14
21
var (
15
22
addr = flag .String ("http" , ":8080" , "http listen address" )
16
- root = flag .String ("root" , "/store/iTunes /" , "music root" )
23
+ root = flag .String ("root" , "/home/flo/nfs/flo/Music /" , "music root" )
17
24
)
18
25
19
26
func main () {
@@ -25,38 +32,57 @@ func main() {
25
32
26
33
func Index (w http.ResponseWriter , r * http.Request ) {
27
34
http .ServeFile (w , r , "./index.html" )
35
+ log .Print ("index called" )
28
36
}
29
37
30
38
func File (w http.ResponseWriter , r * http.Request ) {
31
39
fn := * root + r .URL .Path [len (filePrefix ):]
32
40
fi , err := os .Stat (fn )
41
+ log .Print ("File called: " , fn )
42
+
33
43
if err != nil {
34
- http .Error (w , err .String (), http .StatusNotFound )
44
+ http .Error (w , err .Error (), http .StatusNotFound )
35
45
return
36
46
}
37
- if fi .IsDirectory () {
47
+ if fi .IsDir () {
38
48
serveDirectory (fn , w , r )
39
49
return
40
50
}
41
51
http .ServeFile (w , r , fn )
42
52
}
43
53
44
- func serveDirectory (fn string , w http.ResponseWriter , r * http.Request ) {
54
+ func serveDirectory (fn string , w http.ResponseWriter ,
55
+ r * http.Request ) {
45
56
defer func () {
46
- if err , ok := recover ().(os. Error ); ok {
47
- http .Error (w , err .String (), http .StatusInternalServerError )
57
+ if err , ok := recover ().(error ); ok {
58
+ http .Error (w , err .Error (), http .StatusInternalServerError )
48
59
}
49
60
}()
50
61
d , err := os .Open (fn )
51
62
if err != nil {
52
63
panic (err )
53
64
}
65
+ log .Print ("serverDirectory called: " , fn )
66
+
54
67
files , err := d .Readdir (- 1 )
55
68
if err != nil {
56
69
panic (err )
57
70
}
58
- j := json .NewEncoder (w )
59
- if err := j .Encode (files ); err != nil {
60
- panic (err )
71
+
72
+ // Json Encode isn't working with the FileInfo interface,
73
+ // therefore populate an Array of Entry and add the Name method
74
+ entries := make ([]Entry , len (files ), len (files ))
75
+
76
+ for k := range files {
77
+ //log.Print(files[k].Name())
78
+ entries [k ].Name = files [k ].Name ()
79
+ entries [k ].IsDir = files [k ].IsDir ()
80
+ entries [k ].Mode = files [k ].Mode ()
81
+ }
82
+
83
+ j := json .NewEncoder (w )
84
+
85
+ if err := j .Encode (& entries ); err != nil {
86
+ panic (err )
61
87
}
62
88
}
0 commit comments