Skip to content

Commit 9f05324

Browse files
author
Florian Unglaub
committed
Go version 1 compliance
1 parent 7191c62 commit 9f05324

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

index.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
height: 500px;
1515
overflow: auto;
1616
}
17-
#browser a, #playlist a {
18-
display: block;
19-
cursor: pointer;
20-
padding: 2px 4px;
21-
border-radius: 5px;
17+
#browser a, #playlist a {
18+
display: block;
19+
cursor: pointer;
20+
padding: 2px 4px;
21+
border-radius: 5px;
2222
overflow: hidden;
23-
margin-bottom: 2px;
23+
margin-bottom: 2px;
2424
}
2525
#browser { background: #8C9A20; }
2626
#browser a.dir { background: #BECD53; color: black; }
@@ -30,11 +30,11 @@
3030
#playlist a.playing { background: #3B1E6C; color: white; }
3131
#browser a:hover, #playlist a:hover { background: #200A46; color: white; }
3232
#controls { margin-top: 10px; }
33-
#controls a {
34-
cursor: pointer;
35-
background: #eee;
36-
padding: 5px;
37-
border-radius: 5px;
33+
#controls a {
34+
cursor: pointer;
35+
background: #eee;
36+
padding: 5px;
37+
border-radius: 5px;
3838
}
3939
</style>
4040
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
@@ -67,7 +67,7 @@
6767
var $b = $('#browser').empty();
6868
function add(i, f) {
6969
if (f.Name[0] == '.' || f.Name[0] == ':') return;
70-
var dir = (f.Mode & 040000);
70+
var dir = f.IsDir;
7171
var cl = dir ? "dir" : "file";
7272
f.Path = path.join('/');
7373
$('<a></a>').text(f.Name).data('file', f)
@@ -127,7 +127,7 @@
127127
</script>
128128
</head>
129129
<body>
130-
<audio id="player" controls autoplay autobuffer>
130+
<audio id="player" controls autoplay autobuffer>
131131
<p>What? Your browser doesn't support &lt;audio&gt;?! Lame.</p>
132132
</audio>
133133
<div id="browser"></div>

player.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ package main
22

33
import (
44
"flag"
5-
"http"
6-
"json"
5+
"net/http"
6+
"encoding/json"
77
"os"
8+
"log"
89
)
910

11+
type Entry struct {
12+
Name string // name of the object
13+
IsDir bool
14+
Mode os.32FileMode
15+
}
16+
1017
const (
1118
filePrefix = "/f/"
1219
)
1320

1421
var (
1522
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")
1724
)
1825

1926
func main() {
@@ -25,38 +32,57 @@ func main() {
2532

2633
func Index(w http.ResponseWriter, r *http.Request) {
2734
http.ServeFile(w, r, "./index.html")
35+
log.Print("index called")
2836
}
2937

3038
func File(w http.ResponseWriter, r *http.Request) {
3139
fn := *root + r.URL.Path[len(filePrefix):]
3240
fi, err := os.Stat(fn)
41+
log.Print("File called: ", fn)
42+
3343
if err != nil {
34-
http.Error(w, err.String(), http.StatusNotFound)
44+
http.Error(w, err.Error(), http.StatusNotFound)
3545
return
3646
}
37-
if fi.IsDirectory() {
47+
if fi.IsDir() {
3848
serveDirectory(fn, w, r)
3949
return
4050
}
4151
http.ServeFile(w, r, fn)
4252
}
4353

44-
func serveDirectory(fn string, w http.ResponseWriter, r *http.Request) {
54+
func serveDirectory(fn string, w http.ResponseWriter,
55+
r *http.Request) {
4556
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)
4859
}
4960
}()
5061
d, err := os.Open(fn)
5162
if err != nil {
5263
panic(err)
5364
}
65+
log.Print("serverDirectory called: ", fn)
66+
5467
files, err := d.Readdir(-1)
5568
if err != nil {
5669
panic(err)
5770
}
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)
6187
}
6288
}

0 commit comments

Comments
 (0)