Prevent non-ascii characters in attachment filenames (#10)

This commit is contained in:
Paul Campbell 2021-02-03 18:48:35 +00:00 committed by GitHub
parent 4785915537
commit c9f904f83f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View file

@ -12,7 +12,7 @@
</parent> </parent>
<artifactId>kemitix-trello</artifactId> <artifactId>kemitix-trello</artifactId>
<version>1.0.2</version> <version>1.0.3</version>
<properties> <properties>
<tiles-maven-plugin.version>2.18</tiles-maven-plugin.version> <tiles-maven-plugin.version>2.18</tiles-maven-plugin.version>

View file

@ -8,7 +8,10 @@ import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -45,7 +48,12 @@ public class TrelloAttachment implements Attachment {
@Override @Override
public File getFilename() { public File getFilename() {
return new File(String.format("%4s - %s.%s", return new File(String.format("%4s - %s.%s",
id, card.getName(), extension())); id, safeCardName(), extension()));
}
private String safeCardName() {
String normalize = Normalizer.normalize(card.getName(), Normalizer.Form.NFD);
return normalize.replaceAll("[^\\p{ASCII}]", "");
} }
private String extension() { private String extension() {

View file

@ -27,4 +27,23 @@ public class TrelloAttachmentTest
//then //then
assertThat(filename.getName()).endsWith(".extension"); assertThat(filename.getName()).endsWith(".extension");
} }
@Test
void convertsUnsafeFilename() {
//given
Attachment attachment = new Attachment();
attachment.setUrl("file.txt");
Card card = new Card();
card.setIdShort("123");
card.setName("Card name Á");
AttachmentDirectory dir = new AttachmentDirectoryImpl();
var trelloAttachment = TrelloAttachment.create(
attachment, card, dir
);
//when
File filename = trelloAttachment.getFilename();
//then
assertThat(filename.getName()).doesNotContain("Á");
assertThat(filename.getName()).isEqualTo(" 123 - Card name A.txt");
}
} }