Skip to content
Snippets Groups Projects
Commit 920865c8 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#510 - specify media type for response

parent 2dcecd58
No related branches found
No related tags found
No related merge requests found
package de.deadlocker8.budgetmaster.images;
import org.springframework.http.MediaType;
import java.util.Optional;
public enum ImageFileExtension
{
PNG("png", "png"),
JPG("jpg", "jpeg"),
SVG("svg", "svg+xml");
PNG("png", "png", MediaType.IMAGE_PNG),
JPG("jpg", "jpeg", MediaType.IMAGE_JPEG),
SVG("svg", "svg+xml", MediaType.valueOf("image/svg+xml"));
private final String fileExtension;
private final String base64Type;
private final MediaType mediaType;
ImageFileExtension(String fileExtension, String base64Type)
ImageFileExtension(String fileExtension, String base64Type, MediaType mediaType)
{
this.fileExtension = fileExtension;
this.base64Type = base64Type;
this.mediaType = mediaType;
}
public String getFileExtension()
......@@ -27,6 +31,11 @@ public enum ImageFileExtension
return base64Type;
}
public MediaType getMediaType()
{
return mediaType;
}
public static Optional<ImageFileExtension> getByExtension(String extension)
{
for(ImageFileExtension currentExtension : values())
......@@ -46,6 +55,7 @@ public enum ImageFileExtension
return "ImageFileExtension{" +
"fileExtension='" + fileExtension + '\'' +
", base64Type='" + base64Type + '\'' +
", mediaType=" + mediaType +
"} " + super.toString();
}
}
......@@ -9,6 +9,9 @@ import de.deadlocker8.budgetmaster.utils.ResourceNotFoundException;
import de.thecodelabs.utils.util.Localization;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
......@@ -106,8 +109,7 @@ public class MediaController extends BaseController
}
@GetMapping("/getImageByIconID/{ID}")
@ResponseBody
public byte[] getImageByIconID(@PathVariable("ID") Integer iconID)
public ResponseEntity<byte[]> getImageByIconID(@PathVariable("ID") Integer iconID)
{
Optional<Icon> iconOptional = iconService.getRepository().findById(iconID);
if(iconOptional.isEmpty())
......@@ -115,6 +117,12 @@ public class MediaController extends BaseController
throw new ResourceNotFoundException();
}
return ArrayUtils.toPrimitive(iconOptional.get().getImage().getImage());
final Image image = iconOptional.get().getImage();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(image.getFileExtension().getMediaType());
final byte[] bytes = ArrayUtils.toPrimitive(image.getImage());
return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment