Attach extension to ID for known image types

This commit is contained in:
Carl Pearson
2025-05-30 05:51:59 -06:00
parent 1e65340652
commit 5da3d30817

View File

@@ -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,