Skip to content

Commit

Permalink
fix: thumbnail generation for URI string containing spaces (#6698)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
修复文章封面图链接包含空格时主题端会因为生成缩略图错误而无法访问的问题

这是由于 URI string 中包含空格无法创建 URI 对象,目前将忽略这种非法参数,如果生成失败则直接返回原始 URI string

#### Which issue(s) this PR fixes:
Fixes #6690

#### Does this PR introduce a user-facing change?
```release-note
修复文章封面图链接包含空格时主题端会因为生成缩略图错误而无法访问的问题
```
  • Loading branch information
guqing authored Sep 28, 2024
1 parent 3fe1afb commit a1fcd51
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

import java.net.URI;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import run.halo.app.core.attachment.ThumbnailService;
import run.halo.app.core.attachment.ThumbnailSize;
import run.halo.app.theme.finders.Finder;
import run.halo.app.theme.finders.ThumbnailFinder;

@Slf4j
@Finder("thumbnail")
@RequiredArgsConstructor
public class ThumbnailFinderImpl implements ThumbnailFinder {
private final ThumbnailService thumbnailService;

@Override
public Mono<String> gen(String uriStr, String size) {
return thumbnailService.generate(URI.create(uriStr), ThumbnailSize.fromName(size))
return Mono.fromSupplier(() -> URI.create(uriStr))
.flatMap(uri -> thumbnailService.generate(uri, ThumbnailSize.fromName(size)))
.map(URI::toString)
.onErrorResume(Throwable.class, e -> {
log.debug("Failed to generate thumbnail for [{}], error: [{}]", uriStr,
e.getMessage());
return Mono.just(uriStr);
})
.defaultIfEmpty(uriStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package run.halo.app.theme.finders.impl;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.net.URI;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import run.halo.app.core.attachment.ThumbnailService;

/**
* Tests for {@link ThumbnailFinderImpl}.
*
* @author guqing
* @since 2.20.0
*/
@ExtendWith(MockitoExtension.class)
class ThumbnailFinderImplTest {

@Mock
ThumbnailService thumbnailService;

@InjectMocks
ThumbnailFinderImpl thumbnailFinder;

@Test
void shouldNotGenWhenUriIsInvalid() {
thumbnailFinder.gen("invalid uri", "l")
.as(StepVerifier::create)
.expectNext("invalid uri")
.verifyComplete();

verify(thumbnailService, times(0)).generate(any(), any());
}

@Test
void shouldGenWhenUriIsValid() {
when(thumbnailService.generate(any(), any()))
.thenReturn(Mono.just(URI.create("/test-thumb.jpg")));
thumbnailFinder.gen("/test.jpg", "l")
.as(StepVerifier::create)
.expectNext("/test-thumb.jpg")
.verifyComplete();

verify(thumbnailService).generate(any(), any());
}
}

0 comments on commit a1fcd51

Please sign in to comment.