Skip to content

Commit

Permalink
extra code in the redetect type command, to read non-local files (#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
landreev committed Jun 3, 2019
1 parent c3ee340 commit edcfad3
Showing 1 changed file with 35 additions and 8 deletions.
Expand Up @@ -3,7 +3,7 @@
import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.dataaccess.DataAccess;
import edu.harvard.iq.dataverse.dataaccess.StorageIO;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
Expand All @@ -14,8 +14,10 @@
import edu.harvard.iq.dataverse.util.EjbUtil;
import edu.harvard.iq.dataverse.util.FileTypeDetection;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.logging.Logger;
import javax.ejb.EJBException;

Expand All @@ -36,17 +38,42 @@ public RedetectFileTypeCommand(DataverseRequest dataveseRequest, DataFile dataFi
@Override
public DataFile execute(CommandContext ctxt) throws CommandException {
DataFile filetoReturn = null;
Path path;
File tempFile = null;
File localFile;


try {
// FIXME: Get this working with S3 and Swift.
path = DataAccess.getStorageIO(fileToRedetect).getFileSystemPath();
logger.fine("path: " + path);
File file = path.toFile();
String newlyDetectedContentType = FileTypeDetection.determineFileType(file);
StorageIO<DataFile> storageIO;

storageIO = fileToRedetect.getStorageIO();
storageIO.open();

if (storageIO.isLocalFile()) {
localFile = storageIO.getFileSystemPath().toFile();
} else {
// Need to create a temporary local file:

ReadableByteChannel targetFileChannel = (ReadableByteChannel) storageIO.getReadChannel();
tempFile = File.createTempFile("tempFileTypeCheck", ".tmp");
FileChannel tempFileChannel = new FileOutputStream(tempFile).getChannel();
tempFileChannel.transferFrom(targetFileChannel, 0, storageIO.getSize());

localFile = tempFile;
}

logger.fine("target file: " + localFile);
String newlyDetectedContentType = FileTypeDetection.determineFileType(localFile);
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);
} finally {
// If we had to create a temp file, delete it now:
if (tempFile != null) {
tempFile.delete();
}
}


filetoReturn = fileToRedetect;
if (!dryRun) {
try {
Expand Down

0 comments on commit edcfad3

Please sign in to comment.