-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfile.go
More file actions
79 lines (70 loc) · 1.92 KB
/
file.go
File metadata and controls
79 lines (70 loc) · 1.92 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
package httpcache
import (
"bufio"
"encoding/gob"
"fmt"
"io"
"net/url"
"os"
"path"
"strings"
"time"
)
// A fileCache implements a cache with files saved in Path.
// It is not safe for concurrency.
type fileCache struct {
Path string
}
// Windows does not accept `\/:*?"<>|` and UNIX `/`, replace all with dash.
var fileNameReplacer = strings.NewReplacer(`/`, `-`, `\`, `-`, `:`, `-`, `*`,
`-`, `?`, `-`, `"`, `-`, `<`, `-`, `>`, `-`, `|`, `-`)
// fileName builds a file name from an URL to be used as cache.
func (f *fileCache) fileName(u *url.URL) string {
return path.Join(f.Path, fileNameReplacer.Replace(u.String()))
}
// Get gets data saved for an URL if present in cache.
func (f *fileCache) Get(u *url.URL) (*entry, error) {
fp, err := os.Open(f.fileName(u))
if err != nil {
return nil, fmt.Errorf("httpcache: could not open %v: %v", u, err)
}
defer fp.Close()
// format before 2025-12-08: encoding/gob
var e entry
if err := gob.NewDecoder(fp).Decode(&e); err == nil {
return &e, nil
}
if _, err := fp.Seek(0, io.SeekStart); err != nil {
return nil, err
}
// simpler format with no binary: first line is date, rest is data
reader := bufio.NewReader(fp)
firstLine, err := reader.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("httpcache: could not read first line: %v", err)
}
saveTime, err := time.Parse(time.RFC3339, firstLine[:len(firstLine)-1])
if err != nil {
return nil, fmt.Errorf("httpcache: error parsing first line (date): %v", err)
}
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
return &entry{data, saveTime}, nil
}
// Put puts data of an URL in cache.
func (f *fileCache) Put(u *url.URL, data []byte) error {
fp, err := os.Create(f.fileName(u))
if err != nil {
return err
}
defer fp.Close()
if _, err := fmt.Fprintln(fp, time.Now().Format(time.RFC3339)); err != nil {
return err
}
if _, err := fp.Write(data); err != nil {
return err
}
return nil
}