Skip to content

Commit

Permalink
Merge pull request #93 from tagbangers/develop
Browse files Browse the repository at this point in the history
1.0.0.M17
  • Loading branch information
ogawa-takeshi authored Sep 24, 2017
2 parents fffd276 + c022b41 commit 0bc304d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 63 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<version>1.5.7.RELEASE</version>
</parent>
<groupId>org.wallride</groupId>
<artifactId>wallride</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
<packaging>pom</packaging>
<name>WallRide</name>
<description>CMS for developers of principles</description>
Expand Down Expand Up @@ -80,7 +80,7 @@
<dependency>
<groupId>org.wallride</groupId>
<artifactId>wallride-core</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
</dependency>

<!-- Spring -->
Expand Down
2 changes: 1 addition & 1 deletion wallride-bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.wallride</groupId>
<artifactId>wallride-parent</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
<relativePath>../wallride-parent/pom.xml</relativePath>
</parent>
<artifactId>wallride-bootstrap</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion wallride-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.wallride</groupId>
<artifactId>wallride</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>wallride-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@

package org.wallride.autoconfigure;

import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
Expand All @@ -44,6 +47,7 @@
import org.wallride.support.StringFormatter;
import org.wallride.web.support.*;

import javax.servlet.ServletContext;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,15 +116,29 @@ public MessageCodesResolver getMessageCodesResolver() {
// additional webmvc-related beans

@Bean
public SimpleUrlHandlerMapping mediaUrlHandlerMapping() {
MediaHttpRequestHandler mediaHttpRequestHandler = new MediaHttpRequestHandler();
mediaHttpRequestHandler.setWallRideProperties(wallRideProperties);
mediaHttpRequestHandler.setMediaService(mediaService);
mediaHttpRequestHandler.setResourceLoader(resourceLoader);
mediaHttpRequestHandler.setCacheSeconds(86400);
public SimpleUrlHandlerMapping mediaUrlHandlerMapping(
ApplicationContext applicationContext,
ServletContext servletContext,
ContentNegotiationManager contentNegotiationManager) {
MediaHttpRequestHandler handler = new MediaHttpRequestHandler();

handler.setServletContext(servletContext);
handler.setApplicationContext(applicationContext);
handler.setContentNegotiationManager(contentNegotiationManager);

handler.setWallRideProperties(wallRideProperties);
handler.setMediaService(mediaService);
handler.setResourceLoader(resourceLoader);
handler.setCacheSeconds(86400);

try {
handler.afterPropertiesSet();
} catch (Exception e) {
throw new BeanInitializationException("Failed to init MediaHttpRequestHandler", e);
}

Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<>();
urlMap.put("/media/{key}", mediaHttpRequestHandler);
urlMap.put("/media/{key}", handler);

SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setOrder(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.support.WebContentGenerator;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.wallride.autoconfigure.WallRideProperties;
import org.wallride.domain.Media;
import org.wallride.service.MediaService;
import org.wallride.support.ExtendedResourceUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
Expand All @@ -49,7 +47,9 @@
import java.net.URLEncoder;
import java.util.Map;

public class MediaHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler, InitializingBean {
public class MediaHttpRequestHandler extends ResourceHttpRequestHandler implements InitializingBean {

private static final String MEDIA_ATTRIBUTE = MediaHttpRequestHandler.class.getName() + ".MEDIA";

private WallRideProperties wallRideProperties;

Expand All @@ -72,53 +72,21 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
}

@Override
public void afterPropertiesSet() throws Exception {

}

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
checkAndPrepare(request, response, true);

protected Resource getResource(HttpServletRequest request) throws IOException {
Map<String, Object> pathVariables = (Map<String, Object>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String key = (String) pathVariables.get("key");

Media media = mediaService.getMedia(key);
int width = ServletRequestUtils.getIntParameter(request, "w", 0);
int height = ServletRequestUtils.getIntParameter(request, "h", 0);
int mode = ServletRequestUtils.getIntParameter(request, "m", 0);

Resource resource = readResource(media, width, height, Media.ResizeMode.values()[mode]);

if (resource == null) {
logger.debug("No matching resource found - returning 404");
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
logger.debug("Resource not modified - returning 304");
return;
}

long length = resource.contentLength();
if (length > Integer.MAX_VALUE) {
throw new IOException("Resource content too long (beyond Integer.MAX_VALUE): " + resource);
}

response.setContentLength((int) length);
response.setContentType(media.getMimeType());
if (!"image".equals(MediaType.parseMediaType(media.getMimeType()).getType())) {
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(media.getOriginalName(), "UTF-8"));
if (media == null) {
return null;
}

FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
}
RequestContextHolder.getRequestAttributes().setAttribute(MEDIA_ATTRIBUTE, media, RequestAttributes.SCOPE_REQUEST);

// public Resource readResource(Media media) throws IOException, EncoderException {
// return readResource(media, 0, 0, null);
// }
int width = ServletRequestUtils.getIntParameter(request, "w", 0);
int height = ServletRequestUtils.getIntParameter(request, "h", 0);
Media.ResizeMode mode = Media.ResizeMode.values()[ServletRequestUtils.getIntParameter(request, "m", 0)];

private Resource readResource(final Media media, final int width, final int height, final Media.ResizeMode mode) throws IOException {
// Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
// final Resource prefix = resourceLoader.getResource(blog.getMediaPath());
final Resource prefix = resourceLoader.getResource(wallRideProperties.getMediaLocation());
Expand All @@ -141,7 +109,6 @@ private Resource readResource(final Media media, final int width, final int heig
temp.deleteOnExit();
resizeImage(resource, temp, width, height, mode);

// AmazonS3ResourceUtils.writeFile(temp, resized);
ExtendedResourceUtils.write(resized, temp);
FileUtils.deleteQuietly(temp);
}
Expand Down Expand Up @@ -201,4 +168,19 @@ private void resizeImage(Resource resource, File file, int width, int height, Me
long stopTime = System.currentTimeMillis();
logger.debug("Resized image: time [{}ms]", stopTime - startTime);
}

@Override
protected MediaType getMediaType(HttpServletRequest request, Resource resource) {
Media media = (Media) RequestContextHolder.getRequestAttributes().getAttribute(MEDIA_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
return MediaType.parseMediaType(media.getMimeType());
}

@Override
protected void setHeaders(HttpServletResponse response, Resource resource, MediaType mediaType) throws IOException {
super.setHeaders(response, resource, mediaType);
if (!"image".equals(mediaType.getType())) {
Media media = (Media) RequestContextHolder.getRequestAttributes().getAttribute(MEDIA_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(media.getOriginalName(), "UTF-8"));
}
}
}
3 changes: 2 additions & 1 deletion wallride-core/src/main/resources/templates/admin/froala.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@

var selector = /*[[${selector}]]*/ '#';
$(selector).froalaEditor({
toolbarButtons: ['paragraphFormat', 'placeholders', 'bold', 'underline', 'formatUL', 'color', 'align', 'quote', 'insertLink', 'insertImage', 'insertFile', 'insertTable', 'undo', 'redo', 'html', 'fullscreen'],
toolbarButtons: ['paragraphFormat', 'placeholders', 'bold', 'underline', 'formatUL', 'color', 'align', 'quote', 'insertLink', 'insertImage', 'insertVideo', 'insertFile', 'insertTable', 'undo', 'redo', 'html', 'fullscreen'],
heightMin: 300,
imageDefaultWidth: 0,
imageDefaultAlign: 'left',
imageUploadURL: /*[[@{__${ADMIN_PATH}__/media/create.json}]]*/ '#',
imageManagerLoadURL: /*[[@{__${ADMIN_PATH}__/media/index.json}]]*/ '#',
fileUploadURL: /*[[@{__${ADMIN_PATH}__/media/create.json}]]*/ '#',
videoUploadURL: /*[[@{__${ADMIN_PATH}__/media/create.json}]]*/ '#',
htmlRemoveTags: []
});
});
Expand Down
6 changes: 3 additions & 3 deletions wallride-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<version>1.5.7.RELEASE</version>
<relativePath />
</parent>
<groupId>org.wallride</groupId>
<artifactId>wallride-parent</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
<packaging>pom</packaging>
<name>WallRide Parent</name>
<description>CMS for developers of principles</description>
Expand Down Expand Up @@ -68,7 +68,7 @@
<dependency>
<groupId>org.wallride</groupId>
<artifactId>wallride-core</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion wallride-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.wallride</groupId>
<artifactId>wallride</artifactId>
<version>1.0.0.M16</version>
<version>1.0.0.M17</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>wallride-tools</artifactId>
Expand Down

0 comments on commit 0bc304d

Please sign in to comment.