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
|
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
|
// Save stores an image and returns its ID
|
||||||
func (s *Store) Save(data []byte, contentType string) string {
|
func (s *Store) Save(data []byte, contentType string) string {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
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{
|
s.images[id] = &Image{
|
||||||
Data: data,
|
Data: data,
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
|
Reference in New Issue
Block a user