Skip to content

Commit 5937d16

Browse files
committed
Add support for generating image previews.
Fix #387
1 parent 54cd812 commit 5937d16

32 files changed

+448
-23
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111
before_script:
1212
- if [ "$SPRING_PROFILES_ACTIVE" = 'travis' ]; then
1313
mysql -u travis -e 'CREATE DATABASE mystamps CHARACTER SET utf8;';
14-
mkdir -p /tmp/uploads;
14+
mkdir -p /tmp/uploads /tmp/preview;
1515
if [ "$TRAVIS_BRANCH" = 'prod' ]; then
1616
pip install --user ansible==2.1.1.0;
1717
fi;

NEWS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- (functionality) add possibility for adding series sales
1414
- (functionality) add interface for viewing series sales (contributed by Sergey Chechenev)
1515
- (functionality) name of category/country in Russian now are optional fields
16+
- (functionality) preview now is generated after uploading an image
1617

1718
0.3
1819
- (functionality) implemented possibility to user to add series to his collection

docker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ ENV SPRING_PROFILES_ACTIVE=test
1616
# See also: https://docs.docker.com/engine/reference/builder/#run
1717
RUN mkdir /data \
1818
&& useradd mystamps --home-dir /data/mystamps --create-home --comment 'MyStamps' \
19-
&& mkdir /data/uploads /data/heap-dumps \
20-
&& chown mystamps:mystamps /data/uploads /data/heap-dumps
19+
&& mkdir /data/uploads /data/preview /data/heap-dumps \
20+
&& chown mystamps:mystamps /data/uploads /data/preview /data/heap-dumps
2121

2222
# Creates mount points and marks them as holding externally mounted volumes from native host.
2323
# See also: https://docs.docker.com/engine/reference/builder/#volume
24-
VOLUME /data/uploads /data/heap-dumps
24+
VOLUME /data/uploads /data/preview /data/heap-dumps
2525

2626
# Sets the user name to use when running the image and for any subsequent RUN, CMD and ENTRYPOINT instructions.
2727
# See also: https://docs.docker.com/engine/reference/builder/#user

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
<version>${javax.validation.version}</version>
2323
</dependency>
2424

25+
<dependency>
26+
<groupId>net.coobird</groupId>
27+
<artifactId>thumbnailator</artifactId>
28+
<version>${thumbnailator.version}</version>
29+
</dependency>
30+
2531
<dependency>
2632
<groupId>org.apache.commons</groupId>
2733
<artifactId>commons-lang3</artifactId>
@@ -521,6 +527,7 @@
521527
<subethasmtp.version>3.1.7</subethasmtp.version>
522528
<surefire.plugin.version>2.19.1</surefire.plugin.version>
523529
<testng.version>6.8.8</testng.version>
530+
<thumbnailator.version>0.4.8</thumbnailator.version>
524531

525532
<!-- Redefine default value from spring-boot-dependencies (https://github.com/spring-projects/spring-boot/blob/v1.4.6.RELEASE/spring-boot-dependencies/pom.xml) -->
526533
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>

src/main/java/ru/mystamps/web/Url.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public final class Url {
6464

6565
public static final String INFO_COLLECTION_PAGE = "/collection/{slug}";
6666

67-
public static final String GET_IMAGE_PAGE = "/image/{id}";
67+
public static final String GET_IMAGE_PAGE = "/image/{id}";
68+
public static final String GET_IMAGE_PREVIEW_PAGE = "/image/preview/{id}";
6869

6970
public static final String FORBIDDEN_PAGE = "/error/403";
7071
public static final String NOT_FOUND_PAGE = "/error/404";
@@ -145,6 +146,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
145146
map.put("SELECTIZE_CSS", SELECTIZE_CSS);
146147
map.put("SELECTIZE_JS", SELECTIZE_JS);
147148
map.put("GET_IMAGE_PAGE", GET_IMAGE_PAGE);
149+
map.put("GET_IMAGE_PREVIEW_PAGE", GET_IMAGE_PREVIEW_PAGE);
148150
map.put("FAVICON_ICO", FAVICON_ICO);
149151
map.put("MAIN_CSS", MAIN_CSS);
150152
map.put("CATALOG_UTILS_JS", CATALOG_UTILS_JS);
@@ -153,6 +155,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
153155
} else {
154156
// Use separate domain for our own resources
155157
map.put("GET_IMAGE_PAGE", STATIC_RESOURCES_URL + GET_IMAGE_PAGE);
158+
map.put("GET_IMAGE_PREVIEW_PAGE", STATIC_RESOURCES_URL + GET_IMAGE_PREVIEW_PAGE);
156159
map.put("FAVICON_ICO", STATIC_RESOURCES_URL + FAVICON_ICO);
157160
map.put("MAIN_CSS", STATIC_RESOURCES_URL + MAIN_CSS);
158161
map.put("CATALOG_UTILS_JS", STATIC_RESOURCES_URL + CATALOG_UTILS_JS);

src/main/java/ru/mystamps/web/config/ServicesConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public CronService getCronService() {
8080
public ImageService getImageService() {
8181
return new ImageServiceImpl(
8282
strategiesConfig.getImagePersistenceStrategy(),
83+
new TimedImagePreviewStrategy(new ThumbnailatorImagePreviewStrategy()),
8384
daoConfig.getImageDao()
8485
);
8586
}

src/main/java/ru/mystamps/web/config/StrategiesConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class FsStrategiesConfig implements StrategiesConfig {
5757
@Override
5858
public ImagePersistenceStrategy getImagePersistenceStrategy() {
5959
return new FilesystemImagePersistenceStrategy(
60-
env.getRequiredProperty("app.upload.dir")
60+
env.getRequiredProperty("app.upload.dir"),
61+
env.getRequiredProperty("app.preview.dir")
6162
);
6263
}
6364

src/main/java/ru/mystamps/web/controller/ImageController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,27 @@ public void getImage(@PathVariable("id") Integer imageId, HttpServletResponse re
6060
response.getOutputStream().write(image.getData());
6161
}
6262

63+
@GetMapping(Url.GET_IMAGE_PREVIEW_PAGE)
64+
public void getImagePreview(@PathVariable("id") Integer imageId, HttpServletResponse response)
65+
throws IOException {
66+
67+
if (imageId == null) {
68+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
69+
return;
70+
}
71+
72+
ImageDto image = imageService.getOrCreatePreview(imageId);
73+
if (image == null) {
74+
// return original image when error has occurred
75+
getImage(imageId, response);
76+
return;
77+
}
78+
79+
response.setContentType("image/" + image.getType().toLowerCase(Locale.ENGLISH));
80+
response.setContentLength(image.getData().length);
81+
82+
response.getOutputStream().write(image.getData());
83+
}
84+
6385
}
6486

src/main/java/ru/mystamps/web/dao/ImageDataDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
import ru.mystamps.web.dao.dto.DbImageDto;
2222

2323
public interface ImageDataDao {
24-
DbImageDto findByImageId(Integer imageId);
24+
DbImageDto findByImageId(Integer imageId, boolean preview);
2525
Integer add(AddImageDataDbDto imageData);
2626
}

src/main/java/ru/mystamps/web/dao/dto/AddImageDataDbDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
public class AddImageDataDbDto {
2626
private Integer imageId;
2727
private byte[] content;
28+
private boolean preview;
2829
}

0 commit comments

Comments
 (0)