]> git.giacomolli.uk - onechat/blob - image_show.go
Show stored images to the user
[onechat] / image_show.go
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "path/filepath"
7 )
8
9 // ImageShow shows a stored image to the root's user. A configured bot sends it
10 // to its owner, and the tool result gives terminal clients its name and path.
11 type ImageShow struct {
12 store *ImageStore
13 bot *TelegramBot
14 }
15
16 // Show checks the stored name and bytes before any output. A refused Telegram
17 // photo goes as a document. An uncertain send is not repeated.
18 func (s *ImageShow) Show(ctx context.Context, name, caption string) (string, error) {
19 data, err := s.store.Read(name)
20 if err != nil {
21 return "", fmt.Errorf("read image to show: %w", err)
22 }
23
24 path, err := filepath.Abs(filepath.Join(s.store.dir, name))
25 if err != nil {
26 return "", fmt.Errorf("find image path: %w", err)
27 }
28
29 if s.bot != nil {
30 err = s.bot.api.SendImage(ctx, s.bot.owner, name, data, caption)
31 if err != nil {
32 return "", fmt.Errorf("show Telegram image: %w", err)
33 }
34 }
35
36 return imageText([]string{name}, caption) + "\n" + path, nil
37 }