View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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      //                                                                           Attribute
36      //
37      @Resource
38      protected HttpServletResponse response;
39  
40      // ===================================================================================
41      //                                                                               Hook
42      //                                                                              ======
43  
44      // ===================================================================================
45      //                                                                      Search Execute
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              // 404
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              // 404
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  }