mirror of https://github.com/halo-dev/halo
Fix the problem of generating permalink due to special filename (#2462)
#### What type of PR is this? /kind bug /area core /milestone 2.0 #### What this PR does / why we need it: Encode path of attachment permalink instead of using URI#resolve methods directly. #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/2408 #### Special notes for reviewers Steps to test: 1. Create a file with special name, e.g.: `hello world.txt` 2. Upload the file and see the permalink #### Does this PR introduce a user-facing change? ```release-note None ```pull/2466/head
parent
02cc2fa7be
commit
1da89f04e2
|
@ -71,4 +71,5 @@ application-local.yaml
|
||||||
application-local.properties
|
application-local.properties
|
||||||
|
|
||||||
### Zip file for test
|
### Zip file for test
|
||||||
!src/test/resources/themes/*.zip
|
!src/test/resources/themes/*.zip
|
||||||
|
src/main/resources/console/
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
package run.halo.app.core.extension.reconciler.attachment;
|
package run.halo.app.core.extension.reconciler.attachment;
|
||||||
|
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.pf4j.PluginManager;
|
import org.pf4j.PluginManager;
|
||||||
|
import org.springframework.web.util.UriUtils;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import run.halo.app.core.extension.attachment.Attachment;
|
import run.halo.app.core.extension.attachment.Attachment;
|
||||||
|
@ -63,10 +67,8 @@ public class AttachmentReconciler implements Reconciler<Request> {
|
||||||
var localRelativePath = annotations.get(Constant.LOCAL_REL_PATH_ANNO_KEY);
|
var localRelativePath = annotations.get(Constant.LOCAL_REL_PATH_ANNO_KEY);
|
||||||
if (localRelativePath != null) {
|
if (localRelativePath != null) {
|
||||||
// TODO Add router function here.
|
// TODO Add router function here.
|
||||||
permalink = externalUrl.get()
|
var encodedPath = UriUtils.encodePath("/upload/" + localRelativePath, UTF_8);
|
||||||
.resolve("/upload/" + localRelativePath)
|
permalink = externalUrl.get().resolve(encodedPath).normalize().toString();
|
||||||
.normalize()
|
|
||||||
.toASCIIString();
|
|
||||||
} else {
|
} else {
|
||||||
var externalLink = annotations.get(Constant.EXTERNAL_LINK_ANNO_KEY);
|
var externalLink = annotations.get(Constant.EXTERNAL_LINK_ANNO_KEY);
|
||||||
if (externalLink != null) {
|
if (externalLink != null) {
|
||||||
|
@ -79,19 +81,25 @@ public class AttachmentReconciler implements Reconciler<Request> {
|
||||||
var status = attachment.getStatus();
|
var status = attachment.getStatus();
|
||||||
if (status == null) {
|
if (status == null) {
|
||||||
status = new AttachmentStatus();
|
status = new AttachmentStatus();
|
||||||
|
attachment.setStatus(status);
|
||||||
}
|
}
|
||||||
status.setPermalink(permalink);
|
status.setPermalink(permalink);
|
||||||
|
|
||||||
// update status
|
|
||||||
attachment.setStatus(status);
|
|
||||||
client.update(attachment);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateStatus(request.name(), attachment.getStatus());
|
||||||
});
|
});
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updateStatus(String attachmentName, AttachmentStatus status) {
|
||||||
|
client.fetch(Attachment.class, attachmentName)
|
||||||
|
.filter(attachment -> !Objects.deepEquals(attachment.getStatus(), status))
|
||||||
|
.ifPresent(attachment -> {
|
||||||
|
attachment.setStatus(status);
|
||||||
|
client.update(attachment);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void removeFinalizer(String attachmentName) {
|
void removeFinalizer(String attachmentName) {
|
||||||
client.fetch(Attachment.class, attachmentName).ifPresent(attachment -> {
|
client.fetch(Attachment.class, attachmentName).ifPresent(attachment -> {
|
||||||
var finalizers = attachment.getMetadata().getFinalizers();
|
var finalizers = attachment.getMetadata().getFinalizers();
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package run.halo.app.core.extension.endpoint;
|
|
||||||
|
|
||||||
class AttachmentEndpointTest {
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue