Skip to content

Commit 663e49b

Browse files
committed
Keep the original image for a note
1 parent 96a688b commit 663e49b

File tree

9 files changed

+84
-49
lines changed

9 files changed

+84
-49
lines changed

model/note/delete.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ func deleteNote(db prefixer.Prefixer, noteID string) {
1616
go func() {
1717
images, err := getImages(db, noteID)
1818
if err == nil {
19+
formats := []string{
20+
consts.NoteImageOriginalFormat,
21+
consts.NoteImageThumbFormat,
22+
}
1923
for _, img := range images {
2024
inst := &instance.Instance{
2125
Domain: db.DomainName(),
2226
Prefix: db.DBPrefix(),
2327
}
24-
_ = inst.ThumbsFS().RemoveNoteThumb(img.ID())
28+
_ = inst.ThumbsFS().RemoveNoteThumb(img.ID(), formats)
2529
_ = couchdb.DeleteDoc(db, img)
2630
}
2731
}

model/note/image.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func NewImageUpload(inst *instance.Instance, note *vfs.FileDoc, name, mime strin
7777
md.CreatedByApp = consts.NotesSlug
7878
img := &Image{DocID: id, Name: name, Mime: mime, Metadata: *md}
7979

80-
thumb, err := inst.ThumbsFS().CreateNoteThumb(id, mime)
80+
thumb, err := inst.ThumbsFS().CreateNoteThumb(id, mime, consts.NoteImageOriginalFormat)
8181
if err != nil {
8282
return nil, err
8383
}
@@ -153,7 +153,8 @@ func (u *ImageUpload) Close() error {
153153

154154
// Save in CouchDB
155155
if err := couchdb.CreateNamedDocWithDB(u.inst, u.Image); err != nil {
156-
_ = u.inst.ThumbsFS().RemoveNoteThumb(u.Image.ID())
156+
formats := []string{consts.NoteImageOriginalFormat}
157+
_ = u.inst.ThumbsFS().RemoveNoteThumb(u.Image.ID(), formats)
157158
return err
158159
}
159160

@@ -214,13 +215,17 @@ func getImages(db prefixer.Prefixer, fileID string) ([]*Image, error) {
214215
// features like cut/paste or undo to have a short grace time when the image
215216
// can be removed from the markdown and reinserted a few seconds later.
216217
func cleanImages(inst *instance.Instance, images []*Image) {
218+
formats := []string{
219+
consts.NoteImageOriginalFormat,
220+
consts.NoteImageThumbFormat,
221+
}
217222
for _, img := range images {
218223
if img.ToRemove {
219224
if img.seen {
220225
img.ToRemove = false
221226
_ = couchdb.UpdateDoc(inst, img)
222227
} else {
223-
_ = inst.ThumbsFS().RemoveNoteThumb(img.ID())
228+
_ = inst.ThumbsFS().RemoveNoteThumb(img.ID(), formats)
224229
_ = couchdb.DeleteDoc(inst, img)
225230
}
226231
} else if !img.seen {

model/note/note.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func buildArchive(inst *instance.Instance, md []byte, images []*Image) ([]byte,
440440
if !image.seen {
441441
continue
442442
}
443-
th, err := fs.OpenNoteThumb(image.ID())
443+
th, err := fs.OpenNoteThumb(image.ID(), consts.NoteImageOriginalFormat)
444444
if err != nil {
445445
return nil, err
446446
}

model/vfs/vfs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ type Thumbser interface {
256256
ServeThumbContent(w http.ResponseWriter, req *http.Request,
257257
img *FileDoc, format string) error
258258

259-
CreateNoteThumb(id, mime string) (ThumbFiler, error)
260-
OpenNoteThumb(id string) (io.ReadCloser, error)
261-
RemoveNoteThumb(id string) error
259+
CreateNoteThumb(id, mime, format string) (ThumbFiler, error)
260+
OpenNoteThumb(id, format string) (io.ReadCloser, error)
261+
RemoveNoteThumb(id string, formats []string) error
262262
ServeNoteThumbContent(w http.ResponseWriter, req *http.Request, id string) error
263263
}
264264

model/vfs/vfsafero/thumbs.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
"path"
99

1010
"github.com/cozy/cozy-stack/model/vfs"
11+
"github.com/cozy/cozy-stack/pkg/consts"
1112
multierror "github.com/hashicorp/go-multierror"
1213
"github.com/spf13/afero"
1314
)
1415

15-
const noteThumbFormat = "note"
16-
1716
// NewThumbsFs creates a new thumb filesystem base on a afero.Fs.
1817
func NewThumbsFs(fs afero.Fs) vfs.Thumbser {
1918
return &thumbs{fs}
@@ -98,8 +97,8 @@ func (t *thumbs) ServeThumbContent(w http.ResponseWriter, req *http.Request,
9897
return nil
9998
}
10099

101-
func (t *thumbs) CreateNoteThumb(id, mime string) (vfs.ThumbFiler, error) {
102-
newname := t.makeName(id, noteThumbFormat)
100+
func (t *thumbs) CreateNoteThumb(id, mime, format string) (vfs.ThumbFiler, error) {
101+
newname := t.makeName(id, format)
103102
dir := path.Dir(newname)
104103
if base := dir; base != "." {
105104
if err := t.fs.MkdirAll(dir, 0755); err != nil {
@@ -120,24 +119,31 @@ func (t *thumbs) CreateNoteThumb(id, mime string) (vfs.ThumbFiler, error) {
120119
return th, nil
121120
}
122121

123-
func (t *thumbs) OpenNoteThumb(id string) (io.ReadCloser, error) {
124-
name := t.makeName(id, noteThumbFormat)
122+
func (t *thumbs) OpenNoteThumb(id, format string) (io.ReadCloser, error) {
123+
name := t.makeName(id, format)
125124
return t.fs.Open(name)
126125
}
127126

128-
func (t *thumbs) RemoveNoteThumb(id string) error {
129-
err := t.fs.Remove(t.makeName(id, noteThumbFormat))
130-
if err != nil && !os.IsNotExist(err) {
131-
return err
127+
func (t *thumbs) RemoveNoteThumb(id string, formats []string) error {
128+
var errm error
129+
for _, format := range formats {
130+
err := t.fs.Remove(t.makeName(id, format))
131+
if err != nil && !os.IsNotExist(err) {
132+
errm = multierror.Append(errm, err)
133+
}
132134
}
133-
return nil
135+
return errm
134136
}
135137

136138
func (t *thumbs) ServeNoteThumbContent(w http.ResponseWriter, req *http.Request, id string) error {
137-
name := t.makeName(id, noteThumbFormat)
139+
name := t.makeName(id, consts.NoteImageThumbFormat)
138140
s, err := t.fs.Stat(name)
139141
if err != nil {
140-
return err
142+
name = t.makeName(id, consts.NoteImageOriginalFormat)
143+
s, err = t.fs.Stat(name)
144+
if err != nil {
145+
return err
146+
}
141147
}
142148
f, err := t.fs.Open(name)
143149
if err != nil {

model/vfs/vfsswift/thumbs_v1.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/cozy/cozy-stack/model/vfs"
11+
"github.com/cozy/cozy-stack/pkg/consts"
1112
"github.com/ncw/swift"
1213
)
1314

@@ -75,11 +76,11 @@ func (t *thumbs) ServeThumbContent(w http.ResponseWriter, req *http.Request, img
7576
return nil
7677
}
7778

78-
func (t *thumbs) CreateNoteThumb(id, mime string) (vfs.ThumbFiler, error) {
79+
func (t *thumbs) CreateNoteThumb(id, mime, format string) (vfs.ThumbFiler, error) {
7980
if err := t.c.ContainerCreate(t.container, nil); err != nil {
8081
return nil, err
8182
}
82-
name := t.makeName(id, noteThumbFormat)
83+
name := t.makeName(id, format)
8384
obj, err := t.c.ObjectCreate(t.container, name, true, "", "", nil)
8485
if err != nil {
8586
return nil, err
@@ -93,8 +94,8 @@ func (t *thumbs) CreateNoteThumb(id, mime string) (vfs.ThumbFiler, error) {
9394
return th, nil
9495
}
9596

96-
func (t *thumbs) OpenNoteThumb(id string) (io.ReadCloser, error) {
97-
name := t.makeName(id, noteThumbFormat)
97+
func (t *thumbs) OpenNoteThumb(id, format string) (io.ReadCloser, error) {
98+
name := t.makeName(id, format)
9899
obj, _, err := t.c.ObjectOpen(t.container, name, false, nil)
99100
if err == swift.ObjectNotFound {
100101
return nil, os.ErrNotExist
@@ -105,16 +106,24 @@ func (t *thumbs) OpenNoteThumb(id string) (io.ReadCloser, error) {
105106
return obj, nil
106107
}
107108

108-
func (t *thumbs) RemoveNoteThumb(id string) error {
109-
objName := t.makeName(id, noteThumbFormat)
110-
return t.c.ObjectDelete(t.container, objName)
109+
func (t *thumbs) RemoveNoteThumb(id string, formats []string) error {
110+
objNames := make([]string, len(formats))
111+
for i, format := range formats {
112+
objNames[i] = t.makeName(id, format)
113+
}
114+
_, err := t.c.BulkDelete(t.container, objNames)
115+
return err
111116
}
112117

113118
func (t *thumbs) ServeNoteThumbContent(w http.ResponseWriter, req *http.Request, id string) error {
114-
name := t.makeName(id, noteThumbFormat)
119+
name := t.makeName(id, consts.NoteImageThumbFormat)
115120
f, o, err := t.c.ObjectOpen(t.container, name, false, nil)
116121
if err != nil {
117-
return wrapSwiftErr(err)
122+
name = t.makeName(id, consts.NoteImageOriginalFormat)
123+
f, o, err = t.c.ObjectOpen(t.container, name, false, nil)
124+
if err != nil {
125+
return wrapSwiftErr(err)
126+
}
118127
}
119128
defer f.Close()
120129

model/vfs/vfsswift/thumbs_v2_v3.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
"time"
1111

1212
"github.com/cozy/cozy-stack/model/vfs"
13+
"github.com/cozy/cozy-stack/pkg/consts"
1314
"github.com/cozy/cozy-stack/pkg/prefixer"
1415
"github.com/ncw/swift"
1516
)
1617

17-
const noteThumbFormat = "note"
18-
1918
var unixEpochZero = time.Time{}
2019

2120
// NewThumbsFsV2 creates a new thumb filesystem base on swift.
@@ -131,8 +130,8 @@ func (t *thumbsV2) ServeThumbContent(w http.ResponseWriter, req *http.Request, i
131130
return nil
132131
}
133132

134-
func (t *thumbsV2) CreateNoteThumb(id, mime string) (vfs.ThumbFiler, error) {
135-
name := t.makeName(id, noteThumbFormat)
133+
func (t *thumbsV2) CreateNoteThumb(id, mime, format string) (vfs.ThumbFiler, error) {
134+
name := t.makeName(id, format)
136135
obj, err := t.c.ObjectCreate(t.container, name, true, "", mime, nil)
137136
if err != nil {
138137
if _, _, errc := t.c.Container(t.container); errc == swift.ContainerNotFound {
@@ -152,8 +151,8 @@ func (t *thumbsV2) CreateNoteThumb(id, mime string) (vfs.ThumbFiler, error) {
152151
return th, nil
153152
}
154153

155-
func (t *thumbsV2) OpenNoteThumb(id string) (io.ReadCloser, error) {
156-
name := t.makeName(id, noteThumbFormat)
154+
func (t *thumbsV2) OpenNoteThumb(id, format string) (io.ReadCloser, error) {
155+
name := t.makeName(id, format)
157156
obj, _, err := t.c.ObjectOpen(t.container, name, false, nil)
158157
if err == swift.ObjectNotFound {
159158
return nil, os.ErrNotExist
@@ -164,16 +163,24 @@ func (t *thumbsV2) OpenNoteThumb(id string) (io.ReadCloser, error) {
164163
return obj, nil
165164
}
166165

167-
func (t *thumbsV2) RemoveNoteThumb(id string) error {
168-
objName := t.makeName(id, noteThumbFormat)
169-
return t.c.ObjectDelete(t.container, objName)
166+
func (t *thumbsV2) RemoveNoteThumb(id string, formats []string) error {
167+
objNames := make([]string, len(formats))
168+
for i, format := range formats {
169+
objNames[i] = t.makeName(id, format)
170+
}
171+
_, err := t.c.BulkDelete(t.container, objNames)
172+
return err
170173
}
171174

172175
func (t *thumbsV2) ServeNoteThumbContent(w http.ResponseWriter, req *http.Request, id string) error {
173-
name := t.makeName(id, noteThumbFormat)
176+
name := t.makeName(id, consts.NoteImageThumbFormat)
174177
f, o, err := t.c.ObjectOpen(t.container, name, false, nil)
175178
if err != nil {
176-
return wrapSwiftErr(err)
179+
name = t.makeName(id, consts.NoteImageOriginalFormat)
180+
f, o, err = t.c.ObjectOpen(t.container, name, false, nil)
181+
if err != nil {
182+
return wrapSwiftErr(err)
183+
}
177184
}
178185
defer f.Close()
179186

pkg/consts/file.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ const (
3333
// ElectronicSafeKey is the metadata key for an electronic safe (certified)
3434
ElectronicSafeKey = "electronicSafe"
3535
)
36+
37+
const (
38+
// NoteImageOriginalFormat is the format for the original image in a note.
39+
NoteImageOriginalFormat = "original"
40+
// NoteImageThumbFormat is the format for a resized image in a note.
41+
NoteImageThumbFormat = "thumb"
42+
)

worker/thumbnail/thumbnail.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func removeThumbnails(i *instance.Instance, img *vfs.FileDoc) error {
327327

328328
func resizeNoteImage(ctx *job.WorkerContext, img *note.Image) error {
329329
fs := ctx.Instance.ThumbsFS()
330-
in, err := fs.OpenNoteThumb(img.ID())
330+
in, err := fs.OpenNoteThumb(img.ID(), consts.NoteImageOriginalFormat)
331331
if err != nil {
332332
return err
333333
}
@@ -348,7 +348,7 @@ func resizeNoteImage(ctx *job.WorkerContext, img *note.Image) error {
348348
}
349349

350350
var th vfs.ThumbFiler
351-
th, err = fs.CreateNoteThumb(img.ID(), "image/jpeg")
351+
th, err = fs.CreateNoteThumb(img.ID(), "image/jpeg", consts.NoteImageThumbFormat)
352352
if err != nil {
353353
return err
354354
}
@@ -362,16 +362,13 @@ func resizeNoteImage(ctx *job.WorkerContext, img *note.Image) error {
362362
return err
363363
}
364364

365-
img.Height = img.Height * note.MaxWidth / img.Width
366-
img.Width = note.MaxWidth
367365
img.ToResize = false
368-
img.Mime = "image/jpeg"
369366
_ = couchdb.UpdateDoc(ctx.Instance, img)
370367

371368
event := note.Event{
372-
"width": img.Width,
373-
"height": img.Height,
374-
"mime": img.Mime,
369+
"width": note.MaxWidth,
370+
"height": img.Height * note.MaxWidth / img.Width,
371+
"mime": "image/jpeg",
375372
"doctype": consts.NotesImages,
376373
}
377374
event.SetID(img.ID())

0 commit comments

Comments
 (0)