Skip to content

Commit

Permalink
Data controller for image content itself
Browse files Browse the repository at this point in the history
  • Loading branch information
thejerome committed Oct 18, 2017
1 parent 74a9a23 commit b34072d
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/main/java/com/efimchick/gallery/controller/DataController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.efimchick.gallery.controller;

import com.efimchick.gallery.service.DataService;
import com.google.common.io.Files;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static java.util.Collections.singletonList;

/**
* Created by Evgenii_Efimchik on 18-Oct-17.
*/

@RestController
@RequestMapping("/data")
public class DataController {

private final DataService dataService;

@Autowired
public DataController(DataService dataService) {
this.dataService = dataService;
}

@GetMapping("/{id:.+}")
ResponseEntity<byte[]> image(@PathVariable("id") String id) {
return dataService.findImageDataById(id)
.map(inputStreamResource -> {
HttpHeaders headers = new HttpHeaders();
headers.put(HttpHeaders.CONTENT_TYPE, singletonList("image/" + Files.getFileExtension(id).toLowerCase()));
return new ResponseEntity(inputStreamResource, headers, HttpStatus.OK);
})
.orElseGet(() -> ResponseEntity.notFound().build());
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/efimchick/gallery/service/DataService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.efimchick.gallery.service;

import com.efimchick.gallery.domain.LocalImage;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static com.efimchick.gallery.domain.Utils.decodeString;


/**
* Created by Evgenii_Efimchik on 17-Oct-17.
*/

@Service
public class DataService {

public Optional<InputStreamResource> findImageDataById(String id) {

Path path = Paths.get(decodeString(id));
System.out.println(path);
return LocalImage.of(path)
.map(LocalImage::getPath)
.map(DataService::getFileInputStream)
.map(InputStreamResource::new);

}

private static InputStream getFileInputStream(Path path) {
try {
return Files.newInputStream(path);
} catch (IOException e) {
e.printStackTrace();
return null;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.efimchick.gallery.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.nio.file.Files;
import java.nio.file.Paths;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Created by Evgenii_Efimchik on 11-Oct-17.
*/

@RunWith(SpringRunner.class)
@SpringBootTest
public class DataControllerTest {

@Autowired
private WebApplicationContext applicationContext;

private MockMvc mockMvc;

@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
}


@Test
public void dataControllerMayFindExistingImage() throws Exception {
mockMvc.perform(get("/data/pictures_HQ%5CIMAG0686.jpg"))
.andExpect(status().isOk());
}

@Test
public void noSuchFilrLeadsTo404() throws Exception {
mockMvc.perform(get("/data/no_such_path"))
.andExpect(status().isNotFound());
}

@Test
public void dataControllerMayFindExistingImageIgnoringCase() throws Exception {
mockMvc.perform(get("/data/pictures_HQ%5CIMAG0686.JPG"))
.andExpect(status().isOk());
}

@Test
public void dataControllerFindsProperImage() throws Exception {
mockMvc.perform(get("/data/pictures_HQ%5CIMAG0686.jpg"))
.andExpect(status().isOk())
.andExpect(content().bytes(Files.readAllBytes(Paths.get("pictures_HQ", "IMAG0686.jpg"))));

mockMvc.perform(get("/data/pictures_HQ%5CIMAG0911.jpg"))
.andExpect(status().isOk())
.andExpect(content().bytes(Files.readAllBytes(Paths.get("pictures_HQ", "IMAG0911.jpg"))));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@SpringBootTest
public class DirControllerTest {


@Autowired
private WebApplicationContext applicationContext;

Expand All @@ -44,12 +43,12 @@ public void galleryDirsAreUp() throws Exception {
public void dirsAreProperlyFoundById() throws Exception {
mockMvc.perform(get("/dirs/pictures_HQ"))
.andExpect(status().isOk())
.andExpect(content().string("{\"name\":\"pictures_HQ\",\"fullName\":\"pictures_HQ\",\"_embedded\":{\"images\":[{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0686.jpg\"}}},{\"name\":\"IMAG0911.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0911.jpg\",\"size\":2164700,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0911.jpg\"}}}]},\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ\"}}}"))
.andExpect(content().string("{\"name\":\"pictures_HQ\",\"fullName\":\"pictures_HQ\",\"_embedded\":{\"images\":[{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0686.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CIMAG0686.jpg\"}}},{\"name\":\"IMAG0911.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0911.jpg\",\"size\":2164700,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0911.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CIMAG0911.jpg\"}}}],\"dirs\":[{\"name\":\"Carimate\",\"fullName\":\"pictures_HQ\\\\Carimate\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5CCarimate\"}}},{\"name\":\"empty\",\"fullName\":\"pictures_HQ\\\\empty\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5Cempty\"}}},{\"name\":\"light\",\"fullName\":\"pictures_HQ\\\\light\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5Clight\"}}},{\"name\":\"yellow\",\"fullName\":\"pictures_HQ\\\\yellow\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5Cyellow\"}}}]},\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ\"}}}"))
.andReturn();

mockMvc.perform(get("/dirs/pictures_HQ%5CCarimate"))
.andExpect(status().isOk())
.andExpect(content().string("{\"name\":\"Carimate\",\"fullName\":\"pictures_HQ\\\\Carimate\",\"_embedded\":{\"images\":[{\"name\":\"IMAG0674.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0674.jpg\",\"size\":2689467,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0674.jpg\"}}},{\"name\":\"IMAG0676.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0676.jpg\",\"size\":2626845,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0676.jpg\"}}},{\"name\":\"IMAG0678.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0678.jpg\",\"size\":2942016,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0678.jpg\"}}},{\"name\":\"IMAG0681.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0681.jpg\",\"size\":1825480,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0681.jpg\"}}},{\"name\":\"IMAG0684.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0684.jpg\",\"size\":2416295,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0684.jpg\"}}},{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0686.jpg\"}}}]},\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5CCarimate\"}}}"))
.andExpect(content().string("{\"name\":\"Carimate\",\"fullName\":\"pictures_HQ\\\\Carimate\",\"_embedded\":{\"images\":[{\"name\":\"IMAG0674.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0674.jpg\",\"size\":2689467,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0674.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CCarimate%5CIMAG0674.jpg\"}}},{\"name\":\"IMAG0676.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0676.jpg\",\"size\":2626845,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0676.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CCarimate%5CIMAG0676.jpg\"}}},{\"name\":\"IMAG0678.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0678.jpg\",\"size\":2942016,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0678.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CCarimate%5CIMAG0678.jpg\"}}},{\"name\":\"IMAG0681.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0681.jpg\",\"size\":1825480,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0681.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CCarimate%5CIMAG0681.jpg\"}}},{\"name\":\"IMAG0684.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0684.jpg\",\"size\":2416295,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0684.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CCarimate%5CIMAG0684.jpg\"}}},{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\Carimate\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CCarimate%5CIMAG0686.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CCarimate%5CIMAG0686.jpg\"}}}]},\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5CCarimate\"}}}"))
.andReturn();
}

Expand All @@ -72,9 +71,13 @@ public void noDirLeadsTo404() throws Exception {

@Test
public void rootCallPointsToRootDir() throws Exception {




mockMvc.perform(get("/dirs"))
.andExpect(status().isOk())
.andExpect(content().string("{\"name\":\"pictures_HQ\",\"fullName\":\"pictures_HQ\",\"_embedded\":{\"images\":[{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0686.jpg\"}}},{\"name\":\"IMAG0911.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0911.jpg\",\"size\":2164700,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0911.jpg\"}}}]},\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ\"}}}"))
.andExpect(content().string("{\"name\":\"pictures_HQ\",\"fullName\":\"pictures_HQ\",\"_embedded\":{\"images\":[{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0686.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CIMAG0686.jpg\"}}},{\"name\":\"IMAG0911.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0911.jpg\",\"size\":2164700,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0911.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CIMAG0911.jpg\"}}}],\"dirs\":[{\"name\":\"Carimate\",\"fullName\":\"pictures_HQ\\\\Carimate\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5CCarimate\"}}},{\"name\":\"empty\",\"fullName\":\"pictures_HQ\\\\empty\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5Cempty\"}}},{\"name\":\"light\",\"fullName\":\"pictures_HQ\\\\light\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5Clight\"}}},{\"name\":\"yellow\",\"fullName\":\"pictures_HQ\\\\yellow\",\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ%5Cyellow\"}}}]},\"_links\":{\"self\":{\"href\":\"http://localhost/dirs/pictures_HQ\"}}}"))
.andReturn();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public void setUp() throws Exception {
public void galleryImageIdFound() throws Exception {
mockMvc.perform(get("/images/pictures_HQ%5CIMAG0686.jpg"))
.andExpect(status().isOk())
.andExpect(content().string("{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0686.jpg\"}}}"))
.andExpect(content().string("{\"name\":\"IMAG0686.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0686.jpg\",\"size\":3175908,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0686.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CIMAG0686.jpg\"}}}"))
.andReturn();

mockMvc.perform(get("/images/pictures_HQ%5CIMAG0911.jpg"))
.andExpect(status().isOk())
.andExpect(content().string("{\"name\":\"IMAG0911.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0911.jpg\",\"size\":2164700,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0911.jpg\"}}}"))
.andExpect(content().string("{\"name\":\"IMAG0911.jpg\",\"fullName\":\"pictures_HQ\\\\IMAG0911.jpg\",\"size\":2164700,\"width\":4224,\"height\":2368,\"_links\":{\"self\":{\"href\":\"http://localhost/images/pictures_HQ%5CIMAG0911.jpg\"},\"data\":{\"href\":\"http://localhost/data/pictures_HQ%5CIMAG0911.jpg\"}}}"))
.andReturn();
}

Expand Down

0 comments on commit b34072d

Please sign in to comment.