US10: Make it possible for horse information to be displayed on the view page, add a get image method to the API

This commit is contained in:
2020-03-28 10:43:15 +01:00
parent 3cc36466df
commit 4be964b537
8 changed files with 127 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
@@ -146,4 +147,24 @@ public class HorseEndpoint {
"Something went wrong while uploading the file");
}
}
@RequestMapping(value = "/image/{path}", produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE}, method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody byte[] getImage(@PathVariable("path") String path) {
// Get the image as byte array and return it with a media type assigned
// https://www.baeldung.com/spring-controller-return-image-file
// https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/MediaType.html
LOGGER.info("GET " + BASE_URL + "/image/{}", path);
try {
return horseService.getImage(path);
} catch(ValidationException e) {
LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Unsupported file type provided. Supported file types are jpg and png.");
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Something went wrong while reading the file");
}
}
}

View File

@@ -18,4 +18,12 @@ public interface FileDao {
* @throws IOException if something goes wrong with deleting the file
*/
void delete(String fileName) throws IOException;
/**
* Used fot getting a file from the local filesystem
* @param fileName of the file to open
* @return the file as bytes
* @throws IOException if something goes wrong during file access
*/
byte[] get(String fileName) throws IOException;
}

View File

@@ -63,4 +63,21 @@ public class HorseFileDao implements FileDao {
throw new IOException("Deleting the file specified failed");
}
}
@Override
public byte[] get(String fileName) throws IOException {
if(fileName == null || fileName.equals(""))
throw new IOException("Cannot open an unexisting file");
LOGGER.trace("Getting file from " + FILE_BASE_PATH + fileName);
try {
// Read the file and return a byte array
// https://stackoverflow.com/a/5083666
File imageFile = new File(FILE_BASE_PATH + fileName);
return Files.readAllBytes(imageFile.toPath());
} catch(Exception e) {
LOGGER.error(e.getMessage());
throw new IOException("Opening the file specified failed");
}
}
}

View File

@@ -64,4 +64,12 @@ public interface HorseService {
* @throws ValidationException will be thrown if the file is in the incorrect format
*/
void saveImage(MultipartFile img) throws IOException, ValidationException;
/**
* @param path of the image to get
* @return the image
* @throws IOException will be thrown if the file could not be accessed
* @throws ValidationException will be thrown if the path does not end with .jpg, .jpeg or .png
*/
byte[] getImage(String path) throws IOException, ValidationException;
}

View File

@@ -78,4 +78,11 @@ public class SimpleHorseService implements HorseService {
LOGGER.trace("saveImage({})", img.getName());
horseFileDao.save(img);
}
@Override
public byte[] getImage(String path) throws IOException, ValidationException {
this.validator.validateImageRequest(path);
LOGGER.trace("getImage({})", path);
return horseFileDao.get(path);
}
}

View File

@@ -68,6 +68,15 @@ public class Validator {
}
}
public void validateImageRequest(String path) throws ValidationException {
if(path == null || path.isEmpty()) {
throw new ValidationException("No image path supplied");
}
if(!path.endsWith(".png") && !path.endsWith(".jpg") && !path.endsWith(".jpeg")) {
throw new ValidationException("Unsupported file extension supplied.");
}
}
public void validateHorseFilter(Map<String, String> filters) throws ValidationException {
if(filters.get("score") != null) {
try {