/
UserController.java
58 lines (45 loc) · 2.06 KB
/
UserController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.careydevelopment.ecosystem.user.controller;
import java.nio.file.Files;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.careydevelopment.ecosystem.user.util.FileUtil;
import com.careydevelopment.ecosystem.user.util.SecurityUtil;
@CrossOrigin(origins = "*")
@RestController
public class UserController {
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
@Autowired
private SecurityUtil securityUtil;
@Autowired
private FileUtil fileUtil;
@GetMapping("/user/{userId}/profileImage")
public ResponseEntity<?> getProfileImage(@PathVariable String userId) {
try {
Path imagePath = fileUtil.fetchProfilePhotoByUserId(userId);
if (imagePath != null) {
LOG.debug("Getting image from " + imagePath.toString());
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(imagePath));
return ResponseEntity
.ok()
.contentLength(imagePath.toFile().length())
.contentType(MediaType.IMAGE_JPEG)
.body(resource);
} else {
LOG.debug("Profile photo not found for user " + userId);
return ResponseEntity.status(HttpStatus.OK).build();
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
}