aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service/FileSystemStorageService.java
blob: 021e5a9fdbf10f40ccd624a563160d3db9c30452 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2008-2020, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import com.juick.model.Attachment;
import com.juick.model.Message;
import com.juick.model.Photo;
import com.juick.model.User;

import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.tiff.TiffField;
import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Rotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

public class FileSystemStorageService implements StorageService {

    private static final Logger logger = LoggerFactory.getLogger(StorageService.class);

    private final String baseDir;
    private final String imgDir;
    private final String tmpDir;
    private final String avatarDir, avatarSmallDir, avatarOriginalDir;
    private final String fullImageDir, mediumImageDir, smallImageDir, thumbnailImageDir;

    public FileSystemStorageService(String baseDir, String tmpDir) {
        this.baseDir = baseDir;
        this.imgDir = Paths.get(baseDir, "i").toString();
        this.tmpDir = tmpDir;
        this.avatarDir = Paths.get(imgDir, "a").toString();
        this.avatarOriginalDir = Paths.get(imgDir, "ao").toString();
        this.avatarSmallDir = Paths.get(imgDir, "as").toString();
        this.fullImageDir = Paths.get(imgDir, "p").toString();
        this.mediumImageDir = Paths.get(imgDir, "photos-1024").toString();
        this.smallImageDir = Paths.get(imgDir, "photos-512").toString();
        this.thumbnailImageDir = Paths.get(imgDir, "ps").toString();
    }

    @Override
    public void setAttachmentMetadata(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(fullImageDir, imageName).toFile();
            File mediumImage = Paths.get(mediumImageDir, imageName).toFile();
            File smallImage = Paths.get(smallImageDir, imageName).toFile();
            File thumbnailImage = Paths.get(thumbnailImageDir, 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 = getAttachment(fullImage);
            original.setUrl(originalUrl);

            Attachment medium = getAttachment(mediumImage);
            medium.setUrl(photo.getMedium());
            original.setMedium(medium);

            Attachment small = getAttachment(smallImage);
            small.setUrl(photo.getSmall());
            original.setSmall(small);

            Attachment thumb = getAttachment(thumbnailImage);
            thumb.setUrl(photo.getMedium());
            original.setThumbnail(thumb);

            msg.setAttachment(original);
        }
    }

    /**
     * Returns <code>BufferedImage</code>, same as <code>ImageIO.read()</code> does.
     *
     * <p>
     * JPEG images with EXIF metadata are rotated according to Orientation tag.
     *
     * @param imageFile a <code>File</code> to read from.
     */
    private static BufferedImage readImageWithOrientation(File imageFile) throws IOException {

        BufferedImage image = ImageIO.read(imageFile);
        if (!FilenameUtils.getExtension(imageFile.getName()).equals("jpg")) {
            return image;
        }

        try {
            ImageMetadata metadata = Imaging.getMetadata(imageFile);

            if (metadata instanceof JpegImageMetadata) {
                JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
                TiffField orientationField = jpegMetadata.findEXIFValue(TiffTagConstants.TIFF_TAG_ORIENTATION);

                if (orientationField != null) {
                    int orientation = orientationField.getIntValue();
                    switch (orientation) {
                    case TiffTagConstants.ORIENTATION_VALUE_ROTATE_90_CW:
                        image = Scalr.rotate(image, Rotation.CW_90);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_ROTATE_180:
                        image = Scalr.rotate(image, Rotation.CW_180);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_ROTATE_270_CW:
                        image = Scalr.rotate(image, Rotation.CW_270);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL:
                        image = Scalr.rotate(image, Rotation.FLIP_HORZ);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_MIRROR_VERTICAL:
                        image = Scalr.rotate(image, Rotation.FLIP_VERT);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_90_CW:
                        image = Scalr.rotate(Scalr.rotate(image, Rotation.FLIP_HORZ), Rotation.CW_90);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_270_CW:
                        image = Scalr.rotate(Scalr.rotate(image, Rotation.FLIP_HORZ), Rotation.CW_270);
                        break;
                    case TiffTagConstants.ORIENTATION_VALUE_HORIZONTAL_NORMAL:
                    default:
                        // do nothing
                        break;
                    }
                }
            }
        } catch (ImageReadException | IOException e) {
            // failed to read metadata.
            // nothing to do here, return image as is.
        }

        return image;
    }

    @Override
    public void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException {
        String ext = FilenameUtils.getExtension(outputFilename);

        Path outputImagePath = Paths.get(fullImageDir, outputFilename);
        // this throws strange exceptions
        // Files.move(Paths.get(tmpDir, tempFilename), outputImagePath);
        FileUtils.moveFile(Paths.get(tmpDir, tempFilename).toFile(), outputImagePath.toFile());
        BufferedImage originalImage = readImageWithOrientation(outputImagePath.toFile());

        int width = originalImage.getWidth();
        int height = originalImage.getHeight();
        int maxDimension = Math.max(width, height);
        BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage;
        BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage;
        BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage;
        ImageIO.write(image1024, ext, Paths.get(mediumImageDir, outputFilename).toFile());
        ImageIO.write(image0512, ext, Paths.get(smallImageDir, outputFilename).toFile());
        ImageIO.write(image0160, ext, Paths.get(thumbnailImageDir, outputFilename).toFile());
    }

    private String getAvatarFileName(User user, String targetExtension) {
        return String.format("%s.%s", user.getUid(), targetExtension);
    }

    private Path getAvatarPath(User user) {
        return Paths.get(avatarDir, getAvatarFileName(user, "png"));
    }

    public void saveAvatar(String tempFilename, User user) throws IOException {
        String ext = FilenameUtils.getExtension(tempFilename);
        String originalName = getAvatarFileName(user, ext);
        Path originalPath = Paths.get(avatarOriginalDir, originalName);
        Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING);
        BufferedImage originalImage = ImageIO.read(originalPath.toFile());
        String targetExt = "png";
        ImageIO.write(Scalr.resize(originalImage, 96), targetExt, getAvatarPath(user).toFile());
        ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(avatarSmallDir, getAvatarFileName(user, targetExt)).toFile());
    }

    public Attachment getAttachment(File imgFile) throws IOException {
        Attachment attachment = new Attachment();
        if (imgFile.exists()) {
            try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) {
                Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
                while (iter.hasNext()) {
                    ImageReader reader = iter.next();
                    try {
                        reader.setInput(stream);
                        attachment.setWidth(reader.getWidth(reader.getMinIndex()));
                        attachment.setHeight(reader.getHeight(reader.getMinIndex()));
                        return attachment;
                    } catch (Exception e) {
                        logger.debug("Error reading {}, trying next reader", imgFile.getAbsolutePath());
                    } finally {
                        reader.dispose();
                    }
                }
            }
        } else {
            logger.warn("File not exists yet: {}", imgFile.getAbsolutePath());
            return attachment;
        }

        logger.warn("Not a known image file {}", imgFile.getAbsolutePath());
        return attachment;
    }

    public Attachment getAvatarMetadata(User user) throws IOException {
        return getAttachment(getAvatarPath(user).toFile());
    }

    @Override
    public String getBaseDirectory() {
        return baseDir;
    }

    @Override
    public String getImageDirectory() {
        return imgDir;
    }

    @Override
    public String getTemporaryDirectory() {
        return tmpDir;
    }
}