Skip to content

Commit

Permalink
如果已经存在压缩对象则跳过部分处理器
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Dec 26, 2023
1 parent 710aa4c commit 08f31e0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public abstract class AbstractSourceCompressor<OS extends OutputStream> implemen
* 如果启用缓存并且缓存不存在,压缩到本地缓存文件。
*
* @param source {@link Source}
* @param format 压缩格式
* @param writer {@link DownloadWriter}
* @param context {@link DownloadContext}
* @return {@link MemoryCompression} / {@link FileCompression}
*/
@Override
public Compression compress(Source source, DownloadWriter writer, DownloadContext context) throws IOException {
public Compression compress(Source source, String format, DownloadWriter writer, DownloadContext context) throws IOException {
DownloadOptions options = DownloadOptions.get(context);
String format = options.getCompressFormat();
String cachePath = options.getCompressCachePath();
String cacheName = getCacheName(source, format, context);
boolean cacheEnable = options.isCompressCacheEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ default boolean support(String format, DownloadContext context) {
* 如果支持对应的格式就会调用该方法执行压缩。
*
* @param source {@link Source}
* @param format 压缩格式
* @param writer {@link DownloadWriter}
* @param context {@link DownloadContext}
* @return {@link Compression}
*/
Compression compress(Source source, DownloadWriter writer, DownloadContext context) throws IOException;
Compression compress(Source source, String format, DownloadWriter writer, DownloadContext context) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class CompressSourceHandler implements DownloadHandler, DownloadLifecycle
@SneakyThrows
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
if (context.contains(Compression.class)) {
return chain.next(context);
}
DownloadOptions options = DownloadOptions.get(context);
Source source = context.get(Source.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
Expand All @@ -58,7 +61,7 @@ public Object handle(DownloadContext context, DownloadHandlerChain chain) {
SourceCompressor compressor = sourceCompressorAdapter.getCompressor(formatToUse, context);
DownloadWriterAdapter writerAdapter = context.get(DownloadWriterAdapter.class);
DownloadWriter writer = writerAdapter.getWriter(source, context);
compression = compressor.compress(source, writer, context);
compression = compressor.compress(source, formatToUse, writer, context);
}
publisher.publish(new SourceCompressedEvent(context, source, compression));
context.set(Compression.class, compression);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.linyuzai.download.core.handler.impl;

import com.github.linyuzai.download.core.compress.Compression;
import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadEventPublisher;
import com.github.linyuzai.download.core.event.DownloadLifecycleListener;
Expand Down Expand Up @@ -33,6 +34,9 @@ public class CreateSourceHandler implements DownloadHandler, DownloadLifecycleLi
*/
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
if (context.contains(Compression.class)) {
return chain.next(context);
}
DownloadOptions options = DownloadOptions.get(context);
Object original = options.getSource();
SourceFactory factory = sourceFactoryAdapter.getFactory(original, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class LoadSourceHandler implements DownloadHandler {
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
Source source = context.get(Source.class);
if (source == null) {
return chain.next(context);
}
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
sourceLoader.load(source, context);
publisher.publish(new SourceLoadedEvent(context, source));
Expand Down

0 comments on commit 08f31e0

Please sign in to comment.