Prevent creating files with illegal characters in name (#7)
This commit is contained in:
parent
5ac2e219c4
commit
6aeb8165f6
2 changed files with 37 additions and 2 deletions
|
@ -17,19 +17,22 @@ public class AttachmentDirectoryImpl implements AttachmentDirectory {
|
|||
private static final Logger LOG =
|
||||
Logger.getLogger(
|
||||
AttachmentDirectoryImpl.class.getName());
|
||||
private static final String ILLEGAL_CHARS = "[\\\\/:*?\"<>|]";
|
||||
|
||||
private Path dir;
|
||||
private List<File> toDelete = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
void init() throws IOException {
|
||||
public void init() throws IOException {
|
||||
dir = Files.createTempDirectory("attachments");
|
||||
LOG.info("Attachments directory: " + dir);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File createFile(File fileName) {
|
||||
File file = dir.resolve(fileName.getName()).toFile();
|
||||
String cleanFilename = fileName.getName()
|
||||
.replaceAll(ILLEGAL_CHARS, "");
|
||||
File file = dir.resolve(cleanFilename).toFile();
|
||||
LOG.info("Created attachment: " + file);
|
||||
toDelete.add(file);
|
||||
return file;
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package net.kemitix.trello;
|
||||
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AttachmentDirectoryImplTest
|
||||
implements WithAssertions {
|
||||
|
||||
AttachmentDirectoryImpl attachmentDirectory =
|
||||
new AttachmentDirectoryImpl();
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws IOException {
|
||||
attachmentDirectory.init();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Creates safe filenames")
|
||||
public void createsSafeFilenames() {
|
||||
//given
|
||||
String filename = "x\\y";
|
||||
//when
|
||||
File result = attachmentDirectory.createFile(new File(filename));
|
||||
//then
|
||||
assertThat(result.getName()).isEqualTo("xy");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue