Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import edu.harvard.iq.dataverse.export.ExportException;
import edu.harvard.iq.dataverse.export.ExportService;
import edu.harvard.iq.dataverse.util.EjbUtil;
import edu.harvard.iq.dataverse.util.FileTypeDetection;
import edu.harvard.iq.dataverse.util.FileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -62,7 +63,7 @@ public DataFile execute(CommandContext ctxt) throws CommandException {
}

logger.fine("target file: " + localFile);
String newlyDetectedContentType = FileTypeDetection.determineFileType(localFile);
String newlyDetectedContentType = FileUtil.determineFileType(localFile, fileToRedetect.getDisplayName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a null check in the next line here? - From looking at #8821, it appears that determineFileType() may return null here, for especially unrecognizable files; and that results in a constraint violation.
Sorry for asking for this while the PR is already in QA; but would be a trivial change. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in FileUtil.java in this PR prevents a null return, so I think we're good?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, of course, doh! Thanks!

fileToRedetect.setContentType(newlyDetectedContentType);
} catch (IOException ex) {
throw new CommandException("Exception while attempting to get the bytes of the file during file type redetection: " + ex.getLocalizedMessage(), this);
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/edu/harvard/iq/dataverse/util/FileTypeDetection.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ public static String determineFileType(File f, String fileName) throws IOExcepti
}
}

if(fileType==null) {
fileType = MIME_TYPE_UNDETERMINED_DEFAULT;
}
logger.fine("returning fileType "+fileType);
return fileType;
}
Expand Down

This file was deleted.

12 changes: 12 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/util/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ public void testDetermineFileTypeByExtension() {
fail("File does not exist: " + file.toPath().toString());
}
}

@Test
public void testDetermineFileTypeFromName() {
//Verify that name of the local file isn't used in determining the type (as we often use *.tmp when the real name has a different extension)
try {
File file = File.createTempFile("empty", "png");
assertEquals("text/plain", FileUtil.determineFileType(file, "something.txt"));
} catch (IOException ex) {
Logger.getLogger(FileUtilTest.class.getName()).log(Level.SEVERE, null, ex);
}

}

@Test
public void testDetermineFileTypeByName() {
Expand Down