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
John Niang 2022-09-24 12:50:15 +08:00 committed by GitHub
parent 02cc2fa7be
commit 1da89f04e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 15 deletions

3
.gitignore vendored
View File

@ -71,4 +71,5 @@ application-local.yaml
application-local.properties
### Zip file for test
!src/test/resources/themes/*.zip
!src/test/resources/themes/*.zip
src/main/resources/console/

View File

@ -1,9 +1,13 @@
package run.halo.app.core.extension.reconciler.attachment;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginManager;
import org.springframework.web.util.UriUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
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);
if (localRelativePath != null) {
// TODO Add router function here.
permalink = externalUrl.get()
.resolve("/upload/" + localRelativePath)
.normalize()
.toASCIIString();
var encodedPath = UriUtils.encodePath("/upload/" + localRelativePath, UTF_8);
permalink = externalUrl.get().resolve(encodedPath).normalize().toString();
} else {
var externalLink = annotations.get(Constant.EXTERNAL_LINK_ANNO_KEY);
if (externalLink != null) {
@ -79,19 +81,25 @@ public class AttachmentReconciler implements Reconciler<Request> {
var status = attachment.getStatus();
if (status == null) {
status = new AttachmentStatus();
attachment.setStatus(status);
}
status.setPermalink(permalink);
// update status
attachment.setStatus(status);
client.update(attachment);
}
}
updateStatus(request.name(), attachment.getStatus());
});
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) {
client.fetch(Attachment.class, attachmentName).ifPresent(attachment -> {
var finalizers = attachment.getMetadata().getFinalizers();

View File

@ -1,5 +0,0 @@
package run.halo.app.core.extension.endpoint;
class AttachmentEndpointTest {
}