-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinfo.go
74 lines (66 loc) · 1.49 KB
/
info.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
package trash
import (
"fmt"
"github.com/rkoesters/xdg/keyfile"
"io"
"net/url"
"strings"
"time"
)
const (
trashInfo = "Trash Info"
timeFormat = "2006-01-02T15:04:05"
)
// Info represents a .trashinfo file.
type Info struct {
Path string
DeletionDate time.Time
}
// NewInfo creates a new Info using the given io.Reader.
func NewInfo(r io.Reader) (*Info, error) {
kf, err := keyfile.New(r)
if err != nil {
return nil, err
}
info := new(Info)
tmp, err := kf.String(trashInfo, "Path")
if err != nil {
return nil, err
}
info.Path, err = url.QueryUnescape(tmp)
if err != nil {
return nil, err
}
tmp, err = kf.String(trashInfo, "DeletionDate")
if err != nil {
return nil, err
}
info.DeletionDate, err = time.ParseInLocation(timeFormat, tmp, time.Local)
if err != nil {
return nil, err
}
return info, nil
}
// String returns Info as a string in the INI format.
func (i *Info) String() string {
return fmt.Sprintf(
"[Trash Info]\nPath=%v\nDeletionDate=%v\n",
queryEscape(i.Path),
i.DeletionDate.Format(timeFormat),
)
}
// queryEscape is a wrapper function around url.QueryEscape that doesn't
// escape '/'.
func queryEscape(s string) string {
// The first for loop is the workaround for "/".
a := strings.Split(s, "/")
for i := 0; i < len(a); i++ {
// The second for loop is the workaround for " ".
b := strings.Split(a[i], " ")
for j := 0; j < len(b); j++ {
b[j] = url.QueryEscape(b[j])
}
a[i] = strings.Join(b, "%20")
}
return strings.Join(a, "/")
}