1 package net.sumaris.server.service.administration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import net.sumaris.core.dao.data.ImageAttachmentDao;
26 import net.sumaris.core.util.crypto.MD5Util;
27 import net.sumaris.core.vo.administration.user.DepartmentVO;
28 import net.sumaris.core.vo.administration.user.PersonVO;
29 import net.sumaris.core.vo.data.ImageAttachmentVO;
30 import net.sumaris.server.config.SumarisServerConfiguration;
31 import net.sumaris.server.http.rest.RestPaths;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 @Service("imageService")
38 public class ImageServiceImpl implements ImageService {
39
40
41 private static final Logger log = LoggerFactory.getLogger(ImageServiceImpl.class);
42
43 private final static String GRAVATAR_URL = "https://www.gravatar.com/avatar/%s";
44
45 private String personAvatarUrl;
46 private String departmentLogoUrl;
47 private SumarisServerConfiguration config;
48
49 @Autowired
50 private ImageAttachmentDao dao;
51
52
53 @Autowired
54 public ImageServiceImpl(SumarisServerConfiguration config) {
55 this.config = config;
56
57
58 personAvatarUrl = config.getServerUrl() + RestPaths.PERSON_AVATAR_PATH;
59 departmentLogoUrl = config.getServerUrl() + RestPaths.DEPARTMENT_LOGO_PATH;
60 }
61
62 @Override
63 public ImageAttachmentVO get(int id) {
64 return dao.get(id);
65 }
66
67 public void fillAvatar(PersonVO person) {
68 if (person == null) return;
69 if (person.getHasAvatar() != null && person.getHasAvatar() && org.apache.commons.lang3.StringUtils.isNotBlank(person.getPubkey())) {
70 person.setAvatar(personAvatarUrl.replace("{pubkey}", person.getPubkey()));
71 }
72
73 else if (org.apache.commons.lang3.StringUtils.isNotBlank(person.getEmail())){
74 person.setAvatar(String.format(GRAVATAR_URL, MD5Util.md5Hex(person.getEmail())));
75 }
76 }
77
78 public void fillLogo(DepartmentVO department) {
79 if (department == null) return;
80 if (department.getHasLogo() != null && department.getHasLogo() && org.apache.commons.lang3.StringUtils.isNotBlank(department.getLabel())) {
81 department.setLogo(departmentLogoUrl.replace("{label}", department.getLabel()));
82 }
83 }
84 }