Skip to content

Commit

Permalink
Update imgs implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Avadem committed Mar 7, 2020
1 parent 1652a23 commit 088ea1f
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 9 deletions.
Binary file added practica/images/tournament/image-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added practica/images/tournament/image-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added practica/images/tournament/image-3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified practica/images/user/image-17.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public void saveImage(String folderName, long id, MultipartFile image) throws IO
Path newFile = createFilePath(id, folder);
image.transferTo(newFile);
}
public Path saveImagePath(String folderName, long id, MultipartFile image) throws IOException {
Path folder = FILES_FOLDER.resolve(folderName);
if (!Files.exists(folder)) {
Files.createDirectories(folder);
}
Path newFile = createFilePath(id, folder);
image.transferTo(newFile);
return newFile;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.practica.demo.Imgs;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Service
@Configuration
public class TournamentImageService implements WebMvcConfigurer {

private static final Path FILES_FOLDER = Paths.get(System.getProperty("user.dir"), "tournamentImages");

private Path createFilePath(long id, Path folder) {
return folder.resolve("image-" + id + ".jpg");
}

public void saveImage(String folderName, long id, MultipartFile image) throws IOException {
Path folder = FILES_FOLDER.resolve(folderName);
if (!Files.exists(folder)) {
Files.createDirectories(folder);
}
Path newFile = createFilePath(id, folder);
image.transferTo(newFile);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/tournamentImages/**")
.addResourceLocations("file:" + FILES_FOLDER.toAbsolutePath().toString() + "/");
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.practica.demo.data.tournament;

import java.io.File;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class TournamentRestController {
Expand All @@ -28,4 +35,44 @@ public class TournamentRestController {
}
}

@RequestMapping(value = "/api/tournaments/{id}/image", method = RequestMethod.POST)
public ResponseEntity<Object>uploadImage(@RequestBody MultipartFile image,@PathVariable int id){
if(tournamentService.uploadImage(image,id)){
return new ResponseEntity<>(HttpStatus.CREATED);
}else{
return new ResponseEntity<>("Image couldn't be uploaded", HttpStatus.CONFLICT);
}
}

@RequestMapping(value = "/api/tournaments/{id}/image", method = RequestMethod.GET)
public ResponseEntity<Object>getImage(@PathVariable int id){
byte[] image = tournamentService.getImage(id);
if (image!=null) {
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
}else {
return new ResponseEntity<>("Image not found", HttpStatus.NOT_FOUND);
}
/*
HttpHeaders headers = new HttpHeaders();
InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
byte[] media = IOUtils.toByteArray(in);
headers.setCacheControl(CacheControl.noCache().getHeaderValue());
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(media, headers, HttpStatus.OK);
return responseEntity;
File img = (File) tournamentService.getImage(id);
InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
IOUtils.copy(in, response.getOutputStream());
if(img!=null){
return new ResponseEntity<>(img,HttpStatus.OK);
}else{
return new ResponseEntity<>("Image couldn't be uploaded", HttpStatus.NOT_FOUND);
}*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

public interface TournamentService {
public abstract List<Tournament> getTournaments();
public abstract boolean createTournament(Tournament tournament);
public abstract boolean uploadImage(MultipartFile imageFile,int id);
public abstract byte[] getImage(int id);
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,111 @@
package com.practica.demo.data.tournament;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.practica.demo.Imgs.ImageService;
import com.practica.demo.data.game.Game;
import com.practica.demo.data.game.GameRepository;

@Service
public class TournamentServiceImp implements TournamentService{
public class TournamentServiceImp implements TournamentService {

@Autowired
private TournamentRepository tournamentRepository;

@Autowired
private GameRepository gameRepository;

@Override
public List<Tournament> getTournaments() {
return tournamentRepository.findAll();
}

@Autowired
private ImageService imgService;

@Override
public boolean createTournament(Tournament tournament) {
try {
tournamentRepository.save(tournament);

Game newGame = new Game();

newGame.setTournament(tournament);

gameRepository.save(newGame);

return true;
}catch(Exception e) {
} catch (Exception e) {
return false;
}
}

@Override
public boolean uploadImage(MultipartFile imageFile, int id) {
try {
Optional<Tournament> updated = tournamentRepository.findById(id);
if (updated.isPresent()) {
Path imgPath = imgService.saveImagePath("tournament", id, imageFile);
Tournament updatedTournament = updated.get();
updatedTournament.setImg(imgPath.toString());
tournamentRepository.save(updatedTournament);
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

@Override
public byte[] getImage(int id) {
Optional<Tournament> aux = tournamentRepository.findById(id);
if (aux.isPresent()) {
Tournament tournament = aux.get();
if (tournament.getImg() != null) {
Path path = Paths.get(tournament.getImg());
// path.resolve(tournament.getImg());
//File file = path.toFile();
try {
return IOUtils.toByteArray( Files.newInputStream(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

/*MockMultipartFile mockFile = new MockMultipartFile("image", new FileInputStream(file));
byte[] content = null;
try {
content = Files.readAllBytes(path);
} catch (final IOException e) {
}
String originalFileName = "image-"+id;
String contentType ="jpg";
MultipartFile img = new MockMultipartFile("image.jpg",
originalFileName , contentType, content);*/

} else {
return null;
}
} else {
return null;
}
}

}

0 comments on commit 088ea1f

Please sign in to comment.