Skip to content

Commit

Permalink
Merge pull request #5620 from effective-webwork/rename-directory
Browse files Browse the repository at this point in the history
Rename directories when changing process title
  • Loading branch information
solth authored May 2, 2023
2 parents 550c225 + 9dd076d commit bbae957
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public interface FileManagementInterface {
* the URI to the resource to rename
* @param newName
* the new name of the resource
* @return true, if successful, false otherwise
* @return URI of renamed resource
*/
URI rename(URI uri, String newName) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public URI rename(URI uri, String newName) throws IOException {
return null;
}

if (isDirectory(uri)) {
return renameDirectory(uri, newName);
}

String substring = uri.toString().substring(0, uri.toString().lastIndexOf('/') + 1);
if (newName.contains("/")) {
newName = newName.substring(newName.lastIndexOf('/') + 1);
Expand All @@ -179,6 +183,22 @@ public URI rename(URI uri, String newName) throws IOException {
return performRename(mappedFileURI, mappedNewFileURI);
}

private URI renameDirectory(URI directoryPath, String newDirectoryName) throws IOException {
File newDirectory = new File(KitodoConfig.getKitodoDataDirectory() + newDirectoryName);
File oldDirectory = new File(KitodoConfig.getKitodoDataDirectory() + directoryPath);

if (!oldDirectory.equals(newDirectory)) {
boolean success = oldDirectory.renameTo(newDirectory);
if (success) {
return newDirectory.toURI();
} else {
throw new IOException("Unable to rename directory '" + directoryPath + "'!");
}
} else {
return newDirectory.toURI();
}
}

private URI performRename(URI mappedFileURI, URI mappedNewFileURI) throws IOException {
File fileToRename = new File(mappedFileURI);
File renamedFile = new File(mappedNewFileURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

public class FileManagementTest {

private static FileManagement fileManagement = new FileManagement();
private static final FileManagement fileManagement = new FileManagement();

private static final String FILE_TEST = "fileTest";
private static final String DIRECTORY_SIZE = "directorySize";
Expand Down Expand Up @@ -150,6 +150,20 @@ public void shouldRenameFile() throws Exception {
assertTrue(fileManagement.fileExist(newUri));
}

@Test
public void shouldSkipRenamingDirectory() throws Exception {
String directoryName = "testDir";
URI resource = fileManagement.create(URI.create(""), directoryName, false);
assumeTrue(fileManagement.isDirectory(resource));
URI expectedUri = new File(KitodoConfig.getKitodoDataDirectory() + resource).toURI();
assertEquals("Renaming directory to the identical name should return identical URI", expectedUri,
fileManagement.rename(resource, directoryName));
String directoryWithTrailingSlash = directoryName + "/";
assertEquals("Renaming directory to the identical name with trailing slash should return identical URI",
expectedUri, fileManagement.rename(resource, directoryWithTrailingSlash));
fileManagement.delete(resource);
}

@Test
public void shouldCopyDirectory() throws Exception {
URI resource = fileManagement.create(URI.create(FILE_TEST), "toCopy", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ private void renameDirectories(URI directory) throws IOException {
List<URI> subDirs = fileService.getSubUris(directory);
for (URI imageDir : subDirs) {
if (fileService.isDirectory(imageDir)) {
fileService.renameFile(imageDir,
fileService.getFileName(imageDir).replace(process.getTitle(), newProcessTitle));
fileService.renameFile(imageDir, imageDir.toString().replace(process.getTitle(), newProcessTitle));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public InputStream readMetadataFile(Process process, boolean forIndexingAll) thr
* @param newFileName
* New file name / destination
* @throws IOException
* is thrown if the rename fails permanently
* is thrown if renaming the file fails permanently
*/
public URI renameFile(URI fileUri, String newFileName) throws IOException {
return fileManagementModule.rename(fileUri, newFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@

public class FileServiceTest {

private static FileService fileService = new FileService();
private static final FileService fileService = new FileService();
private static final Logger logger = LogManager.getLogger(FileServiceTest.class);
private static final String OLD_DIRECTORY_NAME = "oldDirectoryName";
private static final String NEW_DIRECTORY_NAME = "newDirectoryName";

@BeforeClass
public static void setUp() throws IOException {
Expand Down Expand Up @@ -144,6 +146,16 @@ public void testRenameFile() throws IOException {
assertTrue(fileService.fileExist(newUri));
}

@Test
public void testRenameDirectory() throws Exception {
URI oldDirUri = fileService.createDirectory(URI.create(""), OLD_DIRECTORY_NAME);
assertTrue(fileService.isDirectory(oldDirUri));
URI newDirUri = fileService.renameFile(oldDirUri, NEW_DIRECTORY_NAME);
assertTrue(fileService.isDirectory(newDirUri));
assertFalse(fileService.isDirectory(oldDirUri));
fileService.delete(newDirUri);
}

/**
* Tests searchForMedia function if a MediaNotFoundException is thrown.
*
Expand Down

0 comments on commit bbae957

Please sign in to comment.