oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "bytes"
5 "context"
6 "errors"
7
8 "gorm.io/gorm"
9)
10
11// The image step's other side (Notion import spec §12), over Mapa
12// images (images spec §7): AddImage and CopyImage, as the runner, the
13// in-process path with no HTTP; Image.Markdown for the line; and the
14// images spec's removal for a page's pending images the write did not
15// attach. Where no object store is bound, Ready refuses with
16// ErrNoImageStore's sentence before anything is downloaded, as the
17// seam did before Mapa images landed.
18
19// storeImages is the ImageStore over the app's Store.
20type storeImages struct{ store *Store }
21
22// addedImage is one pending image the run added or copied.
23type addedImage struct{ image Image }
24
25func (added addedImage) PublicID() string { return added.image.PublicID }
26
27func (added addedImage) Side() int {
28 return max(added.image.Width, added.image.Height)
29}
30
31func (added addedImage) Markdown(alt string) string {
32 return added.image.Markdown(alt)
33}
34
35func (images storeImages) Ready(context.Context) error {
36 if _, err := images.store.objects(); err != nil {
37 return errNoImageStore
38 }
39 return nil
40}
41
42func (images storeImages) Add(ctx context.Context, who string,
43 data []byte) (AddedImage, error) {
44 actor, err := images.store.known(ctx, byAddress(who))
45 if err != nil {
46 return nil, err
47 }
48 image, err := images.store.AddImage(ctx, actor, bytes.NewReader(data))
49 return addedImage{image}, err
50}
51
52func (images storeImages) Copy(ctx context.Context, who,
53 publicID string) (AddedImage, error) {
54 actor, err := images.store.known(ctx, byAddress(who))
55 if err != nil {
56 return nil, err
57 }
58 image, err := images.store.CopyImage(ctx, actor, publicID)
59 return addedImage{image}, err
60}
61
62// Discard removes a page's pending images at once, as a removal by the
63// runner: nothing holds quota for seven days (spec §12).
64func (images storeImages) Discard(ctx context.Context, who string,
65 added []AddedImage) error {
66 made := copied{ids: map[string]string{}}
67 for _, each := range added {
68 made.ids[each.PublicID()] = each.PublicID()
69 }
70 images.store.dropCopies(ctx, byAddress(who), made)
71 return nil
72}
73
74// Held is whether an image is still there and attached to the page: a
75// reused line must name an image the page holds, not one the sweep or a
76// teammate's removal took away (spec §12).
77func (images storeImages) Held(ctx context.Context, publicID string,
78 pageID int64) bool {
79 var row ImageRow
80 err := images.store.database.WithContext(ctx).
81 Where("public_id = ? AND page_id = ?", publicID, pageID).
82 First(&row).Error
83 return err == nil
84}
85
86func (images storeImages) Markdown(publicID, alt string) string {
87 return Image{PublicID: publicID}.Markdown(alt)
88}
89
90// quotaReason names which allowance a quota refusal came from.
91func quotaReason(err error) string {
92 var refusal quotaError
93 if errors.As(err, &refusal) && refusal.team {
94 return "the team's image allowance is full"
95 }
96 return "your own image allowance is full"
97}
98
99// notionImageOf reads an image place by print.
100func notionImageOf(database *gorm.DB, fingerprint string) (NotionImage,
101 bool) {
102 var row NotionImage
103 err := database.Where("place_print = ?", fingerprint).First(&row).Error
104 return row, err == nil
105}
106