Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
TikaFileTypeDetector.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2014 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.modules.filetypeid;
20 
21 import java.util.SortedSet;
22 import org.apache.tika.Tika;
23 import org.apache.tika.mime.MediaType;
24 import org.apache.tika.mime.MimeTypes;
25 
26 import org.sleuthkit.datamodel.AbstractFile;
27 import org.sleuthkit.datamodel.BlackboardArtifact;
28 import org.sleuthkit.datamodel.BlackboardAttribute;
29 import org.sleuthkit.datamodel.TskCoreException;
30 
35 @Deprecated
36 public class TikaFileTypeDetector {
37 
38  private static final Tika tikaInst = new Tika(); //calling detect() with this should be thread-safe
39  private final int BUFFER_SIZE = 64 * 1024; //how many bytes to pass in
40  private final byte buffer[] = new byte[BUFFER_SIZE];
41 
53  @Deprecated
54  public synchronized String detectAndSave(AbstractFile abstractFile) throws TskCoreException {
55  String mimeType = detect(abstractFile);
56  if (mimeType != null) {
57  // add artifact
58  BlackboardArtifact getInfoArt = abstractFile.getGenInfoArtifact();
59  BlackboardAttribute batt = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_FILE_TYPE_SIG.getTypeID(), FileTypeIdModuleFactory.getModuleName(), mimeType);
60  getInfoArt.addAttribute(batt);
61 
62  // we don't fire the event because we just updated TSK_GEN_INFO, which isn't displayed in the tree and is vague.
63  }
64  return mimeType;
65  }
66 
76  @Deprecated
77  public synchronized String detect(AbstractFile abstractFile) {
78  try {
79  byte buf[];
80  int len = abstractFile.read(buffer, 0, BUFFER_SIZE);
81  if (len < BUFFER_SIZE) {
82  buf = new byte[len];
83  System.arraycopy(buffer, 0, buf, 0, len);
84  } else {
85  buf = buffer;
86  }
87 
88  String mimetype = tikaInst.detect(buf, abstractFile.getName());
89  // Remove tika's name out of the general types like msoffice and ooxml
90  return mimetype.replace("tika-", ""); //NON-NLS
91  } catch (Exception ex) {
92  //do nothing
93  }
94  return null;
95  }
96 
107  @Deprecated
108  public boolean isMimeTypeDetectable(String mimeType) {
109  boolean ret = false;
110 
111  SortedSet<MediaType> m = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
112  String[] split = mimeType.split("/");
113 
114  if (split.length == 2) {
115  String type = split[0];
116  String subtype = split[1];
117  MediaType mediaType = new MediaType(type, subtype);
118  ret = m.contains(mediaType);
119  }
120 
121  return ret;
122  }
123 }
synchronized String detectAndSave(AbstractFile abstractFile)
synchronized String detect(AbstractFile abstractFile)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.