Skip to content

Commit 28737ae

Browse files
committed
Enviando imagem via endpoint
1 parent 0d705e0 commit 28737ae

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
package jdc.loja;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.boot.CommandLineRunner;
54
import org.springframework.boot.SpringApplication;
65
import org.springframework.boot.autoconfigure.SpringBootApplication;
76

8-
import jdc.loja.services.S3Service;
9-
107
@SpringBootApplication
118
public class LojaApplication implements CommandLineRunner {
12-
13-
@Autowired
14-
private S3Service service;
159

1610
public static void main(String[] args) {
1711
SpringApplication.run(LojaApplication.class, args);
1812
}
1913

2014
@Override
2115
public void run(String... args) throws Exception {
22-
service.uploadFile("C:\\dbz.jpg");
2316
}
2417
}

src/main/java/jdc/loja/config/SecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
4747
};
4848

4949
private static final String[] PUBLIC_MATCHERS_POST = {
50-
"/clientes/**",
50+
"/clientes",
51+
"/clientes/picture",
5152
"/auth/forgot/**"
5253
};
5354

src/main/java/jdc/loja/resources/ClienteResource.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.web.bind.annotation.RequestMethod;
1818
import org.springframework.web.bind.annotation.RequestParam;
1919
import org.springframework.web.bind.annotation.RestController;
20+
import org.springframework.web.multipart.MultipartFile;
2021
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
2122

2223
import jdc.loja.domain.Cliente;
@@ -83,4 +84,10 @@ public ResponseEntity<Page<ClienteDTO>> findPage(
8384
Page<ClienteDTO> listDto = list.map(obj -> new ClienteDTO(obj));
8485
return ResponseEntity.ok().body(listDto);
8586
}
87+
88+
@RequestMapping(value="/picture", method=RequestMethod.POST)
89+
public ResponseEntity<Void> uploadProfilePicture(@RequestParam(name="file") MultipartFile file) {
90+
URI uri = service.uploadProfilePicture(file);
91+
return ResponseEntity.created(uri).build();
92+
}
8693
}

src/main/java/jdc/loja/services/ClienteService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jdc.loja.services;
22

3+
import java.net.URI;
34
import java.util.Arrays;
45
import java.util.List;
56
import java.util.Optional;
@@ -11,6 +12,7 @@
1112
import org.springframework.data.domain.Sort.Direction;
1213
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1314
import org.springframework.stereotype.Service;
15+
import org.springframework.web.multipart.MultipartFile;
1416

1517
import jdc.loja.domain.Cidade;
1618
import jdc.loja.domain.Cliente;
@@ -38,6 +40,9 @@ public class ClienteService {
3840
@Autowired
3941
private BCryptPasswordEncoder pe;
4042

43+
@Autowired
44+
private S3Service s3;
45+
4146
public Cliente find(Integer id) {
4247

4348
UserSS user = UserService.authenticated();
@@ -107,4 +112,8 @@ private void updateData(Cliente newObj, Cliente obj) {
107112
newObj.setNome(obj.getNome());
108113
newObj.setEmail(obj.getEmail());
109114
}
115+
116+
public URI uploadProfilePicture(MultipartFile multipartFile) {
117+
return s3.uploadFile(multipartFile);
118+
}
110119
}
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
11
package jdc.loja.services;
22

3-
import java.io.File;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
47

58
import org.slf4j.Logger;
69
import org.slf4j.LoggerFactory;
710
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.beans.factory.annotation.Value;
912
import org.springframework.stereotype.Service;
13+
import org.springframework.web.multipart.MultipartFile;
1014

11-
import com.amazonaws.AmazonClientException;
12-
import com.amazonaws.AmazonServiceException;
1315
import com.amazonaws.services.s3.AmazonS3;
14-
import com.amazonaws.services.s3.model.PutObjectRequest;
16+
import com.amazonaws.services.s3.model.ObjectMetadata;
1517

1618
@Service
1719
public class S3Service {
18-
20+
1921
private Logger LOG = LoggerFactory.getLogger(S3Service.class);
2022

2123
@Autowired
2224
private AmazonS3 s3client;
23-
25+
2426
@Value("${s3.bucket}")
2527
private String bucketName;
26-
27-
public void uploadFile(String localFilePath) {
28+
29+
public URI uploadFile(MultipartFile multipartFile) {
30+
try {
31+
String filename = multipartFile.getOriginalFilename();
32+
InputStream is = multipartFile.getInputStream();
33+
String contentType = multipartFile.getContentType();
34+
return uploadFile(is, filename, contentType);
35+
} catch (IOException e) {
36+
throw new RuntimeException("Erro de IO: " + e.getMessage());
37+
}
38+
}
39+
40+
public URI uploadFile(InputStream is, String filename, String contentType) {
2841
try {
29-
File file = new File(localFilePath);
42+
ObjectMetadata meta = new ObjectMetadata();
43+
meta.setContentType(contentType);
3044
LOG.info("Iniciando upload");
31-
s3client.putObject(new PutObjectRequest(bucketName, "teste.jpg", file));
45+
s3client.putObject(bucketName, filename, is, meta);
3246
LOG.info("Upload finalizado");
33-
} catch (AmazonServiceException e) {
34-
LOG.info("AmazonServiceException: " + e.getMessage());
35-
LOG.info("Status code: " + e.getErrorCode());
36-
} catch (AmazonClientException e) {
37-
LOG.info("AmazonClientException: " + e.getMessage());
47+
return s3client.getUrl(bucketName, filename).toURI();
48+
} catch (URISyntaxException e) {
49+
throw new RuntimeException("Erro ao converter URL para URI");
3850
}
3951
}
4052
}

0 commit comments

Comments
 (0)