Autopsy  4.19.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SummaryHelpers.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.discovery.search;
20 
21 import com.google.common.io.Files;
22 import java.awt.Image;
23 import java.io.BufferedReader;
24 import java.io.File;
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;
45 import org.sleuthkit.datamodel.AbstractFile;
46 import org.sleuthkit.datamodel.Content;
47 import org.sleuthkit.datamodel.TskCoreException;
48 
52 class SummaryHelpers {
53 
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;
57 
58  private SummaryHelpers() {
59  // Class should not be instantiated
60  }
61 
69  static TextSummary getDefaultSummary(AbstractFile file) {
70  Image image = null;
71  int countOfImages = 0;
72  try {
73  Content largestChild = null;
74  for (Content child : file.getChildren()) {
75  if (child instanceof AbstractFile && ImageUtils.isImageThumbnailSupported((AbstractFile) child)) {
76  countOfImages++;
77  if (largestChild == null || child.getSize() > largestChild.getSize()) {
78  largestChild = child;
79  }
80  }
81  }
82  if (largestChild != null) {
83  image = ImageUtils.getThumbnail(largestChild, ImageUtils.ICON_SIZE_LARGE);
84  }
85  } catch (TskCoreException ex) {
86  logger.log(Level.WARNING, "Error getting children for file: " + file.getId(), ex);
87  }
88  image = image == null ? image : image.getScaledInstance(ImageUtils.ICON_SIZE_MEDIUM, ImageUtils.ICON_SIZE_MEDIUM,
89  Image.SCALE_SMOOTH);
90  String summaryText = null;
91  if (file.getMd5Hash() != null) {
92  try {
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);
96  }
97  }
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) {
104  try {
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);
108  }
109  }
110  } else {
111  summaryText = firstLines;
112  }
113  }
114  return new TextSummary(summaryText, image, countOfImages);
115  }
116 
126  static String getTranslatedVersion(String documentString) {
127  try {
128  TextTranslationService translatorInstance = TextTranslationService.getInstance();
129  if (translatorInstance.hasProvider()) {
130  String translatedResult = translatorInstance.translate(documentString);
131  if (translatedResult.isEmpty() == false) {
132  return translatedResult;
133  }
134  }
135  } catch (NoServiceProviderException | TranslationException ex) {
136  logger.log(Level.INFO, "Error translating string for summary", ex);
137  }
138  return null;
139  }
140 
150  static String getSavedSummary(String summarySavePath) {
151  if (summarySavePath == null) {
152  return null;
153  }
154  File savedFile = new File(summarySavePath);
155  if (savedFile.exists()) {
156  try (BufferedReader bReader = new BufferedReader(new FileReader(savedFile))) {
157  // pass the path to the file as a parameter
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();
163  }
164  return sBuilder.toString();
165  } catch (IOException ingored) {
166  //summary file may not exist or may be incomplete in which case return null so a summary can be generated
167  return null; //no saved summary was able to be found
168  }
169  } else {
170  try { //if the file didn't exist make sure the parent directories exist before we move on to creating a summary
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);
174  }
175  return null; //no saved summary was able to be found
176  }
177 
178  }
179 
186  static void saveSummary(String summary, String summarySavePath) {
187  if (summarySavePath == null) {
188  return; //can't save a summary if we don't have a path
189  }
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);
194  }
195  }
196 
204  static String getFirstLines(AbstractFile file) {
205  TextExtractor extractor;
206  try {
207  extractor = TextExtractorFactory.getExtractor(file, null);
208  } catch (TextExtractorFactory.NoTextExtractorFound ignored) {
209  //no extractor found, use Strings Extractor
210  extractor = TextExtractorFactory.getStringsExtractor(file, null);
211  }
212 
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();
221  }
222  }
223 
231  static TextSummarizer getLocalSummarizer() {
232  if (summarizerToUse == null) {
233  Collection<? extends TextSummarizer> summarizers
234  = Lookup.getDefault().lookupAll(TextSummarizer.class
235  );
236  if (!summarizers.isEmpty()) {
237  summarizerToUse = summarizers.iterator().next();
238  }
239  }
240  return summarizerToUse;
241  }
242 
243 }

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.