Skip to content

Commit

Permalink
centralize duplicate file check with related methods #6574
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Apr 30, 2020
1 parent 15d18dd commit c3e9275
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/main/java/edu/harvard/iq/dataverse/api/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import edu.harvard.iq.dataverse.export.ExportService;
import edu.harvard.iq.dataverse.ingest.IngestRequest;
import edu.harvard.iq.dataverse.ingest.IngestServiceBean;
import edu.harvard.iq.dataverse.ingest.IngestUtil;
import edu.harvard.iq.dataverse.makedatacount.MakeDataCountLoggingServiceBean;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.BundleUtil;
Expand Down Expand Up @@ -369,7 +370,6 @@ public Response updateFileMetadata(@FormDataParam("jsonData") String jsonData,
//the updated fileMetadata is not populated to the DataFile object where its easily accessible.
//Due to this we have to find the FileMetadata inside the DatasetVersion by comparing files info.
List<FileMetadata> fmdList = editVersion.getFileMetadatas();
List<String> filePathsAndNames = new ArrayList<>();
for(FileMetadata testFmd : fmdList) {
DataFile daf = testFmd.getDataFile();
// Not sure I understand why we are comparing the checksum values here,
Expand All @@ -382,11 +382,6 @@ public Response updateFileMetadata(@FormDataParam("jsonData") String jsonData,
&& daf.getChecksumValue().equals(df.getChecksumValue())) {
upFmd = testFmd;
}
String path = "";
if (testFmd.getDirectoryLabel() != null) {
path = testFmd.getDirectoryLabel() + "/";
}
filePathsAndNames.add(path + testFmd.getLabel());
}

if (upFmd == null){
Expand All @@ -397,21 +392,16 @@ public Response updateFileMetadata(@FormDataParam("jsonData") String jsonData,
javax.json.JsonObject jsonObject = jsonReader.readObject();
String label = jsonObject.getString("label", null);
String directoryLabel = jsonObject.getString("directoryLabel", null);
if (label != null || directoryLabel != null) {
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
if (label == null) {
label = df.getFileMetadata().getLabel();
}
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
if (label == null) {
label = df.getFileMetadata().getLabel();
}
if (IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) {
String incomingPathPlusFileName = path + label;
logger.fine(filePathsAndNames.toString());
logger.fine("incomingPathName: " + incomingPathPlusFileName);
if (filePathsAndNames.contains(incomingPathPlusFileName)) {
// TODO: Move this English to Bundle.properties.
return error(BAD_REQUEST, "Filename already exists at " + incomingPathPlusFileName);
}
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("files.api.metadata.update.duplicateFile", Arrays.asList(incomingPathPlusFileName)));
}

optionalFileParams.addOptionalParams(upFmd);
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/ingest/IngestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,47 @@ public static String duplicateFilenameCheck(FileMetadata fileMetadata, Set<Strin
return fileName;
}

/**
* Given a new proposed label or directoryLabel for a file, check against
* existing files if a duplicate directoryLabel/label combination would be
* created.
*
* @param label The new label (filename) that is being proposed. Can be
* null.
* @param directoryLabel The new directoryLabel (file path) that is being
* proposed. Can be null.
* @param fileMetadatas The list fileMetadatas to be compared against,
* probably from a draft.
* @param dataFile The file that is being updated with a new name or path.
* @return true if there is a conflict, false otherwise.
*/
public static boolean conflictsWithExistingFilenames(String label, String directoryLabel, List<FileMetadata> fileMetadatas, DataFile dataFile) {
List<String> filePathsAndNames = new ArrayList<>();
for (FileMetadata fileMetadata : fileMetadatas) {
String path = "";
if (fileMetadata.getDirectoryLabel() != null) {
path = fileMetadata.getDirectoryLabel() + "/";
}
filePathsAndNames.add(path + fileMetadata.getLabel());
}
if (label != null || directoryLabel != null) {
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
if (label == null) {
label = dataFile.getFileMetadata().getLabel();
}
String incomingPathPlusFileName = path + label;
logger.fine(filePathsAndNames.toString());
logger.fine("incomingPathName: " + incomingPathPlusFileName);
if (filePathsAndNames.contains(incomingPathPlusFileName)) {
return true;
}
}
return false;
}

// This method is called on a single file, when we need to modify the name
// of an already ingested/persisted datafile. For ex., when we have converted
// a file to tabular data, and want to update the extension accordingly.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/propertyFiles/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,9 @@ admin.api.deleteUser.failure.groupMember=the user is a member of Explicit Group(
admin.api.deleteUser.failure.pendingRequests=the user has pending File Access Request(s)
admin.api.deleteUser.success=Authenticated User {0} deleted.

#Files.java
files.api.metadata.update.duplicateFile=Filename already exists at {0}

#Datasets.java
datasets.api.updatePIDMetadata.failure.dataset.must.be.released=Modify Registration Metadata must be run on a published dataset.
datasets.api.updatePIDMetadata.auth.mustBeSuperUser=Forbidden. You must be a superuser.
Expand Down

0 comments on commit c3e9275

Please sign in to comment.