Skip to content

Commit

Permalink
new physical file delete framework, applied to bulk draft file delete…
Browse files Browse the repository at this point in the history
…s on the

dataset page. (#5535)
  • Loading branch information
landreev committed Feb 27, 2019
1 parent ac17050 commit 507918c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/DataFileServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -1591,4 +1591,17 @@ public void finalizeFileDelete(Long dataFileId, String storageLocation) throws I
directStorageAccess.delete();
}

public void finalizeFileDeletes(Map<Long, String> storageLocations) {
storageLocations.keySet().stream().forEach((dataFileId) -> {
String storageLocation = storageLocations.get(dataFileId);

try {
finalizeFileDelete(dataFileId, storageLocation);
} catch (IOException ioex) {
logger.warning("Failed to delete the physical file associated with the deleted datafile id="
+ dataFileId + ", storage location: " + storageLocation);
}
});
}

}
32 changes: 31 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,9 @@ public String save() {

// Use the Create or Update command to save the dataset:
Command<Dataset> cmd;
try {
Map<Long, String> deleteStorageLocations = null;

try {
if (editMode == EditMode.CREATE) {
if ( selectedTemplate != null ) {
if ( isSessionUserAuthenticated() ) {
Expand All @@ -2657,6 +2659,24 @@ public String save() {
}

} else {
if (!filesToBeDeleted.isEmpty()) {
deleteStorageLocations = new HashMap<>();
for (FileMetadata fmd : filesToBeDeleted) {
try {
DataFile dataFile = fmd.getDataFile();
StorageIO<DataFile> storageIO = dataFile.getStorageIO();
storageIO.open();
String storageLocation = storageIO.getStorageLocation();
if (storageLocation != null) {
deleteStorageLocations.put(dataFile.getId(), storageLocation);
}
} catch (IOException ioex) {
// something potentially wrong with the physical file,
// or connection to the physical storage?
// we'll still try to delete the datafile from the database
}
}
}
cmd = new UpdateDatasetVersionCommand(dataset, dvRequestService.getDataverseRequest(), filesToBeDeleted, clone );
((UpdateDatasetVersionCommand) cmd).setValidateLenient(true);
}
Expand Down Expand Up @@ -2687,6 +2707,16 @@ public String save() {
return returnToDraftVersion();
}

// Have we just deleted some draft datafiles (successfully)?
// finalize the physical file deletes:
// (DataFileService will double-check that the datafiles no
// longer exist in the database, before attempting to delete
// the physical files)

if (deleteStorageLocations != null) {
datafileService.finalizeFileDeletes(deleteStorageLocations);
}

if (editMode != null) {
if (editMode.equals(EditMode.CREATE)) {
// We allow users to upload files on Create:
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ public String save() {
// TODO:
// add diagnostics reporting for individual data files that
// we failed to delete.
logger.info("Failed to delete DataFile id=" + dataFileId + " from the database; " + cmde.getMessage());
logger.warning("Failed to delete DataFile id=" + dataFileId + " from the database; " + cmde.getMessage());
}
if (deleteCommandSuccess) {
if (storageLocation != null) {
Expand All @@ -1332,7 +1332,7 @@ public String save() {
try {
datafileService.finalizeFileDelete(dataFileId, storageLocation);
} catch (IOException ioex) {
logger.info("Failed to delete the physical file associated with the deleted datafile id="
logger.warning("Failed to delete the physical file associated with the deleted datafile id="
+ dataFileId + ", storage location: " + storageLocation);
}
}
Expand Down

0 comments on commit 507918c

Please sign in to comment.