Skip to content

Commit

Permalink
Feat: Use tika to detect content type more accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
gongy committed Jul 29, 2022
1 parent 1a4d466 commit e421cc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>jaxb</artifactId>
<version>2.3.6-1</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>aws-s3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import jenkins.model.ArtifactManager;
import jenkins.util.VirtualFile;
import org.apache.http.client.methods.HttpGet;
import org.apache.tika.Tika;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.BlobStores;
Expand Down Expand Up @@ -159,13 +160,21 @@ public Map<String, String> invoke(File f, VirtualChannel channel) {
if (contentType == null) {
contentType = URLConnection.guessContentTypeFromName(theFile.getName());
}
if (contentType == null){
contentType = detectByTika(theFile);
}
contentTypes.put(relPath, contentType);
} catch (IOException e) {
Functions.printStackTrace(e, listener.error("Unable to determine content type for file: " + theFile));
}
}
return contentTypes;
}

static String detectByTika(File f) throws IOException {
Tika tika = new Tika();
return tika.detect(f);
}
}

private static class UploadToBlobStorage extends MasterToSlaveFileCallable<Void> {
Expand Down

0 comments on commit e421cc7

Please sign in to comment.