-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Avadem
committed
Mar 7, 2020
1 parent
1652a23
commit 088ea1f
Showing
9 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
39 changes: 39 additions & 0 deletions
39
practica/src/main/java/com/practica/demo/Imgs/TournamentImageService.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,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/"); | ||
} | ||
} |
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
87 changes: 78 additions & 9 deletions
87
practica/src/main/java/com/practica/demo/data/tournament/TournamentServiceImp.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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
} |