Skip to content

Commit

Permalink
switch to mime type check #9601
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed May 24, 2023
1 parent 8f83738 commit 505e8f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1185,52 +1185,14 @@ public boolean fileMetadataExtractable(DataFile dataFile) {
// Inspired by fileMetadataExtractable, above
public boolean fileMetadataExtractableFromNetcdf(DataFile dataFile, Path tempLocationPath) {
logger.fine("fileMetadataExtractableFromNetcdf dataFileIn: " + dataFile + ". tempLocationPath: " + tempLocationPath);
boolean extractable = false;
String dataFileLocation = null;
if (tempLocationPath != null) {
// This file was just uploaded and hasn't been saved to S3 or local storage.
dataFileLocation = tempLocationPath.toString();
} else {
// This file is already on S3 or local storage.
File tempFile = null;
File localFile;
StorageIO<DataFile> storageIO;
try {
storageIO = dataFile.getStorageIO();
storageIO.open();
if (storageIO.isLocalFile()) {
localFile = storageIO.getFileSystemPath().toFile();
dataFileLocation = localFile.getAbsolutePath();
logger.info("fileMetadataExtractable2: file is local. Path: " + dataFileLocation);
} else {
// Need to create a temporary local file:
tempFile = File.createTempFile("tempFileExtractMetadataNcml", ".tmp");
try ( ReadableByteChannel targetFileChannel = (ReadableByteChannel) storageIO.getReadChannel(); FileChannel tempFileChannel = new FileOutputStream(tempFile).getChannel();) {
tempFileChannel.transferFrom(targetFileChannel, 0, storageIO.getSize());
}
dataFileLocation = tempFile.getAbsolutePath();
logger.info("fileMetadataExtractable2: file is on S3. Downloaded and saved to temp path: " + dataFileLocation);
}
} catch (IOException ex) {
logger.info("fileMetadataExtractable2, could not use storageIO for data file id " + dataFile.getId() + ". Exception: " + ex);
}
}
if (dataFileLocation != null) {
try ( NetcdfFile netcdfFile = NetcdfFiles.open(dataFileLocation)) {
logger.info("fileMetadataExtractable2: trying to open " + dataFileLocation);
if (netcdfFile != null) {
logger.info("fileMetadataExtractable2: returning true");
extractable = true;
} else {
logger.info("NetcdfFiles.open() could not open file id " + dataFile.getId() + " (null returned).");
}
} catch (IOException ex) {
logger.info("NetcdfFiles.open() could not open file id " + dataFile.getId() + ". Exception caught: " + ex);
}
} else {
logger.info("dataFileLocation is null for file id " + dataFile.getId() + ". Can't extract NcML.");
logger.info("fileMetadataExtractableFromNetcdf dataFileIn: " + dataFile + ". tempLocationPath: " + tempLocationPath + ". contentType: " + dataFile.getContentType());
if (dataFile.getContentType() != null
&& (dataFile.getContentType().equals(FileUtil.MIME_TYPE_NETCDF)
|| dataFile.getContentType().equals(FileUtil.MIME_TYPE_XNETCDF)
|| dataFile.getContentType().equals(FileUtil.MIME_TYPE_HDF5))) {
return true;
}
return extractable;
return false;
}

/*
Expand Down
4 changes: 4 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 @@ -182,6 +182,10 @@ public class FileUtil implements java.io.Serializable {
//Todo - this is the same as MIME_TYPE_TSV_ALT
public static final String MIME_TYPE_INGESTED_FILE = "text/tab-separated-values";

public static final String MIME_TYPE_NETCDF = "application/netcdf";
public static final String MIME_TYPE_XNETCDF = "application/x-netcdf";
public static final String MIME_TYPE_HDF5 = "application/x-hdf5";

// File type "thumbnail classes" tags:

public static final String FILE_THUMBNAIL_CLASS_AUDIO = "audio";
Expand Down

0 comments on commit 505e8f2

Please sign in to comment.