diff options
author | 2017-10-15 05:37:18 +0300 | |
---|---|---|
committer | 2017-10-15 19:50:15 +0300 | |
commit | 9a4add44ecbd32dea6ac3d30fd81ae1ac82e3dbc (patch) | |
tree | 38b7a4ba01d1fc47841d3ba2787209b2b2a2de9e /juick-server-jdbc/src | |
parent | aeda9e3733e685f5069b5cfbb16272cd32073fa0 (diff) |
api: ImagesService
Diffstat (limited to 'juick-server-jdbc/src')
4 files changed, 99 insertions, 13 deletions
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index a2a6c164..f71837a7 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -26,6 +26,9 @@ import com.juick.service.search.SearchService; import com.juick.util.MessageUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; @@ -51,18 +54,21 @@ import java.util.List; */ @Repository public class MessagesServiceImpl extends BaseJdbcService implements MessagesService { - private final UserService userService; - private final SearchService searchService; + private static final Logger logger = LoggerFactory.getLogger(MessagesServiceImpl.class); + @Inject + private UserService userService; + @Inject + private SearchService searchService; + @Inject + private ImagesService imagesService; + @Value("${img_path:/var/www/juick.com/i/}") + private String imgDir; + @Value("${img_url:https://i.juick.com/}") + private String baseImagesUrl; @Inject - public MessagesServiceImpl(JdbcTemplate jdbcTemplate, UserService userService, SearchService searchService) { + public MessagesServiceImpl(JdbcTemplate jdbcTemplate) { super(jdbcTemplate, null); - - Assert.notNull(userService, "UserService must be initialized"); - this.userService = userService; - - Assert.notNull(searchService, "SearchService must be initialized"); - this.searchService = searchService; } private class MessageMapper implements RowMapper<Message> { @@ -96,6 +102,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msg.setRepliesBy(rs.getString(19)); msg.setText(rs.getString(20)); msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(21))); + try { + imagesService.setAttachmentMetadata(imgDir, baseImagesUrl, msg); + } catch (Exception e) { + logger.warn("images exception", e); + } return msg; } } diff --git a/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java b/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java index 63fafc37..dbd13098 100644 --- a/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java +++ b/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java @@ -19,6 +19,8 @@ package com.juick.configuration; import ch.vorburger.exec.ManagedProcessException; import ch.vorburger.mariadb4j.DB; +import com.juick.service.ImagesService; +import com.juick.service.MockImagesService; import com.juick.service.search.SearchService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -79,4 +81,8 @@ public class RepositoryConfiguration { } }; } + @Bean + public ImagesService imagesService() { + return new MockImagesService(); + } } diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java index 47699dbc..f5701b0c 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -22,11 +22,12 @@ import com.juick.Tag; import com.juick.User; import com.juick.configuration.RepositoryConfiguration; import com.juick.server.helpers.AnonymousUser; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; +import org.junit.runner.RunWith; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; import java.util.Collections; @@ -38,6 +39,7 @@ import static org.hamcrest.Matchers.equalTo; /** * Created by aalexeev on 11/25/16. */ +@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = RepositoryConfiguration.class) public class MessageServiceTest extends AbstractJUnit4SpringContextTests { @Inject @@ -59,10 +61,15 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { int ugnichId, freefdId, mid; + private static boolean setUpIsDone = false; + @Before public void createUgniches() { - ugnichId = userService.createUser("ugnich", "secret"); - freefdId = userService.createUser("freefd", "secret"); + if (!setUpIsDone) { + ugnichId = userService.createUser("ugnich", "secret"); + freefdId = userService.createUser("freefd", "secret"); + setUpIsDone = true; + } } @Test diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java b/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java new file mode 100644 index 00000000..6f47de91 --- /dev/null +++ b/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java @@ -0,0 +1,62 @@ +package com.juick.service; + +import com.juick.Attachment; +import com.juick.Message; +import com.juick.Photo; +import org.springframework.util.StringUtils; + +import java.io.File; +import java.nio.file.Paths; + +public class MockImagesService implements ImagesService { + @Override + public void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception { + if (!StringUtils.isEmpty(msg.getAttachmentType())) { + Photo photo = new Photo(); + if (msg.getRid()> 0) { + photo.setSmall(String.format("%sphotos-512/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType())); + photo.setMedium(String.format("%sphotos-1024/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType())); + photo.setThumbnail(String.format("%sps/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType())); + } else { + photo.setSmall(String.format("%sphotos-512/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType())); + photo.setMedium(String.format("%sphotos-1024/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType())); + photo.setThumbnail(String.format("%sps/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType())); + } + msg.setPhoto(photo); + StringBuilder builder = new StringBuilder(); + builder.append(baseUrl); + builder.append(msg.getAttachmentType().equals("mp4") ? "video" : "p"); + builder.append("/").append(msg.getMid()); + if (msg.getRid() > 0) { + builder.append("-").append(msg.getRid()); + } + builder.append(".").append(msg.getAttachmentType()); + String originalUrl = builder.toString(); + + Attachment original = new Attachment(); + original.setUrl(originalUrl); + original.setHeight(2048); + original.setWidth(2048); + + Attachment medium = new Attachment(); + medium.setUrl(photo.getMedium()); + medium.setWidth(1024); + medium.setHeight(1024); + original.setMedium(medium); + + Attachment small = new Attachment(); + small.setUrl(photo.getSmall()); + small.setWidth(1024); + small.setHeight(1024); + original.setSmall(small); + + Attachment thumb = new Attachment(); + thumb.setUrl(photo.getMedium()); + thumb.setWidth(1024); + thumb.setHeight(1024); + original.setThumbnail(thumb); + + msg.setAttachment(original); + } + } +} |