Skip to content

Commit de61fe0

Browse files
authored
fix: blob race condition (#619)
* fix: blob race condition * fix: blob race condition
1 parent b0994f6 commit de61fe0

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

imagor.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (app *Imagor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
208208
return
209209
}
210210
app.setResponseHeaders(w, r, blob, p)
211-
if checkStatNotModified(w, r, blob.Stat) {
211+
if blob != nil && checkStatNotModified(w, r, blob.Stat) {
212212
w.WriteHeader(http.StatusNotModified)
213213
return
214214
}
@@ -779,6 +779,10 @@ func (app *Imagor) suppress(
779779

780780
// setResponseHeaders sets common response headers for blob responses
781781
func (app *Imagor) setResponseHeaders(w http.ResponseWriter, r *http.Request, blob *Blob, p imagorpath.Params) {
782+
if blob == nil {
783+
w.Header().Set("Content-Type", "application/octet-stream")
784+
return
785+
}
782786
w.Header().Set("Content-Type", blob.ContentType())
783787
w.Header().Set("Content-Disposition", getContentDisposition(p, blob))
784788
setCacheHeaders(w, r, getTtl(p, app.CacheHeaderTTL), app.CacheHeaderSWR)
@@ -966,9 +970,11 @@ func getContentDisposition(p imagorpath.Params, blob *Blob) string {
966970
_, filename = filepath.Split(p.Image)
967971
}
968972
filename = strings.ReplaceAll(filename, `"`, "%22")
969-
if ext := getExtension(blob.BlobType()); ext != "" &&
970-
!(ext == ".jpg" && strings.HasSuffix(filename, ".jpeg")) {
971-
filename = strings.TrimSuffix(filename, ext) + ext
973+
if blob != nil {
974+
if ext := getExtension(blob.BlobType()); ext != "" &&
975+
!(ext == ".jpg" && strings.HasSuffix(filename, ".jpeg")) {
976+
filename = strings.TrimSuffix(filename, ext) + ext
977+
}
972978
}
973979
return fmt.Sprintf(`attachment; filename="%s"`, filename)
974980
}

storage/filestorage/filestorage.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ func (s *FileStorage) Get(_ *http.Request, image string) (*imagor.Blob, error) {
6666
if !ok {
6767
return nil, imagor.ErrInvalid
6868
}
69-
return imagor.NewBlobFromFile(image, func(stat os.FileInfo) error {
69+
blob := imagor.NewBlobFromFile(image, func(stat os.FileInfo) error {
7070
if s.Expiration > 0 && time.Now().Sub(stat.ModTime()) > s.Expiration {
7171
return imagor.ErrExpired
7272
}
7373
return nil
74-
}), nil
74+
})
75+
return blob, blob.Err()
7576
}
7677

7778
// Put implements imagor.Storage interface

storage/filestorage/filestorage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestFileStorage_Load_Save(t *testing.T) {
198198
})
199199

200200
t.Run("expiration", func(t *testing.T) {
201-
s := New(dir, WithExpiration(time.Millisecond*10))
201+
s := New(dir, WithExpiration(time.Millisecond*100))
202202
var err error
203203

204204
_, err = checkBlob(s.Get(r, "/foo/bar/asdf"))

storage/gcloudstorage/gcloudstorage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (s *GCloudStorage) Get(r *http.Request, image string) (imageData *imagor.Bl
7575
ModifiedTime: attrs.Updated,
7676
}
7777
}
78-
return blob, err
78+
return blob, blob.Err()
7979
}
8080

8181
// Put implements imagor.Storage interface

storage/gcloudstorage/gcloudstorage_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,5 @@ func TestContextCancel(t *testing.T) {
201201
assert.Equal(t, "bar", string(buf))
202202
cancel()
203203
b, err = s.Get(r, "/foo/bar/asdf")
204-
require.NoError(t, err)
205-
buf, err = b.ReadAll()
206-
assert.Empty(t, buf)
207204
require.ErrorIs(t, err, context.Canceled)
208205
}

0 commit comments

Comments
 (0)