|
3 | 3 | import com.fasterxml.jackson.core.type.TypeReference; |
4 | 4 | import lombok.extern.slf4j.Slf4j; |
5 | 5 | import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.springframework.core.io.buffer.DataBuffer; |
| 7 | +import org.springframework.core.io.buffer.DataBufferFactory; |
| 8 | +import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
6 | 9 | import org.springframework.http.*; |
7 | | -import org.springframework.http.client.ClientHttpRequestExecution; |
8 | | -import org.springframework.scheduling.annotation.Scheduled; |
| 10 | +import org.springframework.http.client.ClientHttpRequest; |
| 11 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; |
9 | 12 | import org.springframework.stereotype.Component; |
10 | 13 | import org.springframework.util.Assert; |
11 | 14 | import org.springframework.util.LinkedMultiValueMap; |
12 | 15 | import org.springframework.util.MultiValueMap; |
13 | 16 | import org.springframework.web.client.HttpClientErrorException; |
| 17 | +import org.springframework.web.client.RequestCallback; |
| 18 | +import org.springframework.web.client.ResponseExtractor; |
14 | 19 | import org.springframework.web.client.RestTemplate; |
15 | 20 | import org.springframework.web.util.UriComponentsBuilder; |
| 21 | +import reactor.core.publisher.Flux; |
| 22 | +import reactor.core.publisher.FluxSink; |
| 23 | +import reactor.core.scheduler.Schedulers; |
16 | 24 | import run.ikaros.api.core.attachment.AttachmentDriver; |
17 | 25 | import run.ikaros.plugin.pan115.Pan115Const; |
18 | 26 | import run.ikaros.plugin.pan115.exception.Pan115RequestFailException; |
|
22 | 30 | import run.ikaros.plugin.pan115.model.Pan115Result; |
23 | 31 | import run.ikaros.plugin.pan115.utils.JsonUtils; |
24 | 32 |
|
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStream; |
25 | 35 | import java.net.URI; |
26 | | -import java.net.URLEncoder; |
| 36 | +import java.net.URLDecoder; |
27 | 37 | import java.nio.charset.StandardCharsets; |
28 | 38 | import java.time.LocalDateTime; |
29 | | -import java.util.*; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.LinkedHashMap; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.concurrent.atomic.AtomicBoolean; |
30 | 44 |
|
31 | 45 | import static run.ikaros.plugin.pan115.Pan115Const.*; |
32 | 46 |
|
33 | 47 | @Slf4j |
34 | 48 | @Component |
35 | 49 | public class DefaultPan115Repository implements Pan115Repository { |
36 | | - private final RestTemplate restTemplate = new RestTemplate(); |
| 50 | + private final RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory()); |
| 51 | + private final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(); |
37 | 52 | private final HttpHeaders headers = new HttpHeaders(); |
38 | 53 |
|
39 | 54 | @Override |
@@ -238,8 +253,126 @@ public String openVideoPlay(String pickCode) { |
238 | 253 | } |
239 | 254 | } |
240 | 255 |
|
241 | | - @Scheduled(fixedDelay = 5000) // 5秒 |
242 | | - public void fixedDelayTask() { |
243 | | - log.info("Fixed delay task - " + new Date()); |
| 256 | + @Override |
| 257 | + public Flux<DataBuffer> openUFileSteam(String pickCode) { |
| 258 | + Assert.hasText(pickCode, "'pickCode' must has text."); |
| 259 | + final String uFileDownUrl = openUFileDownUrl(pickCode); |
| 260 | + return streamFileWithRestTemplate(uFileDownUrl); |
| 261 | + // return Flux.create(sink -> { |
| 262 | + // Schedulers.boundedElastic().schedule(() -> { |
| 263 | + // try { |
| 264 | + // ResponseEntity<byte[]> response = restTemplate.getForEntity( |
| 265 | + // uFileDownUrl, byte[].class); |
| 266 | + // if (response.getStatusCode() == HttpStatus.OK && |
| 267 | + // response.getBody() != null) { |
| 268 | + // // 一次性发送所有数据(适合小文件) |
| 269 | + // DataBuffer dataBuffer = dataBufferFactory.allocateBuffer( |
| 270 | + // response.getBody().length); |
| 271 | + // dataBuffer.write(response.getBody()); |
| 272 | + // sink.next(dataBuffer); |
| 273 | + // sink.complete(); |
| 274 | + // } else { |
| 275 | + // sink.error(new RuntimeException( |
| 276 | + // "请求失败,状态码: " + response.getStatusCode())); |
| 277 | + // } |
| 278 | + // } catch (Exception e) { |
| 279 | + // sink.error(e); |
| 280 | + // } |
| 281 | + // }); |
| 282 | + // }); |
| 283 | + } |
| 284 | + |
| 285 | + |
| 286 | + /** |
| 287 | + * 使用 RestTemplate 获取文件流,转换为 Flux<DataBuffer> |
| 288 | + * |
| 289 | + * @param fileUrl 文件URL |
| 290 | + * @param chunkSize 分块大小(字节),默认为 8192 |
| 291 | + * @return Flux<DataBuffer> |
| 292 | + */ |
| 293 | + public Flux<DataBuffer> streamFileWithRestTemplate(String fileUrl, int chunkSize) { |
| 294 | + if (chunkSize <= 0) { |
| 295 | + chunkSize = 8192; // 默认8KB |
| 296 | + } |
| 297 | + |
| 298 | + final int finalChunkSize = chunkSize; |
| 299 | + |
| 300 | + return Flux.create(sink -> { |
| 301 | + // 使用 boundedElastic 调度器执行阻塞操作 |
| 302 | + Schedulers.boundedElastic().schedule(() -> { |
| 303 | + try { |
| 304 | + String newUrl = fileUrl; |
| 305 | + if (StringUtils.isNoneBlank(newUrl)) { |
| 306 | + newUrl = URLDecoder.decode(newUrl, StandardCharsets.UTF_8); |
| 307 | + } |
| 308 | + log.debug("Open stream for url: {}", newUrl); |
| 309 | + restTemplate.execute(newUrl, HttpMethod.GET, new RequestCallback() { |
| 310 | + @Override |
| 311 | + public void doWithRequest(ClientHttpRequest request) throws IOException { |
| 312 | + request.getHeaders().addAll(headers); |
| 313 | + } |
| 314 | + }, |
| 315 | + (ResponseExtractor<Void>) response -> { |
| 316 | + if (response.getStatusCode() != HttpStatus.OK) { |
| 317 | + sink.error(new RuntimeException( |
| 318 | + "请求失败,状态码: " + response.getStatusCode())); |
| 319 | + return null; |
| 320 | + } |
| 321 | + |
| 322 | + try (InputStream inputStream = response.getBody()) { |
| 323 | + byte[] buffer = new byte[finalChunkSize]; |
| 324 | + int bytesRead; |
| 325 | + AtomicBoolean isCanceled = new AtomicBoolean(false); |
| 326 | + |
| 327 | + sink.onCancel(() -> { |
| 328 | + isCanceled.set(true); |
| 329 | + try { |
| 330 | + inputStream.close(); |
| 331 | + } catch (IOException e) { |
| 332 | + // 忽略关闭异常 |
| 333 | + } |
| 334 | + }); |
| 335 | + |
| 336 | + while (!isCanceled.get() && |
| 337 | + (bytesRead = inputStream.read(buffer)) != -1) { |
| 338 | + |
| 339 | + // 创建 DataBuffer |
| 340 | + DataBuffer dataBuffer = dataBufferFactory.allocateBuffer(bytesRead); |
| 341 | + dataBuffer.write(buffer, 0, bytesRead); |
| 342 | + |
| 343 | + // 发布数据块 |
| 344 | + sink.next(dataBuffer); |
| 345 | + |
| 346 | + // 检查是否取消 |
| 347 | + if (sink.isCancelled()) { |
| 348 | + log.debug("Close stream for url: {}", fileUrl); |
| 349 | + break; |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + if (!isCanceled.get()) { |
| 354 | + sink.complete(); |
| 355 | + } |
| 356 | + } catch (Exception e) { |
| 357 | + if (!sink.isCancelled()) { |
| 358 | + sink.error(e); |
| 359 | + } |
| 360 | + } |
| 361 | + return null; |
| 362 | + }); |
| 363 | + } catch (Exception e) { |
| 364 | + if (!sink.isCancelled()) { |
| 365 | + sink.error(e); |
| 366 | + } |
| 367 | + } |
| 368 | + }); |
| 369 | + }, FluxSink.OverflowStrategy.BUFFER); |
| 370 | + } |
| 371 | + |
| 372 | + /** |
| 373 | + * 简化版本:使用默认分块大小 |
| 374 | + */ |
| 375 | + public Flux<DataBuffer> streamFileWithRestTemplate(String fileUrl) { |
| 376 | + return streamFileWithRestTemplate(fileUrl, 8192); |
244 | 377 | } |
245 | 378 | } |
0 commit comments