View Javadoc
1   package net.sumaris.server.service.administration;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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      /* Logger */
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          // Prepare URL for String formatter
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          // Use gravatar URL
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  }