-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data controller for image content itself
- Loading branch information
Showing
5 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/com/efimchick/gallery/controller/DataController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/main/java/com/efimchick/gallery/service/DataService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/com/efimchick/gallery/controller/DataControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")))); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters