aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service/ImagesServiceImpl.java
blob: 4bdcd7d680f00e785bf1e4731681c14dc37727cc (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
/*
 * 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 com.juick.model.Attachment;
import com.juick.model.Message;
import com.juick.model.Photo;
import com.juick.util.ImageUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class ImagesServiceImpl implements ImagesService {
    private ImageUtils imageUtils;
    private String imgDir;

    public ImagesServiceImpl(String imgDir, String tmpDir) {
        this.imgDir = imgDir;
        imageUtils = new ImageUtils(imgDir, tmpDir);
    }
    @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(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 = imageUtils.getAttachment(fullImage);
            original.setUrl(originalUrl);

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

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

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

            msg.setAttachment(original);
        }
    }

    @Override
    public void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException {
        imageUtils.saveImageWithPreviews(tempFilename, outputFilename);
    }

    @Override
    public void saveAvatar(String tempFilename, int uid) throws IOException {
        imageUtils.saveAvatar(tempFilename, uid);
    }
}