diff --git a/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java b/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java index e891887f59..be5d5bd06e 100644 --- a/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java +++ b/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java @@ -549,15 +549,22 @@ Mono computeIfAbsent(String version, Publisher content) { // double check of the resource .filter(res -> isResourceMatch(res, newFilename)) .switchIfEmpty(Mono.using( - () -> tempDir.resolve(newFilename), + () -> { + if (!Files.exists(tempDir)) { + Files.createDirectories(tempDir); + } + return tempDir.resolve(newFilename); + }, path -> DataBufferUtils.write(content, path, CREATE, TRUNCATE_EXISTING) .then(Mono.fromSupplier( () -> new FileSystemResource(path) )), path -> { - // clean up old resource - cleanUp(this.resource); + if (shouldCleanUp(path)) { + // clean up old resource + cleanUp(this.resource); + } }) .subscribeOn(scheduler) .doOnNext(newResource -> this.resource = newResource) @@ -579,6 +586,18 @@ Mono computeIfAbsent(String version, Publisher content) { }); } + private boolean shouldCleanUp(Path newPath) { + if (this.resource == null || !this.resource.exists()) { + return false; + } + try { + var oldPath = this.resource.getFile().toPath(); + return !oldPath.equals(newPath); + } catch (IOException e) { + return false; + } + } + private static void cleanUp(Resource resource) { if (resource instanceof WritableResource wr && wr.isWritable() diff --git a/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java b/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java index 8d34e55d65..6918aba6e9 100644 --- a/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java +++ b/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java @@ -46,6 +46,7 @@ import org.pf4j.PluginDescriptor; import org.pf4j.PluginWrapper; import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.util.FileSystemUtils; import org.springframework.web.server.ServerWebInputException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -391,6 +392,24 @@ void shouldComputeBundleFileIfAbsent() { } }) .verifyComplete(); + + try { + FileSystemUtils.deleteRecursively(tempDir); + } catch (IOException e) { + throw new RuntimeException(e); + } + cache.computeIfAbsent("fake-version", fakeContent) + .as(StepVerifier::create) + .assertNext(resource -> { + try { + assertThat(Files.exists(tempDir)).isTrue(); + assertEquals(tempDir.resolve("different-version.js"), + resource.getFile().toPath()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + .verifyComplete(); } @Test