diff options
Diffstat (limited to 'internal/notify/files.go')
| -rw-r--r-- | internal/notify/files.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/internal/notify/files.go b/internal/notify/files.go index 53e7f3f..e4da336 100644 --- a/internal/notify/files.go +++ b/internal/notify/files.go @@ -15,6 +15,7 @@ import ( "encoding/json" "os" "path/filepath" + "strconv" ) // RuntimeDir is where the daemon publishes. It is tmpfs, so a reboot clears @@ -45,6 +46,39 @@ func Publish(dir string, live, history []Popup) error { return writeJSON(filepath.Join(dir, "history.json"), history) } +// ImagesDir is where the daemon writes decoded image-data. +func ImagesDir(dir string) string { + return filepath.Join(dir, "img") +} + +// WriteImage writes a decoded image as <id>.png under the image directory. +func WriteImage(dir string, id uint32, data []byte) (string, error) { + imgDir := ImagesDir(dir) + if err := os.MkdirAll(imgDir, 0o700); err != nil { + return "", err + } + path := filepath.Join(imgDir, strconv.FormatUint(uint64(id), 10)+".png") + tmp, err := os.CreateTemp(imgDir, ".img-*") + if err != nil { + return "", err + } + tmpName := tmp.Name() + if _, err := tmp.Write(data); err != nil { + tmp.Close() + os.Remove(tmpName) + return "", err + } + if err := tmp.Close(); err != nil { + os.Remove(tmpName) + return "", err + } + if err := os.Rename(tmpName, path); err != nil { + os.Remove(tmpName) + return "", err + } + return path, nil +} + func writeJSON(path string, v any) error { data, err := json.Marshal(v) if err != nil { |
