diff --git a/store/store.go b/store/store.go index 08d3f9a..3d84eae 100644 --- a/store/store.go +++ b/store/store.go @@ -57,12 +57,32 @@ func (s *Store) Get(id string) (*Image, bool) { return image, true } +// isImageContentType checks if the content type is a valid image +func extFromContentType(contentType string) (string, bool) { + val, ok := map[string]string{ + "image/jpeg": "jpg", + "image/jpg": "jpg", + "image/png": "png", + "image/gif": "gif", + "image/webp": "webp", + "image/bmp": "bmp", + }[contentType] + return val, ok +} + // Save stores an image and returns its ID func (s *Store) Save(data []byte, contentType string) string { s.mu.Lock() defer s.mu.Unlock() - id := uuid.New().String() + ext, ok := extFromContentType(contentType) + if !ok { + ext = "" + } else { + ext = "." + ext + } + + id := uuid.New().String() + ext s.images[id] = &Image{ Data: data, ContentType: contentType,