Skip to content

Commit

Permalink
feat(runner): allow chaning mime type and extension of plain text files
Browse files Browse the repository at this point in the history
  • Loading branch information
sabberworm committed Oct 7, 2024
1 parent 71e7074 commit 1f57d62
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/main/java/com/swisscom/aem/tools/impl/file/PlainTextFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -15,6 +16,14 @@ public class PlainTextFile implements File {
@Getter
private final String name;

@Getter
@Setter
private String mimeType = "text/plain;charset=utf-8";

@Getter
@Setter
private String extension = "txt";

@SuppressWarnings("PMD.AvoidStringBufferField")
@Delegate(types = StringBuilder.class)
private final StringBuilder result = new StringBuilder();
Expand All @@ -26,16 +35,6 @@ public void println() {
result.append('\n');
}

@Override
public String getMimeType() {
return "text/plain;charset=utf-8";
}

@Override
public String getExtension() {
return "txt";
}

@Override
public byte[] getContents() {
return result.toString().getBytes(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,24 @@ public void file_plainText() throws HopperException, RepositoryException {
runHandler.getLastFile()
);
}

@Test
public void file_plainTextCustomType() throws HopperException, RepositoryException {
runnerService.builder()
.runHandler(runHandler)
.build(new Script(
Arrays.asList(
new Declare.Config().withDeclarations(Collections.singletonMap("txt", "file:txt('main')")),
new RunScript.Config().withExtension("jexl").withCode("txt.mimeType = 'text/javascript';\ntxt.extension = 'js';\ntxt.append('\"use strict\"')")
),
LogLevel.DEBUG
))
.run(context.resourceResolver().adaptTo(Session.class), true);

assertEquals(
"main.js (text/javascript):\n"
+ "\"use strict\"",
runHandler.getLastFile()
);
}
}

0 comments on commit 1f57d62

Please sign in to comment.