File: blob.go

package info (click to toggle)
aerc 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (45 lines) | stat: -rw-r--r-- 817 bytes parent folder | download | duplicates (2)
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
package cache

import (
	"os"
	"path"

	"git.sr.ht/~rockorager/go-jmap"
)

func (c *JMAPCache) GetBlob(id jmap.ID) ([]byte, error) {
	fpath := c.blobPath(id)
	if fpath == "" {
		return nil, notfound
	}
	return os.ReadFile(fpath)
}

func (c *JMAPCache) PutBlob(id jmap.ID, buf []byte) error {
	fpath := c.blobPath(id)
	if fpath == "" {
		return nil
	}
	_ = os.MkdirAll(path.Dir(fpath), 0o700)
	return os.WriteFile(fpath, buf, 0o600)
}

func (c *JMAPCache) DeleteBlob(id jmap.ID) error {
	fpath := c.blobPath(id)
	if fpath == "" {
		return nil
	}
	defer func() {
		_ = os.Remove(path.Dir(fpath))
	}()
	return os.Remove(fpath)
}

func (c *JMAPCache) blobPath(id jmap.ID) string {
	if c.blobsDir == "" || id == "" {
		return ""
	}
	name := string(id)
	sub := name[len(name)-2:]
	return path.Join(c.blobsDir, sub, name)
}