Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relativepath placeholder #5440

Merged
merged 5 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private enum MetadataLevel {
private static final Pattern VARIABLE_FINDER_REGEX = Pattern.compile(
"(\\$?)\\((?:(prefs|processid|processtitle|projectid|stepid|stepname)|"
+ "(?:(meta|process|product|template)\\.(?:(firstchild|topstruct)\\.)?([^)]+)|"
+ "(?:(filename|basename))))\\)");
+ "(?:(filename|basename|relativepath))))\\)");

/**
* The map is filled with replacement instructions that are required for
Expand Down Expand Up @@ -392,7 +392,8 @@ public boolean containsFiles(String stringWithVariables) {
if (Objects.isNull(stringWithVariables)) {
return false;
}
return stringWithVariables.contains("(filename)") | stringWithVariables.contains("(basename)");
return stringWithVariables.contains("(filename)") | stringWithVariables.contains("(basename)")
| stringWithVariables.contains("(relativepath)");
}

/**
Expand All @@ -404,6 +405,8 @@ private String determineReplacementForFilePlaceholder(Matcher variableFinder, St
return variableFinder.group(1) + FilenameUtils.getName(filename);
case "basename":
return variableFinder.group(1) + FilenameUtils.getBaseName(filename);
case "relativepath":
return variableFinder.group(1) + filename;
default:
logger.warn("Cannot replace \"{}\": no such case defined in switch", variableFinder.group());
return variableFinder.group();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.kitodo.api.MdSec;
import org.kitodo.api.MetadataEntry;
Expand Down Expand Up @@ -141,12 +142,11 @@ private void replaceFLocatForExport(Workpiece workpiece, Process process)
for (Entry<MediaVariant, URI> mediaFileForMediaVariant : physicalDivision.getMediaFiles().entrySet()) {
for (Folder folder : folders) {
if (folder.getFileGroup().equals(mediaFileForMediaVariant.getKey().getUse())) {
int lastSeparator = mediaFileForMediaVariant.getValue().toString().lastIndexOf('/');
String lastSegment = mediaFileForMediaVariant.getValue().toString()
.substring(lastSeparator + 1);
String mediaFileWithPath = mediaFileForMediaVariant.getValue().toString();
String mediaFilename = FilenameUtils.getName(mediaFileWithPath);
String mediaFile = variableReplacer.containsFiles(folder.getUrlStructure())
? variableReplacer.replaceWithFilename(folder.getUrlStructure(), lastSegment)
: variableReplacer.replace(folder.getUrlStructure() + lastSegment);
? variableReplacer.replaceWithFilename(folder.getUrlStructure(), mediaFileWithPath)
: variableReplacer.replace(folder.getUrlStructure() + mediaFilename);
mediaFileForMediaVariant.setValue(new URI(mediaFile));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ public void shouldReplaceBasename() {

assertEquals("String was replaced incorrectly!", expected, replaced);
}

@Test
public void shouldReplaceRelativePath() {
VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null);

String testFilenameWithPath = "src/testFile.txt";

String replaced = variableReplacer.replaceWithFilename("-filename (relativepath) -hardcoded test",
testFilenameWithPath);
String expected = "-filename " + testFilenameWithPath + " -hardcoded test";

assertEquals("String was replaced incorrectly!", expected, replaced);
}

@Test
public void shouldContainFile() {
Expand Down