Skip to content
Snippets Groups Projects
Commit 2bf23ed5 authored by srosse's avatar srosse
Browse files

OO-3742: fix unzip of a file which already exists in the same folder

parent 335da26b
No related branches found
No related tags found
No related merge requests found
...@@ -48,6 +48,7 @@ import org.olat.core.gui.translator.Translator; ...@@ -48,6 +48,7 @@ import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.id.Roles; import org.olat.core.id.Roles;
import org.olat.core.logging.AssertException; import org.olat.core.logging.AssertException;
import org.olat.core.util.StringHelper;
import org.olat.core.util.ZipUtil; import org.olat.core.util.ZipUtil;
import org.olat.core.util.vfs.Quota; import org.olat.core.util.vfs.Quota;
import org.olat.core.util.vfs.VFSConstants; import org.olat.core.util.vfs.VFSConstants;
...@@ -68,11 +69,12 @@ public class CmdUnzip extends BasicController implements FolderCommand { ...@@ -68,11 +69,12 @@ public class CmdUnzip extends BasicController implements FolderCommand {
super(ureq, wControl); super(ureq, wControl);
} }
@Override
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wContr, Translator trans) { public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wContr, Translator trans) {
this.translator = trans; this.translator = trans;
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath()); FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
VFSContainer currentContainer = folderComponent.getCurrentContainer(); VFSContainer currentContainer = folderComponent.getCurrentContainer();
if (!(currentContainer.canWrite() == VFSConstants.YES)) if (currentContainer.canWrite() != VFSConstants.YES)
throw new AssertException("Cannot unzip to folder. Writing denied."); throw new AssertException("Cannot unzip to folder. Writing denied.");
//check if command is executed on a file containing invalid filenames or paths - checks if the resulting folder has a valid name //check if command is executed on a file containing invalid filenames or paths - checks if the resulting folder has a valid name
...@@ -81,7 +83,7 @@ public class CmdUnzip extends BasicController implements FolderCommand { ...@@ -81,7 +83,7 @@ public class CmdUnzip extends BasicController implements FolderCommand {
return null; return null;
} }
List<String> lockedFiles = new ArrayList<String>(); List<String> lockedFiles = new ArrayList<>();
for (String sItem:selection.getFiles()) { for (String sItem:selection.getFiles()) {
VFSItem vfsItem = currentContainer.resolve(sItem); VFSItem vfsItem = currentContainer.resolve(sItem);
if (vfsItem instanceof VFSLeaf) { if (vfsItem instanceof VFSLeaf) {
...@@ -181,7 +183,19 @@ public class CmdUnzip extends BasicController implements FolderCommand { ...@@ -181,7 +183,19 @@ public class CmdUnzip extends BasicController implements FolderCommand {
VFSContainer zipContainer = currentContainer.createChildContainer(sZipContainer); VFSContainer zipContainer = currentContainer.createChildContainer(sZipContainer);
if (zipContainer == null) { if (zipContainer == null) {
if(versioning) { if(versioning) {
zipContainer =(VFSContainer)currentContainer.resolve(sZipContainer); VFSItem resolvedItem = currentContainer.resolve(sZipContainer);
if(resolvedItem instanceof VFSContainer) {
zipContainer = (VFSContainer)resolvedItem;
} else {
String numberedFilename = findContainerName(currentContainer, sZipContainer);
if(StringHelper.containsNonWhitespace(numberedFilename)) {
zipContainer = currentContainer.createChildContainer(numberedFilename);
}
if(zipContainer == null) {// we try our best
wControl.setError(translator.translate("unzip.alreadyexists", new String[] {sZipContainer}));
return false;
}
}
} else { } else {
// folder already exists... issue warning // folder already exists... issue warning
wControl.setError(translator.translate("unzip.alreadyexists", new String[] {sZipContainer})); wControl.setError(translator.translate("unzip.alreadyexists", new String[] {sZipContainer}));
...@@ -212,6 +226,19 @@ public class CmdUnzip extends BasicController implements FolderCommand { ...@@ -212,6 +226,19 @@ public class CmdUnzip extends BasicController implements FolderCommand {
} }
return true; return true;
} }
private String findContainerName(VFSContainer container, String filename) {
String newName = filename;
VFSItem newFile = container.resolve(newName);
for(int count=1; newFile != null && count < 999 ; count++) {
newName = filename + "_" + count;
newFile = container.resolve(newName);
}
if(newFile == null) {
return newName;
}
return null;
}
@Override @Override
public int getStatus() { public int getStatus() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment