-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
247 lines (198 loc) · 6.12 KB
/
main.go
File metadata and controls
247 lines (198 loc) · 6.12 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
package main
import (
"context"
"flag"
"fmt"
"hash/fnv"
"log"
"net/http"
"os"
"path"
"syscall"
"stanleymw/subsonicfs/readbuf"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/supersonic-app/go-subsonic/subsonic"
)
var hasher = fnv.New64()
var SubsonicClient subsonic.Client
func hash(s string) uint64 {
hasher.Reset()
hasher.Write([]byte(s))
return hasher.Sum64()
}
type subsonicFS struct {
fs.Inode
}
type subsonicArtist struct {
fs.Inode
clientObj *subsonic.ArtistID3
}
type subsonicAlbum struct {
fs.Inode
clientObj *subsonic.AlbumID3
}
type subsonicSong struct {
fs.Inode
clientObj *subsonic.Child
streamer *readbuf.ReaderBuf
}
// The root populates the tree in its OnAdd method
var _ = (fs.NodeOnAdder)((*subsonicFS)(nil))
func (song *subsonicSong) Open(ctx context.Context, flags uint32) (fs.FileHandle, uint32, syscall.Errno) {
if song.streamer != nil {
return &song, fuse.FOPEN_DIRECT_IO, 0
}
stmr, err := SubsonicClient.Stream(song.clientObj.ID, nil)
if err != nil {
return nil, 0, syscall.ENOENT
}
song.streamer = readbuf.NewReaderBuf(stmr, song.clientObj.Size)
return &song, fuse.FOPEN_DIRECT_IO, 0
}
func (song *subsonicSong) Read(ctx context.Context, f fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
readStart := off
readEnd := min(off+int64(len(dest)), int64(len(*song.streamer.InternalCache)))
song.streamer.EnsureCached(readStart, readEnd)
return fuse.ReadResultData((*song.streamer.InternalCache)[readStart:readEnd]), 0
}
func (song *subsonicSong) Flush(ctx context.Context, f fs.FileHandle) syscall.Errno {
song.streamer = nil
return 0
}
func (song *subsonicSong) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
sobj := song.clientObj
out.Ctime = uint64(sobj.Created.Unix())
out.Mtime = uint64(sobj.Created.Unix())
out.Atime = uint64(sobj.Played.Unix())
out.Mode = syscall.S_IFREG
out.Size = uint64(sobj.Size)
out.Ino = song.StableAttr().Ino
return 0
}
func (album *subsonicAlbum) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
aobj := album.clientObj
out.Ctime = uint64(aobj.Created.Unix())
out.Mtime = uint64(aobj.Created.Unix())
out.Atime = uint64(aobj.Created.Unix())
out.Mode = syscall.S_IFREG
out.Ino = album.StableAttr().Ino
return 0
}
// Album -> Song (dynamic discovery)
func (album *subsonicAlbum) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
// if we havent already discovered it yet
if len(album.Children()) == 0 {
albumInfo, _ := SubsonicClient.GetAlbum(album.clientObj.ID)
for _, song := range albumInfo.Song {
songIno := album.Inode.NewPersistentInode(
ctx,
&subsonicSong{
clientObj: song,
},
fs.StableAttr{Mode: syscall.S_IFREG, Ino: hash(song.ID)},
)
album.Inode.AddChild(path.Base(song.Path), songIno, true)
}
}
songs := []fuse.DirEntry{}
for name, ino := range album.Children() {
songs = append(songs, fuse.DirEntry{
Mode: fuse.S_IFREG,
Name: name,
Ino: ino.StableAttr().Ino,
})
}
return fs.NewListDirStream(songs), 0
}
// Artist -> Album (Dynamic discovery)
func (artist *subsonicArtist) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
// if we havent already discovered it yet
if len(artist.Children()) == 0 {
art2, _ := SubsonicClient.GetArtist(artist.clientObj.ID)
for _, album := range art2.Album {
// dir, base := filepath.Split(f.Name)
// ch := p.NewPersistentInode(ctx, &fs.Inode{}, fs.StableAttr{Mode: fuse.S_IFDIR})
// p.AddChild(f.Title, ch, true)
albumInode := artist.NewPersistentInode(
ctx,
&subsonicAlbum{
clientObj: album,
},
fs.StableAttr{Mode: fuse.S_IFDIR, Ino: hash(album.ID)})
artist.AddChild(fmt.Sprint(album.Name, " (", album.Year, ")"), albumInode, true)
}
}
songs := []fuse.DirEntry{}
for name, ino := range artist.Children() {
songs = append(songs, fuse.DirEntry{
Mode: fuse.S_IFDIR,
Name: name,
Ino: ino.StableAttr().Ino,
})
}
return fs.NewListDirStream(songs), 0
}
func (album *subsonicSong) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
chil := album.GetChild(name)
if chil != nil {
return chil, 0
}
return nil, syscall.ENOENT
}
func (sr *subsonicFS) OnAdd(ctx context.Context) {
// Construct the filesystem tree: index all of the artists and their albums
p := &sr.Inode
log.Println("Discovering artists...")
artists, err := SubsonicClient.GetArtists(nil)
if err != nil {
log.Fatal("Fatal error! Could not get artists")
return
}
for _, idx := range artists.Index {
for _, artist := range idx.Artist {
artistInode := p.NewPersistentInode(
ctx,
&subsonicArtist{
clientObj: artist,
},
fs.StableAttr{Mode: fuse.S_IFDIR, Ino: hash(artist.ID)})
p.AddChild(artist.Name, artistInode, true)
}
}
log.Println("Artists successfully indexed!")
}
func main() {
hostname := flag.String("hostname", "http://127.0.0.1:4533", "Hostname/IP Address of the Subsonic Server")
username := flag.String("username", "user", "Username for the account")
password := flag.String("password", "user", "Password for the account")
mountDir := flag.String("mountDir", "/tmp/SubsonicFS", "Location to mount SubsonicFS")
passwordAuth := flag.Bool("passwordAuth", false, "Whether or not to use plain-text password authentication (Default is off as it is insecure)")
flag.Parse()
SubsonicClient = subsonic.Client{
Client: &http.Client{},
BaseUrl: *hostname,
User: *username,
ClientName: "SubsonicFS",
PasswordAuth: *passwordAuth,
RequestedAPIVersion: "1.16.1",
}
err := SubsonicClient.Authenticate(*password)
if err != nil {
log.Fatalf("Authentication failed! Check your username and password\n%s", err)
return
}
root := &subsonicFS{}
os.Mkdir(*mountDir, 0755)
log.Printf("Logged in as: %s", SubsonicClient.User)
log.Printf("Mounting at %s...", *mountDir)
server, err := fs.Mount(*mountDir, root, &fs.Options{
MountOptions: fuse.MountOptions{Debug: false},
})
if err != nil {
log.Fatal(err)
}
log.Println("Serving...")
server.Wait()
fmt.Println("Quitting!")
}