Files
plugin-s3/src/main/java/run/halo/s3os/S3UnlinkServiceImpl.java
longjuan 8ff4acba6e perf: improve more friendly exception messages (#113)
```release-note
友好地提示异常信息
```
fixes https://github.com/halo-dev/plugin-s3/issues/105

验证方法:
1. ak/sk乱输,发生接收到403状态码(接收错误状态码)
2. endpoint网址改成不存在的,如.com改成.comaaa(未知主机)
3. endpoint端口改成没监听的(超时)
2024-01-16 14:14:12 +00:00

37 lines
1.4 KiB
Java

package run.halo.s3os;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.attachment.Attachment;
import run.halo.app.core.extension.attachment.Policy;
import run.halo.app.extension.ReactiveExtensionClient;
@Service
@RequiredArgsConstructor
@Slf4j
public class S3UnlinkServiceImpl implements S3UnlinkService {
private final ReactiveExtensionClient client;
private final S3OsAttachmentHandler handler;
@Override
public Mono<Attachment> unlink(String name) {
return client.get(Attachment.class, name)
.flatMap((attachment) -> client.get(Policy.class, attachment.getSpec().getPolicyName())
.doOnNext((policy) -> {
if (!handler.shouldHandle(policy)) {
throw new ServerWebInputException(
"This attachment policy is not managed by plugin-s3.");
}
}).thenReturn(attachment))
.flatMap(attachment -> {
attachment.getMetadata().getAnnotations().put(
S3OsAttachmentHandler.SKIP_REMOTE_DELETION_ANNO, Boolean.TRUE.toString());
return client.delete(attachment);
});
}
}