Guest User

Untitled

a guest
May 8th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. package com.bss.sistema.genesis.storage.local;
  2.  
  3. import static java.nio.file.FileSystems.getDefault;
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardCopyOption;
  11. import java.util.UUID;
  12.  
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.web.multipart.MultipartFile;
  16.  
  17. import com.bss.sistema.genesis.storage.FotoStorage;
  18.  
  19. import net.coobird.thumbnailator.Thumbnails;
  20. import net.coobird.thumbnailator.name.Rename;
  21.  
  22. public class FotoStorageLocal implements FotoStorage {
  23.  
  24. private static final Logger logger = LoggerFactory.getLogger(FotoStorageLocal.class);
  25.  
  26. //private Path local;
  27. //private Path localTemporario;
  28.  
  29. Path local = Paths.get("C:\\Users\\BRUNO\\.genesisfotos\\temp");
  30. Path localTemporario = Paths.get("C:\\Users\\BRUNO\\.genesisfotos");
  31.  
  32.  
  33.  
  34.  
  35. public FotoStorageLocal() {
  36. //Versao MAC
  37. //this(getDefault().getPath(System.getenv("user.home"), ".genesisfotos"));
  38.  
  39. // versao windows
  40. this(getDefault().getPath(System.getProperty("user.home"), ".genesisfotos"));
  41. System.out.println(">>>>>>>> CRIADO GENESIS + FOTOS ");
  42. }
  43.  
  44. public FotoStorageLocal(Path path) {
  45. this.local = path;
  46. criarPastas();
  47. }
  48.  
  49.  
  50. @Override
  51. public void salvar(String foto) {
  52. try {
  53. Files.move(this.localTemporario.resolve(foto), this.local.resolve(foto),StandardCopyOption.REPLACE_EXISTING);
  54. //Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
  55. } catch (IOException e) {
  56. throw new RuntimeException("Erro movendo a foto para destino final", e);
  57. }
  58. try {
  59. Thumbnails.of(this.local.resolve(foto).toString()).size(40, 68).toFiles(Rename.PREFIX_DOT_THUMBNAIL);
  60. } catch (IOException e) {
  61. throw new RuntimeException("Erro gerando thumbnail", e);
  62. }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. @Override
  69. public String salvarTemporariamente(MultipartFile[] files) {
  70. String novoNome = null;
  71. if (files != null && files.length > 0) {
  72. MultipartFile arquivo = files[0];
  73. novoNome = renomearArquivo(arquivo.getOriginalFilename());
  74. try {
  75. arquivo.transferTo(new File(this.localTemporario.toAbsolutePath().toString() + getDefault().getSeparator() + novoNome));
  76. } catch (IOException e) {
  77. throw new RuntimeException("Erro salvando a foto na pasta temporária", e);
  78. }
  79. }
  80.  
  81. return novoNome;
  82. }
  83.  
  84.  
  85. @Override
  86. public byte[] recuperarFotoTemporaria(String nome) {
  87. try {
  88. return Files.readAllBytes(this.localTemporario.resolve(nome));
  89. } catch (IOException e) {
  90. throw new RuntimeException("Erro lendo a foto temporária", e);
  91. }
  92. }
  93.  
  94.  
  95.  
  96. @Override
  97. public byte[] recuperar(String nome) {
  98. try {
  99. return Files.readAllBytes(this.local.resolve(nome));
  100. } catch (IOException e) {
  101. throw new RuntimeException("Erro lendo a foto nao e a temporaria mais", e);
  102. }
  103. }
  104.  
  105. private void criarPastas() {
  106. try {
  107. Files.createDirectories(this.local);
  108. this.localTemporario = getDefault().getPath(this.local.toString(), "temp");
  109. Files.createDirectories(this.localTemporario);
  110.  
  111. if (logger.isDebugEnabled()) {
  112. logger.debug("Pastas criadas para salvar fotos.");
  113. logger.debug("Pasta default: " + this.local.toAbsolutePath());
  114. logger.debug("Pasta temporária: " + this.localTemporario.toAbsolutePath());
  115. }
  116. } catch (IOException e) {
  117. throw new RuntimeException("Erro criando pasta para salvar foto", e);
  118. }
  119. }
  120.  
  121. private String renomearArquivo(String nomeOriginal) {
  122. String novoNome = UUID.randomUUID().toString() + "_" + nomeOriginal;
  123.  
  124. if (logger.isDebugEnabled()) {
  125. logger.debug(String.format("Nome original: %s, novo nome: %s", nomeOriginal, novoNome));
  126. }
  127.  
  128. return novoNome;
  129.  
  130. }
  131.  
  132. }
Add Comment
Please, Sign In to add comment