-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
67 lines (62 loc) · 1.28 KB
/
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
package main
import (
id3 "github.com/gocode/go-id3"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type MediaFile struct {
Name string
IsDir bool
AbsPath string
Size float64
IsAudio bool
IsVideo bool
ID3Name string
Artist string
Album string
Length string
}
func getFiles(path string) ([]MediaFile, error) {
fileInfo, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
var files = make([]MediaFile, 0, len(fileInfo))
var f MediaFile
for _, fi := range fileInfo {
switch strings.ToLower(filepath.Ext(fi.Name())) {
case ".mp3", ".ogg":
var fd, err = os.Open(filepath.Join(path, fi.Name()))
if err != nil {
return nil, err
}
id := id3.Read(fd)
if id== nil {
id = &id3.File{}
}
f = MediaFile{
Name: fi.Name(),
IsDir: fi.IsDir(),
AbsPath: filepath.Join(path, fi.Name()),
Size: float64(fi.Size()) / (1024 * 1024),
IsAudio: true,
ID3Name: id.Name,
Artist: id.Artist,
Album: id.Album,
Length: id.Length,
}
files = append(files, f)
case "", ".mp4":
f = MediaFile{
Name: fi.Name(),
IsDir: fi.IsDir(),
AbsPath: filepath.Join(path, fi.Name()),
Size: float64(fi.Size()) / (1024 * 1024),
}
files = append(files, f)
}
}
return files[:len(files)], nil
}