github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/files/note_image.go (about) 1 package files 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/cozy/cozy-stack/model/instance" 9 "github.com/cozy/cozy-stack/model/note" 10 "github.com/cozy/cozy-stack/model/vfs" 11 "github.com/cozy/cozy-stack/pkg/couchdb" 12 "github.com/cozy/cozy-stack/pkg/jsonapi" 13 ) 14 15 type apiNoteImage struct { 16 inst *instance.Instance 17 doc *note.Image 18 } 19 20 // NewNoteImage creates an object that can be used to serialize an image for a 21 // note to JSON-API. 22 func NewNoteImage(inst *instance.Instance, img *note.Image) *apiNoteImage { 23 return &apiNoteImage{inst: inst, doc: img} 24 } 25 26 func (img *apiNoteImage) ID() string { return img.doc.ID() } 27 func (img *apiNoteImage) Rev() string { return img.doc.Rev() } 28 func (img *apiNoteImage) SetID(id string) { img.doc.SetID(id) } 29 func (img *apiNoteImage) SetRev(rev string) { img.doc.SetRev(rev) } 30 func (img *apiNoteImage) DocType() string { return img.doc.DocType() } 31 func (img *apiNoteImage) Clone() couchdb.Doc { cloned := *img; return &cloned } 32 func (img *apiNoteImage) MarshalJSON() ([]byte, error) { return json.Marshal(img.doc) } 33 func (img *apiNoteImage) Relationships() jsonapi.RelationshipMap { return nil } 34 func (img *apiNoteImage) Included() []jsonapi.Object { return nil } 35 func (img *apiNoteImage) Links() *jsonapi.LinksList { 36 links := jsonapi.LinksList{} 37 if secret, err := vfs.GetStore().AddThumb(img.inst, img.doc.ID()); err == nil { 38 parts := strings.SplitN(img.doc.ID(), "/", 2) 39 links.Self = fmt.Sprintf("/notes/%s/images/%s/%s", parts[0], parts[1], secret) 40 } 41 return &links 42 } 43 44 var _ jsonapi.Object = (*apiNoteImage)(nil)