fix: store uploads under relative file directory
parent
c956d06d49
commit
4c15249264
@ -0,0 +1,44 @@
|
|||||||
|
package com.sztzjy.linkCommerce.util.file;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class LocalFileUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createsRelativeFileRootAndUsesFilePrefixedDatePaths() throws IOException {
|
||||||
|
Path relativeRoot = Paths.get("target", "upload-root-" + UUID.randomUUID());
|
||||||
|
try {
|
||||||
|
LocalFileUtil fileUtil = new LocalFileUtil(relativeRoot.toString());
|
||||||
|
|
||||||
|
assertTrue(Files.isDirectory(relativeRoot));
|
||||||
|
String path = fileUtil.getDiskRelativePath("example", "pdf");
|
||||||
|
assertTrue(path.startsWith("file/"));
|
||||||
|
assertTrue(path.endsWith("/example.pdf"));
|
||||||
|
assertEquals(relativeRoot.toAbsolutePath().normalize().resolve(path.substring("file/".length())).toString(),
|
||||||
|
fileUtil.getFullPath(path));
|
||||||
|
assertEquals(relativeRoot.toAbsolutePath().normalize().resolve("2024/1/2/legacy.pdf").toString(),
|
||||||
|
fileUtil.getFullPath("/2024/1/2/legacy.pdf"));
|
||||||
|
} finally {
|
||||||
|
if (Files.exists(relativeRoot)) {
|
||||||
|
try (java.util.stream.Stream<Path> files = Files.walk(relativeRoot)) {
|
||||||
|
files.sorted(java.util.Comparator.reverseOrder()).forEach(path -> {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
} catch (IOException exception) {
|
||||||
|
throw new IllegalStateException(exception);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue