/
cacher.go
92 lines (78 loc) · 2.02 KB
/
cacher.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
package goproxy
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// Cacher defines a set of intuitive methods used to cache module files for the
// `Goproxy`.
type Cacher interface {
// Get gets the matched cache for the name. It returns the
// `os.ErrNotExist` if not found.
//
// It is the caller's responsibility to close the returned
// `io.ReadCloser`.
//
// Note that the returned `io.ReadCloser` can optionally implement the
// following interfaces:
// * `io.Seeker`
// For the Range request header.
// * `interface{ ModTime() time.Time }`
// For the Last-Modified response header.
// * `interface{ Checksum() []byte }`
// For the ETag response header.
Get(ctx context.Context, name string) (io.ReadCloser, error)
// Set sets the content as a cache with the name.
Set(ctx context.Context, name string, content io.ReadSeeker) error
}
// DirCacher implements the `Cacher` using a directory on the local filesystem.
// If the directory does not exist, it will be created with 0750 permissions.
type DirCacher string
// Get implements the `Cacher`.
func (dc DirCacher) Get(
ctx context.Context,
name string,
) (io.ReadCloser, error) {
fileName := filepath.Join(string(dc), filepath.FromSlash(name))
fileInfo, err := os.Stat(fileName)
if err != nil {
return nil, err
}
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
return &struct {
*os.File
os.FileInfo
}{
File: file,
FileInfo: fileInfo,
}, nil
}
// Set implements the `Cacher`.
func (dc DirCacher) Set(
ctx context.Context,
name string,
content io.ReadSeeker,
) error {
fileName := filepath.Join(string(dc), filepath.FromSlash(name))
dir := filepath.Dir(fileName)
if err := os.MkdirAll(dir, 0750); err != nil {
return err
}
file, err := ioutil.TempFile(dir, "")
if err != nil {
return err
}
defer os.Remove(file.Name())
if _, err := io.Copy(file, content); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
return os.Rename(file.Name(), fileName)
}