Skip to content

Commit

Permalink
Merge pull request #6774 from IQSS/2734-preserve-orig-filename
Browse files Browse the repository at this point in the history
2734 preserve orig filename
  • Loading branch information
kcondon authored Apr 2, 2020
2 parents 3aede7e + 0856d47 commit d631b05
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 20 deletions.
25 changes: 25 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/DataFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.FileUtil;
import edu.harvard.iq.dataverse.util.ShapefileHandler;
import edu.harvard.iq.dataverse.util.StringUtil;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
Expand Down Expand Up @@ -398,6 +399,30 @@ public Long getOriginalFileSize() {
}
return null;
}

public String getOriginalFileName() {
if (isTabularData()) {
DataTable dataTable = getDataTable();
if (dataTable != null) {
return dataTable.getOriginalFileName() != null ? dataTable.getOriginalFileName()
: getDerivedOriginalFileName();
}
}
return null;
}


private String getDerivedOriginalFileName() {
FileMetadata fm = getFileMetadata();
String filename = fm.getLabel();
String originalExtension = FileUtil.generateOriginalExtension(getOriginalFileFormat());
String extensionToRemove = StringUtil.substringIncludingLast(filename, ".");
if (StringUtil.nonEmpty(extensionToRemove)) {
return filename.replaceAll(extensionToRemove + "$", originalExtension);
} else{
return filename + originalExtension ;
}
}

@Override
public boolean isAncestorOf( DvObject other ) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/DataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public DataTable() {

private Long originalFileSize;

/**
* originalFileName: the file name upon upload/ingest
*/
@Column( nullable = true )
private String originalFileName;

/*
* Getter and Setter methods:
*/
Expand Down Expand Up @@ -194,6 +200,14 @@ public String getOriginalFormatVersion() {
public void setOriginalFormatVersion(String originalFormatVersion) {
this.originalFormatVersion = originalFormatVersion;
}

public String getOriginalFileName() {
return originalFileName;
}

public void setOriginalFileName(String originalFileName) {
this.originalFileName = originalFileName;
}

/*
* Custom overrides for hashCode(), equals() and toString() methods:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,14 @@ public static StorageIO<DataFile> retreive(StorageIO<DataFile> storageIO) {
inputStreamIO.setMimeType("application/x-unknown");
}

String fileName = storageIO.getFileName();
if (fileName != null) {
if (originalMimeType != null) {
String origFileExtension = generateOriginalExtension(originalMimeType);
inputStreamIO.setFileName(fileName.replaceAll(".tab$", origFileExtension));
} else {
inputStreamIO.setFileName(fileName.replaceAll(".tab$", ""));
}
}
inputStreamIO.setFileName(dataFile.getOriginalFileName());

return inputStreamIO;

}

// TODO:
// do what the comment below says - move this code into the file util,
// or something like that!
// -- L.A. 4.0 beta15
// Shouldn't be here; should be part of the DataFileFormatType, or
// something like that...

/*
DataFile.getOriginalFileName() method replaces this code
private static String generateOriginalExtension(String fileType) {
if (fileType.equalsIgnoreCase("application/x-spss-sav")) {
Expand All @@ -125,5 +112,5 @@ private static String generateOriginalExtension(String fileType) {
}
logger.severe(fileType + " does not have an associated file extension");
return "";
}
} */
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,23 @@ protected void executeImpl(CommandContext ctxt) throws CommandException {
// Modify the file name - which is stored in FileMetadata, and there
// could be more than one:

String originalExtension = FileUtil.generateOriginalExtension(originalFileFormat);

// String originalExtension = FileUtil.generateOriginalExtension(originalFileFormat);
String originalFileName = uningest.getOriginalFileName();
for (FileMetadata fm : uningest.getFileMetadatas()) {

fm.setLabel(originalFileName);
ctxt.em().merge(fm);

/*
getOriginalFileName method replaces this code
String filename = fm.getLabel();
String extensionToRemove = StringUtil.substringIncludingLast(filename, ".");
if (StringUtil.nonEmpty(extensionToRemove)) {
String newFileName = filename.replace(extensionToRemove, originalExtension);
fm.setLabel(newFileName);
ctxt.em().merge(fm);
}
*/

DatasetVersion dv = fm.getDatasetVersion();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ public boolean ingestAsTabular(Long datafile_id) {

dataFile.setDataTable(tabDataIngest.getDataTable());
tabDataIngest.getDataTable().setDataFile(dataFile);

tabDataIngest.getDataTable().setOriginalFileName(originalFileName);

try {
produceSummaryStatistics(dataFile, tabFile);
produceFrequencyStatistics(dataFile, tabFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public static JsonObjectBuilder json(DataFile df, FileMetadata fileMetadata) {
.add("originalFileFormat", df.getOriginalFileFormat())
.add("originalFormatLabel", df.getOriginalFormatLabel())
.add ("originalFileSize", df.getOriginalFileSize())
.add("originalFileName", df.getOriginalFileName())
.add("UNF", df.getUnf())
//---------------------------------------------
// For file replace: rootDataFileId, previousDataFileId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

ALTER TABLE datatable ADD COLUMN IF NOT EXISTS originalfilename character varying(255);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.DataFileCategory;
import edu.harvard.iq.dataverse.DataFileTag;
import edu.harvard.iq.dataverse.DataTable;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.DatasetField;
import edu.harvard.iq.dataverse.DatasetFieldCompoundValue;
Expand Down Expand Up @@ -154,6 +155,9 @@ public void testGetFileCategories() {
tag.setTypeByLabel("Survey");
dataFileTags.add(tag);
dataFile.setTags(dataFileTags);
DataTable dt = new DataTable();
dataFile.setDataTable(dt);
dataFile.getDataTable().setOriginalFileName("50by1000.dta");
fmd.setDatasetVersion(dsVersion);
fmd.setDataFile(dataFile);
List<DataFileCategory> fileCategories = new ArrayList<>();
Expand All @@ -171,6 +175,7 @@ public void testGetFileCategories() {
assertEquals("", jsonObject.getJsonObject("dataFile").getString("filename"));
assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("filesize"));
assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("rootDataFileId"));
assertEquals("50by1000.dta", jsonObject.getJsonObject("dataFile").getString("originalFileName"));
assertEquals("Survey", jsonObject.getJsonObject("dataFile").getJsonArray("tabularTags").getString(0));
}

Expand Down

0 comments on commit d631b05

Please sign in to comment.