优化
This commit is contained in:
@@ -101,12 +101,18 @@ public class LocalQualificationStorage {
|
||||
}
|
||||
|
||||
public Resource load(long tenantId, String fileName) {
|
||||
if (!fileName.matches("[0-9a-fA-F-]{36}\\.(jpg|png|pdf)")) throw new BizException("Invalid file name");
|
||||
if (fileName == null || !fileName.matches("[0-9a-fA-F-]{36}\\.(jpg|png|pdf)")) {
|
||||
throw fileNotFound();
|
||||
}
|
||||
Path directory = tenantDirectory(tenantId);
|
||||
Path target = directory.resolve(fileName).normalize();
|
||||
if (!target.startsWith(directory) || !Files.isRegularFile(target)) throw new BizException("File not found");
|
||||
if (!target.startsWith(directory) || !Files.isRegularFile(target)) throw fileNotFound();
|
||||
try { return new UrlResource(target.toUri()); }
|
||||
catch (Exception exception) { throw new BizException("File not found"); }
|
||||
catch (Exception exception) { throw fileNotFound(); }
|
||||
}
|
||||
|
||||
private BizException fileNotFound() {
|
||||
return new BizException(ResultCode.NOT_FOUND, "File not found");
|
||||
}
|
||||
|
||||
public void delete(StoredFile file) {
|
||||
|
||||
@@ -60,7 +60,10 @@ class LocalQualificationStorageTest {
|
||||
assertTrue(Files.isRegularFile(stored.path()));
|
||||
assertEquals(stored.path().toAbsolutePath(),
|
||||
storage.load(10001L, stored.fileName()).getFile().toPath().toAbsolutePath());
|
||||
assertThrows(BizException.class, () -> storage.load(10002L, stored.fileName()));
|
||||
BizException crossTenant = assertThrows(BizException.class,
|
||||
() -> storage.load(10002L, stored.fileName()));
|
||||
assertEquals(ResultCode.NOT_FOUND, crossTenant.getResultCode());
|
||||
assertEquals("File not found", crossTenant.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,9 +114,22 @@ class LocalQualificationStorageTest {
|
||||
|
||||
@Test
|
||||
void legacySeedPathCannotBePretendedToBeAStoredFile() {
|
||||
|
||||
assertThrows(BizException.class,
|
||||
BizException exception = assertThrows(BizException.class,
|
||||
() -> storage.load(10001L, "demo-business-license.jpg"));
|
||||
|
||||
assertEquals(ResultCode.NOT_FOUND, exception.getResultCode());
|
||||
assertEquals("File not found", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingQualificationReturnsNotFoundWithoutRevealingOwnership() {
|
||||
storage.initialize();
|
||||
|
||||
BizException exception = assertThrows(BizException.class,
|
||||
() -> storage.load(10001L, "00000000-0000-0000-0000-000000000000.pdf"));
|
||||
|
||||
assertEquals(ResultCode.NOT_FOUND, exception.getResultCode());
|
||||
assertEquals("File not found", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user