19 package org.sleuthkit.autopsy.discovery.search;
21 import com.google.common.io.Files;
22 import java.awt.Image;
23 import java.io.BufferedReader;
25 import java.io.FileReader;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.Reader;
29 import java.nio.file.Paths;
30 import java.util.Collection;
31 import java.util.logging.Level;
32 import org.apache.commons.lang.StringUtils;
33 import org.openide.util.Lookup;
52 class SummaryHelpers {
54 private static final int PREVIEW_SIZE = 256;
55 private final static Logger logger = Logger.getLogger(SummaryHelpers.class.getName());
56 private static volatile TextSummarizer summarizerToUse = null;
58 private SummaryHelpers() {
69 static TextSummary getDefaultSummary(AbstractFile file) {
71 int countOfImages = 0;
73 Content largestChild = null;
74 for (Content child : file.getChildren()) {
75 if (child instanceof AbstractFile && ImageUtils.isImageThumbnailSupported((AbstractFile) child)) {
77 if (largestChild == null || child.getSize() > largestChild.getSize()) {
82 if (largestChild != null) {
83 image = ImageUtils.getThumbnail(largestChild, ImageUtils.ICON_SIZE_LARGE);
85 }
catch (TskCoreException ex) {
86 logger.log(Level.WARNING,
"Error getting children for file: " + file.getId(), ex);
88 image = image == null ? image : image.getScaledInstance(ImageUtils.ICON_SIZE_MEDIUM, ImageUtils.ICON_SIZE_MEDIUM,
90 String summaryText = null;
91 if (file.getMd5Hash() != null) {
93 summaryText = getSavedSummary(Paths.get(Case.getCurrentCaseThrows().getCacheDirectory(),
"summaries", file.getMd5Hash() +
"-default-" + PREVIEW_SIZE +
"-translated.txt").toString());
94 }
catch (NoCurrentCaseException ex) {
95 logger.log(Level.WARNING,
"Unable to retrieve saved summary. No case is open.", ex);
98 if (StringUtils.isBlank(summaryText)) {
99 String firstLines = getFirstLines(file);
100 String translatedFirstLines = getTranslatedVersion(firstLines);
101 if (!StringUtils.isBlank(translatedFirstLines)) {
102 summaryText = translatedFirstLines;
103 if (file.getMd5Hash() != null) {
105 saveSummary(summaryText, Paths.get(Case.getCurrentCaseThrows().getCacheDirectory(),
"summaries", file.getMd5Hash() +
"-default-" + PREVIEW_SIZE +
"-translated.txt").toString());
106 }
catch (NoCurrentCaseException ex) {
107 logger.log(Level.WARNING,
"Unable to save translated summary. No case is open.", ex);
111 summaryText = firstLines;
114 return new TextSummary(summaryText, image, countOfImages);
126 static String getTranslatedVersion(String documentString) {
128 TextTranslationService translatorInstance = TextTranslationService.getInstance();
129 if (translatorInstance.hasProvider()) {
130 String translatedResult = translatorInstance.translate(documentString);
131 if (translatedResult.isEmpty() ==
false) {
132 return translatedResult;
135 }
catch (NoServiceProviderException | TranslationException ex) {
136 logger.log(Level.INFO,
"Error translating string for summary", ex);
150 static String getSavedSummary(String summarySavePath) {
151 if (summarySavePath == null) {
154 File savedFile =
new File(summarySavePath);
155 if (savedFile.exists()) {
156 try (BufferedReader bReader =
new BufferedReader(
new FileReader(savedFile))) {
158 StringBuilder sBuilder =
new StringBuilder(PREVIEW_SIZE);
159 String sCurrentLine = bReader.readLine();
160 while (sCurrentLine != null) {
161 sBuilder.append(sCurrentLine).append(
'\n');
162 sCurrentLine = bReader.readLine();
164 return sBuilder.toString();
165 }
catch (IOException ingored) {
171 Files.createParentDirs(savedFile);
172 }
catch (IOException ex) {
173 logger.log(Level.WARNING,
"Unable to create summaries directory in case folder for file at: " + summarySavePath, ex);
186 static void saveSummary(String summary, String summarySavePath) {
187 if (summarySavePath == null) {
190 try (FileWriter myWriter =
new FileWriter(summarySavePath)) {
191 myWriter.write(summary);
192 }
catch (IOException ex) {
193 logger.log(Level.WARNING,
"Unable to save summary at: " + summarySavePath, ex);
204 static String getFirstLines(AbstractFile file) {
205 TextExtractor extractor;
207 extractor = TextExtractorFactory.getExtractor(file, null);
208 }
catch (TextExtractorFactory.NoTextExtractorFound ignored) {
210 extractor = TextExtractorFactory.getStringsExtractor(file, null);
213 try (Reader reader = extractor.getReader()) {
214 char[] cbuf =
new char[PREVIEW_SIZE];
215 reader.read(cbuf, 0, PREVIEW_SIZE);
216 return new String(cbuf);
217 }
catch (IOException ex) {
218 return Bundle.FileSearch_documentSummary_noBytes();
219 }
catch (TextExtractor.InitReaderException ex) {
220 return Bundle.FileSearch_documentSummary_noPreview();
231 static TextSummarizer getLocalSummarizer() {
232 if (summarizerToUse == null) {
233 Collection<? extends TextSummarizer> summarizers
234 = Lookup.getDefault().lookupAll(TextSummarizer.class
236 if (!summarizers.isEmpty()) {
237 summarizerToUse = summarizers.iterator().next();
240 return summarizerToUse;