-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
117 lines (104 loc) · 2.32 KB
/
catalog.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2019 by Chris Palmer (https://noncombatant.org)
// SPDX-License-Identifier: GPL-3.0
package main
import (
"compress/gzip"
"encoding/gob"
"fmt"
"id3"
"io"
"log"
"os"
"path"
"path/filepath"
)
type Catalog struct {
ItemInfos
}
func (c *Catalog) writeToFile(pathname string) error {
w, e := os.Create(pathname)
if e != nil {
return e
}
zw, e := gzip.NewWriterLevel(w, 9)
if e != nil {
return e
}
if e := gob.NewEncoder(zw).Encode(c); e != nil {
return e
}
if e := zw.Close(); e != nil {
return e
}
return w.Close()
}
const (
catalogBasename = "catalog.gobs.gz"
eraseLine = "\033[2K\r"
)
func shouldSkipFile(info os.FileInfo) bool {
return info.Name() == "" || info.Name()[0] == '.' || info.Size() == 0 || info.Mode().IsDir() || !info.Mode().IsRegular()
}
func newCatalog(log *log.Logger, root string) (*Catalog, error) {
var c Catalog
previousDir := ""
e := filepath.Walk(root,
func(pathname string, info os.FileInfo, e error) error {
if e != nil {
log.Print(e)
return e
}
if shouldSkipFile(info) {
return nil
}
// If we have progressed to a new directory, print progress indicator.
dir := path.Dir(path.Dir(pathname[len(root)+1:]))
if dir != previousDir {
fmt.Fprintf(os.Stdout, "%s%s", eraseLine, dir)
previousDir = dir
}
if isAudioPathname(pathname) || isVideoPathname(pathname) {
webPathname := pathname[len(root)+1:]
itemInfo := ItemInfo{Pathname: webPathname}
input, e := os.Open(pathname)
if e != nil {
log.Print(e)
return e
}
itemInfo.File, _ = id3.Read(input)
if e := input.Close(); e != nil {
log.Print(e)
return e
}
time := info.ModTime()
itemInfo.ModTime = fmt.Sprintf("%04d-%02d-%02d", time.Year(), time.Month(), time.Day())
itemInfo.fillMetadata()
c.ItemInfos = append(c.ItemInfos, itemInfo)
}
return nil
})
fmt.Fprintf(os.Stdout, "%s\n", eraseLine)
return &c, e
}
func readCatalogFromFile(pathname string) (*Catalog, error) {
f, e := os.Open(pathname)
if e != nil {
return nil, e
}
zr, e := gzip.NewReader(f)
if e != nil {
return nil, e
}
var c Catalog
d := gob.NewDecoder(zr)
if e := d.Decode(&c); e != nil && e != io.EOF {
return nil, e
}
if e = zr.Close(); e != nil {
return nil, e
}
if e = f.Close(); e != nil {
return nil, e
}
return &c, nil
}