From 9a4add44ecbd32dea6ac3d30fd81ae1ac82e3dbc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 15 Oct 2017 05:37:18 +0300 Subject: api: ImagesService --- .../api/configuration/ApiAppConfiguration.java | 3 + .../juick/api/configuration/ApiInitializer.java | 5 +- .../juick/api/configuration/ApiSecurityConfig.java | 2 + .../java/com/juick/api/tests/MessagesTests.java | 26 ++++++-- juick-core/src/main/java/com/juick/Attachment.java | 58 ++++++++++++++++++ juick-core/src/main/java/com/juick/Message.java | 50 +++------------ juick-core/src/main/java/com/juick/Photo.java | 2 + .../com/juick/formatters/PlainTextFormatter.java | 2 +- .../src/test/java/com/juick/MessageTest.java | 25 -------- .../configuration/CrosspostInitializer.java | 3 +- .../src/main/java/com/juick/rss/MessagesView.java | 2 +- .../juick/rss/configuration/RssInitializer.java | 3 +- .../test/java/com/juick/rss/tests/RSSTests.java | 7 +-- .../main/java/com/juick/service/ImagesService.java | 7 +++ .../com/juick/service/MessagesServiceImpl.java | 29 ++++++--- .../configuration/RepositoryConfiguration.java | 6 ++ .../java/com/juick/service/MessageServiceTest.java | 15 +++-- .../java/com/juick/service/MockImagesService.java | 62 +++++++++++++++++++ juick-server-web/build.gradle | 2 +- .../server/configuration/BaseWebConfiguration.java | 9 +++ .../java/com/juick/server/util/ImageUtils.java | 10 +++ .../java/com/juick/service/ImagesServiceImpl.java | 71 ++++++++++++++++++++++ .../ws/configuration/WebsocketInitializer.java | 3 +- juick-www/build.gradle | 1 - .../juick/www/configuration/WwwInitializer.java | 2 - .../java/com/juick/www/controllers/UserThread.java | 2 +- .../components/configuration/BotInitializer.java | 3 +- .../java/com/juick/components/XMPPConnection.java | 6 +- .../components/configuration/XmppInitializer.java | 3 +- src/test/java/com/juick/tests/ApiTests.java | 9 +-- 30 files changed, 312 insertions(+), 116 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/Attachment.java create mode 100644 juick-server-core/src/main/java/com/juick/service/ImagesService.java create mode 100644 juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java create mode 100644 juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java index f77611e2..c78ea51a 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -20,6 +20,8 @@ package com.juick.api.configuration; import com.juick.api.ApiServer; import com.juick.api.TelegramBotManager; import com.juick.server.configuration.BaseWebConfiguration; +import com.juick.service.ImagesService; +import com.juick.service.ImagesServiceImpl; import org.springframework.context.annotation.*; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @@ -69,6 +71,7 @@ public class ApiAppConfiguration extends BaseWebConfiguration { public ApiServer apiServer() { return new ApiServer(); } + @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java index fa025e56..8f9cb4ed 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -17,7 +17,6 @@ package com.juick.api.configuration; -import com.juick.configuration.DataConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -32,13 +31,13 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { return new Class[]{ - ApiSecurityConfig.class, DataConfiguration.class, MessengerConfiguration.class + ApiSecurityConfig.class }; } @Override protected Class[] getServletConfigClasses() { - return new Class[]{ ApiAppConfiguration.class }; + return new Class[]{ MessengerConfiguration.class }; } @Override diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java index e4ae4b80..7e3f3345 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java @@ -24,6 +24,7 @@ import com.juick.service.security.deprecated.RequestParamHashRememberMeServices; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; @@ -48,6 +49,7 @@ import java.util.concurrent.TimeUnit; @Configuration @EnableWebSecurity @PropertySource("classpath:juick.conf") +@Import(ApiAppConfiguration.class) public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { @Value("${auth_remember_me_key}") private String rememberMeKey; diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java index fc71c755..518e3762 100644 --- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java +++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java @@ -28,11 +28,10 @@ import com.juick.api.configuration.ApiSecurityConfig; import com.juick.api.configuration.MessengerConfiguration; import com.juick.configuration.MockDataConfiguration; import com.juick.server.helpers.TagStats; -import com.juick.service.MessagesService; -import com.juick.service.TagService; -import com.juick.service.UserService; +import com.juick.service.*; import com.juick.test.util.MockUtils; import com.juick.util.DateFormattersHolder; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -91,6 +90,10 @@ public class MessagesTests { ApiServer apiServer() { return Mockito.mock(ApiServer.class); } + @Bean + ImagesService imagesService() { + return new MockImagesService(); + } } private MockMvc mockMvc; @@ -105,6 +108,8 @@ public class MessagesTests { private TagService tagService; @Inject private ObjectMapper jsonMapper; + @Inject + private ImagesService imagesService; private User ugnich, freefd; String ugnichName, ugnichPassword, freefdName, freefdPassword; @@ -138,6 +143,13 @@ public class MessagesTests { .thenReturn(new User()); } + @After + public void resetMocks() { + Mockito.reset(userService); + Mockito.reset(messagesService); + Mockito.reset(tagService); + } + @Test public void testAllUnAuthorized() throws Exception { @@ -164,6 +176,8 @@ public class MessagesTests { Message msg = MockUtils.mockMessage(1, ugnich, msgText); Instant now = Instant.now(); msg.setTimestamp(now); + msg.setAttachmentType("png"); + imagesService.setAttachmentMetadata("", "http://localhost/", msg); when(messagesService.getMyFeed(1, 0, true)) .thenReturn(Collections.singletonList(1)); when(messagesService.getMessages(Collections.singletonList(1))) @@ -177,7 +191,9 @@ public class MessagesTests { .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[0].mid", is(1))) .andExpect(jsonPath("$[0].timestamp", is(DateFormattersHolder.getMessageFormatterInstance().format(now)))) - .andExpect(jsonPath("$[0].body", is(msgText))); + .andExpect(jsonPath("$[0].body", is(msgText))) + .andExpect(jsonPath("$[0].attachment.url", is("http://localhost/p/1.png"))) + .andExpect(jsonPath("$[0].attachment.small.url", is("http://localhost/photos-512/1.png"))); } @Test @@ -235,7 +251,7 @@ public class MessagesTests { get("/home") .with(httpBasic(ugnichName, ugnichPassword)) .header("Origin", "http://api.example.net")) - .andExpect(status().isOk()) + .andExpect(status().isNotFound()) .andExpect(header().string("Access-Control-Allow-Origin", "*")); } diff --git a/juick-core/src/main/java/com/juick/Attachment.java b/juick-core/src/main/java/com/juick/Attachment.java new file mode 100644 index 00000000..76f2995a --- /dev/null +++ b/juick-core/src/main/java/com/juick/Attachment.java @@ -0,0 +1,58 @@ +package com.juick; + +public class Attachment { + private String url; + private Integer height; + private Integer width; + private Attachment small; + private Attachment medium; + private Attachment thumbnail; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public Integer getHeight() { + return height; + } + + public void setHeight(Integer height) { + this.height = height; + } + + public Integer getWidth() { + return width; + } + + public void setWidth(Integer width) { + this.width = width; + } + + public Attachment getSmall() { + return small; + } + + public void setSmall(Attachment small) { + this.small = small; + } + + public Attachment getMedium() { + return medium; + } + + public void setMedium(Attachment medium) { + this.medium = medium; + } + + public Attachment getThumbnail() { + return thumbnail; + } + + public void setThumbnail(Attachment thumbnail) { + this.thumbnail = thumbnail; + } +} diff --git a/juick-core/src/main/java/com/juick/Message.java b/juick-core/src/main/java/com/juick/Message.java index 25463c46..8f903e1f 100644 --- a/juick-core/src/main/java/com/juick/Message.java +++ b/juick-core/src/main/java/com/juick/Message.java @@ -27,16 +27,13 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import java.time.Instant; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; /** * @author Ugnich Anton */ @XmlRootElement(name = "juick", namespace = "http://juick.com/message") -@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) +@XmlAccessorType() public class Message implements Comparable { private int mid = 0; private int rid = 0; @@ -67,6 +64,8 @@ public class Message implements Comparable { private String attachmentType; @XmlTransient private Photo photo; + @XmlTransient + private Attachment attachment; public String Video = null; public Place Place = null; private int likes; @@ -158,25 +157,6 @@ public class Message implements Comparable { } } - @JsonIgnore - public String getAttachmentURL() { - if (attachmentType != null) { - StringBuilder builder = new StringBuilder(); - - builder.append("http://i.juick.com/"); - builder.append(attachmentType.equals("mp4") ? "video" : "photos-1024"); - builder.append("/").append(getMid()); - - if (getRid() > 0) - builder.append("-").append(getRid()); - - builder.append(".").append(attachmentType); - - return builder.toString(); - } - return null; - } - @JsonIgnore public String getTagsString() { StringBuilder builder = new StringBuilder(); @@ -352,22 +332,10 @@ public class Message implements Comparable { this.repliesBy = repliesBy; } - @JsonProperty("photo") - @XmlTransient - public Photo getPhotoURLs() { - if (StringUtils.isNotBlank(attachmentType)) { - Photo photo = new Photo(); - if (rid > 0) { - photo.setSmall(String.format("https://i.juick.com/photos-512/%d-%d.%s", mid, rid, attachmentType)); - photo.setMedium(String.format("https://i.juick.com/photos-1024/%d-%d.%s", mid, rid, attachmentType)); - photo.setThumbnail(String.format("https://i.juick.com/ps/%d-%d.%s", mid, rid, attachmentType)); - } else { - photo.setSmall(String.format("https://i.juick.com/photos-512/%d.%s", mid, attachmentType)); - photo.setMedium(String.format("https://i.juick.com/photos-1024/%d.%s", mid, attachmentType)); - photo.setThumbnail(String.format("https://i.juick.com/ps/%d.%s", mid, attachmentType)); - } - return photo; - } - return null; + public Attachment getAttachment() { + return attachment; + } + public void setAttachment(Attachment attachment) { + this.attachment = attachment; } } diff --git a/juick-core/src/main/java/com/juick/Photo.java b/juick-core/src/main/java/com/juick/Photo.java index af20fc88..06299610 100644 --- a/juick-core/src/main/java/com/juick/Photo.java +++ b/juick-core/src/main/java/com/juick/Photo.java @@ -20,6 +20,8 @@ package com.juick; /** * Created by vitalyster on 30.11.2016. */ +// used for compatibility +@Deprecated public class Photo { private String small; private String medium; diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java index c62e0134..06ae8a0a 100644 --- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -39,7 +39,7 @@ public class PlainTextFormatter { sb.append(title).append(jmsg.getUser().getName()).append(":\n") .append(subtitle).append("\n").append(jmsg.getText()).append("\n"); if (jmsg.getPhoto() != null) { - sb.append(jmsg.getAttachmentURL()); + sb.append(jmsg.getAttachment().getMedium().getUrl()); } return sb.toString(); } diff --git a/juick-core/src/test/java/com/juick/MessageTest.java b/juick-core/src/test/java/com/juick/MessageTest.java index b420edfa..3bdac9be 100644 --- a/juick-core/src/test/java/com/juick/MessageTest.java +++ b/juick-core/src/test/java/com/juick/MessageTest.java @@ -127,31 +127,6 @@ public class MessageTest { assertThat(message2.compareTo(message1), equalTo(1)); } - @Test - public void attachmentURLShouldBeReturnCorrectResult() throws Exception { - Message message1 = new Message(); - - assertThat(message1.getAttachmentURL(), nullValue()); - - message1.setAttachmentType("jpeg"); - - assertThat(message1.getAttachmentURL(), equalTo("http://i.juick.com/photos-1024/0.jpeg")); - - message1.setRid(1); - - assertThat(message1.getAttachmentURL(), equalTo("http://i.juick.com/photos-1024/0-1.jpeg")); - - Message message2 = new Message(); - - message2.setAttachmentType("mp4"); - - assertThat(message2.getAttachmentURL(), equalTo("http://i.juick.com/video/0.mp4")); - - message2.setRid(1); - - assertThat(message2.getAttachmentURL(), equalTo("http://i.juick.com/video/0-1.mp4")); - } - @Test public void tagsStringShouldBeEmptyIfNoTags() throws Exception { Message message = new Message(); diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java index 09cebb26..2e33abf0 100644 --- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java +++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java @@ -17,7 +17,6 @@ package com.juick.components.configuration; -import com.juick.configuration.DataConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -31,7 +30,7 @@ public class CrosspostInitializer extends AbstractAnnotationConfigDispatcherServ @Override protected Class[] getRootConfigClasses() { - return new Class[]{ DataConfiguration.class }; + return new Class[]{ }; } @Override diff --git a/juick-rss/src/main/java/com/juick/rss/MessagesView.java b/juick-rss/src/main/java/com/juick/rss/MessagesView.java index 146641c5..ae06412b 100644 --- a/juick-rss/src/main/java/com/juick/rss/MessagesView.java +++ b/juick-rss/src/main/java/com/juick/rss/MessagesView.java @@ -125,7 +125,7 @@ public class MessagesView extends AbstractRssFeedView { String type = msg.getAttachmentType().equals("jpg") ? "image/jpeg" : "image/png"; MediaEntryModuleImpl module = new MediaEntryModuleImpl(); try { - UrlReference reference = new UrlReference(msg.getAttachmentURL()); + UrlReference reference = new UrlReference(msg.getAttachment().getUrl()); MediaContent mediaContent = new MediaContent(reference); mediaContent.setType(type); Metadata metadata = new Metadata(); diff --git a/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java b/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java index d6fe6e20..617da288 100644 --- a/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java +++ b/juick-rss/src/main/java/com/juick/rss/configuration/RssInitializer.java @@ -17,7 +17,6 @@ package com.juick.rss.configuration; -import com.juick.configuration.DataConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -31,7 +30,7 @@ public class RssInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { - return new Class[]{RssAppConfiguration.class, DataConfiguration.class}; + return new Class[]{RssAppConfiguration.class}; } @Override diff --git a/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java b/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java index 1edfe0d2..7c356771 100644 --- a/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java +++ b/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java @@ -20,7 +20,6 @@ package com.juick.rss.tests; import com.juick.Message; import com.juick.Tag; import com.juick.User; -import com.juick.configuration.DataConfiguration; import com.juick.rss.configuration.RssAppConfiguration; import com.juick.rss.configuration.RssMvcConfiguration; import com.juick.service.MessagesService; @@ -50,9 +49,7 @@ import java.util.List; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** * Created by vitalyster on 13.12.2016. @@ -62,7 +59,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @WebAppConfiguration public class RSSTests { @Configuration - @Import(value = {RssMvcConfiguration.class, RssAppConfiguration.class, DataConfiguration.class}) + @Import(value = {RssMvcConfiguration.class, RssAppConfiguration.class}) static class Config { @Bean @Primary diff --git a/juick-server-core/src/main/java/com/juick/service/ImagesService.java b/juick-server-core/src/main/java/com/juick/service/ImagesService.java new file mode 100644 index 00000000..b5cff16e --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/ImagesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +import com.juick.Message; + +public interface ImagesService { + void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception; +} 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 { @@ -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); + } + } +} diff --git a/juick-server-web/build.gradle b/juick-server-web/build.gradle index 7df26a9a..7c43d653 100644 --- a/juick-server-web/build.gradle +++ b/juick-server-web/build.gradle @@ -5,7 +5,7 @@ sourceCompatibility = 1.8 dependencies { compile project(':juick-server-core') - + compile project(':juick-server-jdbc') compile "com.fasterxml.jackson.core:jackson-core:${rootProject.jacksonVersion}" compile "com.fasterxml.jackson.core:jackson-databind:${rootProject.jacksonVersion}" compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java index a2422df9..efbe66cb 100644 --- a/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java +++ b/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java @@ -21,8 +21,12 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.juick.configuration.DataConfiguration; +import com.juick.service.ImagesService; +import com.juick.service.ImagesServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.multipart.MultipartResolver; @@ -35,6 +39,7 @@ import java.util.List; /** * Created by vitalyster on 28.06.2016. */ +@Import(DataConfiguration.class) @Configuration public class BaseWebConfiguration implements WebMvcConfigurer { @@ -67,4 +72,8 @@ public class BaseWebConfiguration implements WebMvcConfigurer { resolver.setMaxUploadSize(10000000); return resolver; } + @Bean + public ImagesService imagesService() { + return new ImagesServiceImpl(); + } } diff --git a/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java b/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java index 25345439..7f21c0a5 100644 --- a/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java +++ b/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java @@ -18,6 +18,7 @@ package com.juick.server.util; +import org.apache.commons.imaging.ImageInfo; import org.apache.commons.io.FilenameUtils; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.Imaging; @@ -149,4 +150,13 @@ public class ImageUtils { ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile()); ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile()); } + + public static Integer getImageHeight(File imageFile) throws IOException, ImageReadException { + ImageInfo info = Imaging.getImageInfo(imageFile); + return info.getHeight(); + } + public static Integer getImageWidth(File imageFile) throws IOException, ImageReadException { + ImageInfo info = Imaging.getImageInfo(imageFile); + return info.getWidth(); + } } \ No newline at end of file diff --git a/juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java new file mode 100644 index 00000000..3a5c77dd --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java @@ -0,0 +1,71 @@ +package com.juick.service; + +import com.juick.Attachment; +import com.juick.Message; +import com.juick.Photo; +import com.juick.server.util.ImageUtils; +import org.springframework.util.StringUtils; + +import java.io.File; +import java.nio.file.Paths; + +public class ImagesServiceImpl 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); + String imageName = String.format("%s.%s", msg.getMid(), msg.getAttachmentType()); + if (msg.getRid() > 0) { + imageName = String.format("%s-%s.%s", msg.getMid(), msg.getRid(), msg.getAttachmentType()); + } + File fullImage = Paths.get(imgDir, "p", imageName).toFile(); + File mediumImage = Paths.get(imgDir, "photos-1024", imageName).toFile(); + File smallImage = Paths.get(imgDir, "photos-512", imageName).toFile(); + File thumbnailImage = Paths.get(imgDir, "ps", imageName).toFile(); + 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(ImageUtils.getImageHeight(fullImage)); + original.setWidth(ImageUtils.getImageWidth(fullImage)); + + Attachment medium = new Attachment(); + medium.setUrl(photo.getMedium()); + medium.setWidth(ImageUtils.getImageWidth(mediumImage)); + medium.setHeight(ImageUtils.getImageHeight(mediumImage)); + original.setMedium(medium); + + Attachment small = new Attachment(); + small.setUrl(photo.getSmall()); + small.setWidth(ImageUtils.getImageWidth(smallImage)); + small.setHeight(ImageUtils.getImageHeight(smallImage)); + original.setSmall(small); + + Attachment thumb = new Attachment(); + thumb.setUrl(photo.getMedium()); + thumb.setWidth(ImageUtils.getImageWidth(thumbnailImage)); + thumb.setHeight(ImageUtils.getImageHeight(thumbnailImage)); + original.setThumbnail(thumb); + + msg.setAttachment(original); + } + } +} diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java index 02904739..c79ea924 100644 --- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java +++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java @@ -17,7 +17,6 @@ package com.juick.ws.configuration; -import com.juick.configuration.DataConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -31,7 +30,7 @@ public class WebsocketInitializer extends AbstractAnnotationConfigDispatcherServ @Override protected Class[] getRootConfigClasses() { - return new Class[]{ DataConfiguration.class }; + return new Class[]{ }; } @Override diff --git a/juick-www/build.gradle b/juick-www/build.gradle index 91d48c80..3ccb2c6c 100644 --- a/juick-www/build.gradle +++ b/juick-www/build.gradle @@ -24,7 +24,6 @@ apply plugin: 'org.akhikhl.gretty' dependencies { compile project(':juick-server-web') - compile project(':juick-server-jdbc') compile 'com.github.scribejava:scribejava-apis:4.2.0' compile 'com.github.ooxi:serialized-php-parser:0.5.0' compile 'com.sun.mail:javax.mail:1.6.0' diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java index 3a97c4be..c621c450 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java @@ -17,7 +17,6 @@ package com.juick.www.configuration; -import com.juick.configuration.DataConfiguration; import com.juick.configuration.SearchConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; @@ -37,7 +36,6 @@ public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletIni return new Class[]{ WwwServletConfiguration.class, WwwAppConfiguration.class, - DataConfiguration.class, SearchConfiguration.class, SapeConfiguration.class, WebSecurityConfig.class diff --git a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java index b2c3c1f5..88217b9d 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java +++ b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java @@ -112,7 +112,7 @@ public class UserThread { headers += ""; } String cardType = StringUtils.isNotEmpty(msg.getAttachmentType()) ? "summary_large_image" : "summary"; - String msgImage = StringUtils.isNotEmpty(msg.getAttachmentType()) ? msg.getPhotoURLs().getMedium() + String msgImage = StringUtils.isNotEmpty(msg.getAttachmentType()) ? msg.getAttachment().getMedium().getUrl() : "https://i.juick.com/a/" + msg.getUser().getUid() + ".png"; model.addAttribute("ogtype", "article"); String cardDescription = StringEscapeUtils.escapeHtml4(PlainTextFormatter.formatTwitterCard(msg)); diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotInitializer.java b/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotInitializer.java index 8b5d9b94..8b8c3623 100644 --- a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotInitializer.java +++ b/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotInitializer.java @@ -17,7 +17,6 @@ package com.juick.components.configuration; -import com.juick.configuration.DataConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -31,7 +30,7 @@ public class BotInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { - return new Class[]{ DataConfiguration.class }; + return new Class[]{ }; } @Override diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java b/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java index 1bfaf429..8a3beb6a 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java @@ -287,7 +287,7 @@ public class XMPPConnection implements AutoCloseable { } com.juick.Message fullMsg = messagesService.getMessage(jmsg.getMid()); String txt = "@" + jmsg.getUser().getName() + ":" + fullMsg.getTagsString() + "\n"; - String attachment = fullMsg.getAttachmentURL(); + String attachment = fullMsg.getAttachment().getMedium().getUrl(); if (attachment != null) { txt += attachment + "\n"; } @@ -330,7 +330,7 @@ public class XMPPConnection implements AutoCloseable { replyQuote = fullReply.getReplyQuote(); String txt = "Reply by @" + jmsg.getUser().getName() + ":\n" + replyQuote + "\n@" + replyTo + " "; - String attachment = jmsg.getAttachmentURL(); + String attachment = jmsg.getAttachment().getMedium().getUrl(); if (attachment != null) { txt += attachment + "\n"; } @@ -357,7 +357,7 @@ public class XMPPConnection implements AutoCloseable { String txt = "Recommended by @" + recomm.getUser().getName() + ":\n"; txt += "@" + jmsg.getUser().getName() + ":" + jmsg.getTagsString() + "\n"; - String attachment = jmsg.getAttachmentURL(); + String attachment = jmsg.getAttachment().getMedium().getUrl(); if (attachment != null) { txt += attachment + "\n"; } diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java index 5ab4aeb2..34f91a85 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java @@ -17,7 +17,6 @@ package com.juick.components.configuration; -import com.juick.configuration.DataConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -31,7 +30,7 @@ public class XmppInitializer extends AbstractAnnotationConfigDispatcherServletIn @Override protected Class[] getRootConfigClasses() { - return new Class[]{DataConfiguration.class}; + return new Class[]{}; } @Override diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 51641c1d..44827ce6 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -26,10 +26,7 @@ import com.juick.User; import com.juick.server.helpers.TagStats; import com.juick.server.protocol.JuickProtocol; import com.juick.server.protocol.ProtocolListener; -import com.juick.service.MessagesService; -import com.juick.service.SubscriptionService; -import com.juick.service.TagService; -import com.juick.service.UserService; +import com.juick.service.*; import com.juick.service.search.SearchService; import com.juick.www.WebApp; import org.apache.commons.dbcp2.BasicDataSource; @@ -136,6 +133,10 @@ public class ApiTests { public WebApp webApp() { return Mockito.mock(WebApp.class); } + @Bean + public ImagesService imagesService() { + return Mockito.mock(ImagesService.class); + } } @Inject -- cgit v1.2.3