summaryrefslogtreecommitdiff
path: root/srv/src/post/asset.go
blob: 18af8f6808af524c65f72ad7fe6d1aabee0a8011 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package post

import (
	"bytes"
	"database/sql"
	"errors"
	"fmt"
	"io"
	"sync"
)

var (
	// ErrAssetNotFound is used to indicate an Asset could not be found in the
	// AssetStore.
	ErrAssetNotFound = errors.New("asset not found")
)

// AssetStore implements the storage and retrieval of binary assets, which are
// intended to be used by posts (e.g. images).
type AssetStore interface {

	// Set sets the id to the contents of the given io.Reader.
	Set(id string, from io.Reader) error

	// Get writes the id's body to the given io.Writer, or returns
	// ErrAssetNotFound.
	Get(id string, into io.Writer) error

	// Delete's the body stored for the id, if any.
	Delete(id string) error
}

type assetStore struct {
	db *sql.DB
}

// NewAssetStore initializes a new AssetStore using an existing SQLDB.
func NewAssetStore(db *SQLDB) AssetStore {
	return &assetStore{
		db: db.db,
	}
}

func (s *assetStore) Set(id string, from io.Reader) error {

	body, err := io.ReadAll(from)
	if err != nil {
		return fmt.Errorf("reading body fully into memory: %w", err)
	}

	_, err = s.db.Exec(
		`INSERT INTO assets (id, body)
		VALUES (?, ?)
		ON CONFLICT (id) DO UPDATE SET body=excluded.body`,
		id, body,
	)

	if err != nil {
		return fmt.Errorf("inserting into assets: %w", err)
	}

	return nil
}

func (s *assetStore) Get(id string, into io.Writer) error {

	var body []byte

	err := s.db.QueryRow(`SELECT body FROM assets WHERE id = ?`, id).Scan(&body)

	if errors.Is(err, sql.ErrNoRows) {
		return ErrAssetNotFound
	} else if err != nil {
		return fmt.Errorf("selecting from assets: %w", err)
	}

	if _, err := io.Copy(into, bytes.NewReader(body)); err != nil {
		return fmt.Errorf("writing body to io.Writer: %w", err)
	}

	return nil
}

func (s *assetStore) Delete(id string) error {
	_, err := s.db.Exec(`DELETE FROM assets WHERE id = ?`, id)
	return err
}

////////////////////////////////////////////////////////////////////////////////

type cachedAssetStore struct {
	inner AssetStore
	m     sync.Map
}

// NewCachedAssetStore wraps an AssetStore in an in-memory cache.
func NewCachedAssetStore(assetStore AssetStore) AssetStore {
	return &cachedAssetStore{
		inner: assetStore,
	}
}

func (s *cachedAssetStore) Set(id string, from io.Reader) error {

	buf := new(bytes.Buffer)
	from = io.TeeReader(from, buf)

	if err := s.inner.Set(id, from); err != nil {
		return err
	}

	s.m.Store(id, buf.Bytes())
	return nil
}

func (s *cachedAssetStore) Get(id string, into io.Writer) error {

	if bodyI, ok := s.m.Load(id); ok {

		if err, ok := bodyI.(error); ok {
			return err
		}

		if _, err := io.Copy(into, bytes.NewReader(bodyI.([]byte))); err != nil {
			return fmt.Errorf("writing body to io.Writer: %w", err)
		}

		return nil
	}

	buf := new(bytes.Buffer)
	into = io.MultiWriter(into, buf)

	if err := s.inner.Get(id, into); errors.Is(err, ErrAssetNotFound) {
		s.m.Store(id, err)
		return err
	} else if err != nil {
		return err
	}

	s.m.Store(id, buf.Bytes())
	return nil
}

func (s *cachedAssetStore) Delete(id string) error {

	if err := s.inner.Delete(id); err != nil {
		return err
	}

	s.m.Delete(id)
	return nil
}