| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path" |
| 6 |
|
| 7 |
"git.sr.ht/~rockorager/go-jmap" |
| 8 |
) |
| 9 |
|
| 10 |
func (c *JMAPCache) GetBlob(id jmap.ID) ([]byte, error) { |
| 11 |
fpath := c.blobPath(id) |
| 12 |
if fpath == "" { |
| 13 |
return nil, notfound |
| 14 |
} |
| 15 |
return os.ReadFile(fpath) |
| 16 |
} |
| 17 |
|
| 18 |
func (c *JMAPCache) PutBlob(id jmap.ID, buf []byte) error { |
| 19 |
fpath := c.blobPath(id) |
| 20 |
if fpath == "" { |
| 21 |
return nil |
| 22 |
} |
| 23 |
_ = os.MkdirAll(path.Dir(fpath), 0o700) |
| 24 |
return os.WriteFile(fpath, buf, 0o600) |
| 25 |
} |
| 26 |
|
| 27 |
func (c *JMAPCache) DeleteBlob(id jmap.ID) error { |
| 28 |
fpath := c.blobPath(id) |
| 29 |
if fpath == "" { |
| 30 |
return nil |
| 31 |
} |
| 32 |
defer func() { |
| 33 |
_ = os.Remove(path.Dir(fpath)) |
| 34 |
}() |
| 35 |
return os.Remove(fpath) |
| 36 |
} |
| 37 |
|
| 38 |
func (c *JMAPCache) blobPath(id jmap.ID) string { |
| 39 |
if c.blobsDir == "" || id == "" { |
| 40 |
return "" |
| 41 |
} |
| 42 |
name := string(id) |
| 43 |
sub := name[len(name)-2:] |
| 44 |
return path.Join(c.blobsDir, sub, name) |
| 45 |
} |