fix(upload): truncate long filenames to safe length while preserving extension for bookdrop and additional files (#2059)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+52
@@ -306,4 +306,56 @@ class FileUploadServiceTest {
|
||||
|
||||
assertDoesNotThrow(() -> service.uploadFile(file, 10L, 3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should truncate long filenames in uploadFileBookDrop")
|
||||
void uploadFileBookDrop_truncatesLongFilename() throws IOException {
|
||||
byte[] content = "data".getBytes();
|
||||
String longName = "A".repeat(300) + ".pdf";
|
||||
MockMultipartFile file = new MockMultipartFile("file", longName, "application/pdf", content);
|
||||
|
||||
service.uploadFileBookDrop(file);
|
||||
|
||||
File[] files = tempDir.toFile().listFiles();
|
||||
assertThat(files).isNotNull();
|
||||
assertThat(files).hasSize(1);
|
||||
String savedName = files[0].getName();
|
||||
|
||||
assertThat(savedName.length()).isLessThan(longName.length());
|
||||
assertThat(savedName).endsWith(".pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should truncate long filenames in uploadAdditionalFile")
|
||||
void uploadAdditionalFile_truncatesLongFilename() {
|
||||
long bookId = 11L;
|
||||
String longName = "B".repeat(300) + ".pdf";
|
||||
MockMultipartFile file = new MockMultipartFile("file", longName, "application/pdf", "payload".getBytes());
|
||||
|
||||
LibraryPathEntity libPath = new LibraryPathEntity();
|
||||
libPath.setId(1L);
|
||||
libPath.setPath(tempDir.toString());
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(bookId);
|
||||
book.setLibraryPath(libPath);
|
||||
book.setFileSubPath(".");
|
||||
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.of(book));
|
||||
when(bookAdditionalFileRepository.save(any(BookAdditionalFileEntity.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||
when(additionalFileMapper.toAdditionalFile(any(BookAdditionalFileEntity.class))).thenReturn(mock(AdditionalFile.class));
|
||||
|
||||
try (MockedStatic<FileFingerprint> fp = mockStatic(FileFingerprint.class)) {
|
||||
fp.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash");
|
||||
|
||||
service.uploadAdditionalFile(bookId, file, AdditionalFileType.ALTERNATIVE_FORMAT, "desc");
|
||||
|
||||
File[] files = tempDir.toFile().listFiles();
|
||||
assertThat(files).isNotNull();
|
||||
assertThat(files).hasSize(1);
|
||||
String savedName = files[0].getName();
|
||||
|
||||
assertThat(savedName.length()).isLessThan(longName.length());
|
||||
assertThat(savedName).endsWith(".pdf");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+62
@@ -821,4 +821,66 @@ class PathPatternResolverTest {
|
||||
MAX_AUTHORS_BYTES, authorsBytes));
|
||||
assertTrue(authorsPart.contains("et al."), "Should add 'et al.' when truncating");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should truncate long filename while preserving extension")
|
||||
void testTruncateFilenameWithExtension_truncatesLongFilename() {
|
||||
String longName = "A".repeat(300);
|
||||
String extension = ".pdf";
|
||||
String filename = longName + extension;
|
||||
|
||||
String result = PathPatternResolver.truncateFilenameWithExtension(filename);
|
||||
|
||||
assertTrue(result.length() < filename.length(), "Filename should be truncated");
|
||||
assertTrue(result.endsWith(extension), "Extension should be preserved");
|
||||
assertTrue(result.getBytes(StandardCharsets.UTF_8).length <= MAX_FILENAME_BYTES,
|
||||
"Result bytes should be <= " + MAX_FILENAME_BYTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should not truncate short filename")
|
||||
void testTruncateFilenameWithExtension_shortFilename() {
|
||||
String filename = "short_filename.pdf";
|
||||
String result = PathPatternResolver.truncateFilenameWithExtension(filename);
|
||||
|
||||
assertEquals(filename, result, "Short filename should not be modified");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should truncate filename without extension if too long")
|
||||
void testTruncateFilenameWithExtension_noExtension() {
|
||||
String longName = "A".repeat(300);
|
||||
String result = PathPatternResolver.truncateFilenameWithExtension(longName);
|
||||
|
||||
assertTrue(result.length() < longName.length(), "Filename should be truncated");
|
||||
assertTrue(result.getBytes(StandardCharsets.UTF_8).length <= MAX_FILENAME_BYTES,
|
||||
"Result bytes should be <= " + MAX_FILENAME_BYTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle long unicode filename with extension")
|
||||
void testTruncateFilenameWithExtension_unicode() {
|
||||
String longName = "测试".repeat(100); // 600 bytes
|
||||
String extension = ".txt";
|
||||
String filename = longName + extension;
|
||||
|
||||
String result = PathPatternResolver.truncateFilenameWithExtension(filename);
|
||||
|
||||
assertTrue(result.length() < filename.length(), "Filename should be truncated");
|
||||
assertTrue(result.endsWith(extension), "Extension should be preserved");
|
||||
assertTrue(result.getBytes(StandardCharsets.UTF_8).length <= MAX_FILENAME_BYTES,
|
||||
"Result bytes should be <= " + MAX_FILENAME_BYTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle hidden files (starting with dot)")
|
||||
void testTruncateFilenameWithExtension_hiddenFile() {
|
||||
String longName = ".config" + "A".repeat(300);
|
||||
|
||||
String result = PathPatternResolver.truncateFilenameWithExtension(longName);
|
||||
|
||||
assertTrue(result.startsWith(".config"), "Should still start with .config (or be treated as filename)");
|
||||
assertTrue(result.getBytes(StandardCharsets.UTF_8).length <= MAX_FILENAME_BYTES,
|
||||
"Result bytes should be <= " + MAX_FILENAME_BYTES);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user