1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.thumbnail;
17
18 import java.io.BufferedInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.util.Map;
22
23 import javax.annotation.Resource;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.codelibs.core.lang.StringUtil;
27 import org.codelibs.fess.app.web.base.FessSearchAction;
28 import org.codelibs.fess.util.DocumentUtil;
29 import org.lastaflute.web.Execute;
30 import org.lastaflute.web.response.ActionResponse;
31
32 public class ThumbnailAction extends FessSearchAction {
33
34
35
36
37 @Resource
38 protected HttpServletResponse response;
39
40
41
42
43
44
45
46
47 @Execute
48 public ActionResponse index(final ThumbnailForm form) {
49 validate(form, messages -> {}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
50 if (isLoginRequired()) {
51 return redirectToLogin();
52 }
53
54 final Map<String, Object> doc =
55 searchHelper.getDocumentByDocId(form.docId, queryHelper.getResponseFields(), getUserBean()).orElse(null);
56 final String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldThumbnail(), String.class);
57 if (StringUtil.isBlank(form.queryId) || StringUtil.isBlank(url) || !thumbnailSupport) {
58
59 throw responseManager.new404("Thumbnail for " + form.docId + " is not found.");
60 }
61
62 final File thumbnailFile = thumbnailManager.getThumbnailFile(doc);
63 if (thumbnailFile == null) {
64 if (fessConfig.isThumbnailEnabled()) {
65 thumbnailManager.offer(doc);
66 }
67
68 throw responseManager.new404("Thumbnail for " + form.docId + " is under generating.");
69 }
70
71 return asStream(form.docId).contentType(getImageMimeType(thumbnailFile)).stream(out -> {
72 try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(thumbnailFile))) {
73 out.write(in);
74 }
75 });
76 }
77
78 protected String getImageMimeType(final File imageFile) {
79 final String path = imageFile.getAbsolutePath();
80 if (path.endsWith(".png")) {
81 return "image/png";
82 }
83 if (path.endsWith(".gif")) {
84 return "image/gif";
85 }
86 if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
87 return "image/jpeg";
88 } else {
89 return "application/octet-stream";
90 }
91 }
92 }