Attach extension to ID for known image types
This commit is contained in:
@@ -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,
|
||||
|
Reference in New Issue
Block a user