Skip to content

Commit c774e7f

Browse files
authored
Merge pull request #1483 from nono/dont-recreate-thumbnails
Do not recreate thumbnails if not needed
2 parents 8eeb76e + 9551e9b commit c774e7f

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

pkg/vfs/vfsafero/impl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ func (f *aferoFileCreation) Close() (err error) {
775775
}
776776

777777
newdoc, olddoc, written := f.newdoc, f.olddoc, f.w
778+
if olddoc == nil {
779+
olddoc = newdoc.Clone().(*vfs.FileDoc)
780+
}
778781

779782
if f.meta != nil {
780783
if errc := (*f.meta).Close(); errc == nil {
@@ -806,12 +809,9 @@ func (f *aferoFileCreation) Close() (err error) {
806809
// The document is already added to the index when closing the file creation
807810
// handler. When updating the content of the document with the final
808811
// informations (size, md5, ...) we can reuse the same document as olddoc.
809-
if olddoc == nil || !olddoc.Trashed {
812+
if f.olddoc == nil || !f.olddoc.Trashed {
810813
newdoc.Trashed = false
811814
}
812-
if olddoc == nil {
813-
olddoc = newdoc.Clone().(*vfs.FileDoc)
814-
}
815815
lockerr := f.afs.mu.Lock()
816816
if lockerr != nil {
817817
return lockerr

pkg/vfs/vfsswift/impl_v1.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,10 @@ func (f *swiftFileCreation) Close() (err error) {
841841
}
842842

843843
newdoc, olddoc, written := f.newdoc, f.olddoc, f.w
844+
if olddoc == nil {
845+
olddoc = newdoc.Clone().(*vfs.FileDoc)
846+
}
847+
844848
if f.meta != nil {
845849
if errc := (*f.meta).Close(); errc == nil {
846850
newdoc.Metadata = (*f.meta).Result()
@@ -886,12 +890,9 @@ func (f *swiftFileCreation) Close() (err error) {
886890
// The document is already added to the index when closing the file creation
887891
// handler. When updating the content of the document with the final
888892
// informations (size, md5, ...) we can reuse the same document as olddoc.
889-
if olddoc == nil || !olddoc.Trashed {
893+
if f.olddoc == nil || !f.olddoc.Trashed {
890894
newdoc.Trashed = false
891895
}
892-
if olddoc == nil {
893-
olddoc = newdoc.Clone().(*vfs.FileDoc)
894-
}
895896
lockerr := f.fs.mu.Lock()
896897
if lockerr != nil {
897898
return lockerr

pkg/vfs/vfsswift/impl_v2.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ func (f *swiftFileCreationV2) Close() (err error) {
723723
}
724724

725725
newdoc, olddoc, written := f.newdoc, f.olddoc, f.w
726+
if olddoc == nil {
727+
olddoc = newdoc.Clone().(*vfs.FileDoc)
728+
}
729+
726730
if f.meta != nil {
727731
if errc := (*f.meta).Close(); errc == nil {
728732
newdoc.Metadata = (*f.meta).Result()
@@ -768,12 +772,9 @@ func (f *swiftFileCreationV2) Close() (err error) {
768772
// The document is already added to the index when closing the file creation
769773
// handler. When updating the content of the document with the final
770774
// informations (size, md5, ...) we can reuse the same document as olddoc.
771-
if olddoc == nil || !olddoc.Trashed {
775+
if f.olddoc == nil || !f.olddoc.Trashed {
772776
newdoc.Trashed = false
773777
}
774-
if olddoc == nil {
775-
olddoc = newdoc.Clone().(*vfs.FileDoc)
776-
}
777778
lockerr := f.fs.mu.Lock()
778779
if lockerr != nil {
779780
return lockerr

pkg/workers/thumbnail/thumbnail.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func Worker(ctx *jobs.WorkerContext) error {
6262
if img.Verb != "DELETED" && img.Doc.Trashed {
6363
return nil
6464
}
65+
if img.OldDoc != nil && sameImg(&img.Doc, img.OldDoc) {
66+
return nil
67+
}
6568

6669
log := ctx.Logger()
6770
log.WithField("nspace", "thumbnail").Debugf("%s %s", img.Verb, img.Doc.ID())
@@ -83,6 +86,20 @@ func Worker(ctx *jobs.WorkerContext) error {
8386
return fmt.Errorf("Unknown type %s for image event", img.Verb)
8487
}
8588

89+
func sameImg(doc, old *vfs.FileDoc) bool {
90+
// XXX It is needed for a file that has just been uploaded. The first
91+
// revision will have the size and md5sum, but is marked as trashed,
92+
// and we have to wait for the second revision to have the file to generate
93+
// the thumbnails
94+
if doc.Trashed != old.Trashed {
95+
return false
96+
}
97+
if doc.ByteSize != old.ByteSize {
98+
return false
99+
}
100+
return bytes.Equal(doc.MD5Sum, old.MD5Sum)
101+
}
102+
86103
type thumbnailMsg struct {
87104
WithMetadata bool `json:"with_metadata"`
88105
}

0 commit comments

Comments
 (0)