Skip to content

Commit

Permalink
Fix #18: Improve content type detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
guusdk committed Nov 17, 2019
1 parent d8a775e commit afef6c1
Showing 1 changed file with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.net.URLConnection;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
Expand Down Expand Up @@ -92,7 +90,7 @@ public boolean contains( UUID uuid )
final Path path = Paths.get( repository.toString(), uuid.toString() );
final boolean result = Files.exists( path );

Log.debug( "UUID '{}' {} exist in respository.", uuid, result ? "does" : "does not" );
Log.debug( "UUID '{}' {} exist in repository.", uuid, result ? "does" : "does not" );
return result;
}

Expand All @@ -119,7 +117,22 @@ public String getContentType( UUID uuid )
try
{
final Path path = Paths.get( repository.toString(), uuid.toString() );
final String result = Files.probeContentType( path );

String result;
try ( final InputStream is = Files.newInputStream( path ) ) {
Log.debug( "UUID '{}' Probing content type based on file content...", uuid );
result = URLConnection.guessContentTypeFromStream( is );
}

if ( result == null || result.isEmpty() ) {
Log.debug( "UUID '{}' Probing content type based on system installed file type detectors...", uuid );
result = Files.probeContentType( path );
}

if ( result == null || result.isEmpty() ) {
Log.debug( "UUID '{}' Probing content type based on file name...", uuid );
result = URLConnection.guessContentTypeFromName( path.getFileName().toString() );
}

Log.debug( "UUID '{}' content type: {}", uuid, result );
return result;
Expand Down

0 comments on commit afef6c1

Please sign in to comment.