Skip to content

Commit

Permalink
download2
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Nov 30, 2023
1 parent 59c3f33 commit f78eb27
Show file tree
Hide file tree
Showing 25 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public abstract class AbstractSourceCompressor<OS extends OutputStream> implemen
*/
@Override
public Compression compress(Source source, DownloadWriter writer, DownloadContext context) throws IOException {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
String cachePath = options.getCompressCachePath();
String cacheName = getCacheName(source, context);
boolean cacheEnable = options.isCompressCacheEnabled();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
//是否启用缓存
if (cacheEnable) {
File dir = new File(cachePath);
Expand Down Expand Up @@ -77,7 +77,7 @@ public Compression compress(Source source, DownloadWriter writer, DownloadContex
*/
@SneakyThrows
public void doCompress(Source source, OutputStream os, DownloadWriter writer, DownloadContext context) {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new SourceCompressionFormatEvent(context, source, getFormat()));
try (OS nos = newOutputStream(os, source, context)) {
Progress progress = new Progress(source.getLength());
Expand Down Expand Up @@ -129,7 +129,7 @@ public void doCompress(Source source, OutputStream os, DownloadWriter writer, Do
* @return 压缩文件缓存名称
*/
public String getCacheName(Source source, DownloadContext context) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
String compressCacheName = options.getCompressCacheName();
String suffix = getSuffix();
String nameToUse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public Object download(DownloadOptions options) {
DownloadContext context = contextFactory.create();
context.set(DownloadOptions.class, options);
context.set(DownloadLogger.class, logger);
if (options.getAsyncConsumer() == null) {
context.set(DownloadMode.class, DownloadMode.SYNC);
} else {
context.set(DownloadMode.class, DownloadMode.ASYNC);
}
DownloadEventPublisher publisher = delegateDownloadEventPublisher(options);
context.set(DownloadEventPublisher.class, publisher);
publisher.publish(new DownloadStartEvent(context));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.linyuzai.download.core.concept;

import com.github.linyuzai.download.core.context.DownloadContext;

public enum DownloadMode {

SYNC, ASYNC;

public static DownloadMode getMode(DownloadContext context) {
return context.get(DownloadMode.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.linyuzai.download.core.event;

import com.github.linyuzai.download.core.context.DownloadContext;

/**
* {@link DownloadEvent} 发布器。
*
* @see SimpleDownloadEventPublisher
* @see ApplicationDownloadEventPublisher
*/
public interface DownloadEventPublisher {

Expand All @@ -14,4 +15,8 @@ public interface DownloadEventPublisher {
* @param event 事件
*/
void publish(Object event);

static DownloadEventPublisher get(DownloadContext context) {
return context.get(DownloadEventPublisher.class);
}
}
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.concept.DownloadMode;
import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadLifecycleListener;
import com.github.linyuzai.download.core.handler.DownloadHandler;
Expand All @@ -11,13 +12,12 @@ public class AsyncConsumeHandler implements DownloadHandler, DownloadLifecycleLi

@Override
public boolean support(DownloadContext context) {
DownloadOptions options = context.get(DownloadOptions.class);
return options.getAsyncConsumer() != null;
return DownloadMode.getMode(context) == DownloadMode.ASYNC;
}

@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
options.getAsyncConsumer().accept(context);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class CompressSourceHandler implements DownloadHandler, DownloadLifecycle
@SneakyThrows
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Source source = context.get(Source.class);
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
Compression compression;
boolean single = source.isSingle();
boolean forceCompress = options.isForceCompress();
Expand Down Expand Up @@ -85,8 +85,8 @@ public void onStart(DownloadContext context) {
public void onComplete(DownloadContext context) {
Compression compression = context.get(Compression.class);
if (compression != null) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadOptions options = DownloadOptions.get(context);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
boolean delete = options.isCompressCacheDelete();
//是否删除缓存
if (delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public class CreateSourceHandler implements DownloadHandler, DownloadLifecycleLi
*/
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Object original = options.getSource();
SourceFactory factory = sourceFactoryAdapter.getFactory(original, context);
Source source = factory.create(original, context);
context.set(Source.class, source);
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new AfterSourceCreatedEvent(context, source));
return chain.next(context);
}
Expand All @@ -63,8 +63,8 @@ public void onStart(DownloadContext context) {
public void onComplete(DownloadContext context) {
Source source = context.get(Source.class);
if (source != null) {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadOptions options = context.get(DownloadOptions.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
DownloadOptions options = DownloadOptions.get(context);
boolean delete = options.isSourceCacheDelete();
//是否删除缓存
if (delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public class LoadSourceHandler implements DownloadHandler {
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
Source source = context.get(Source.class);
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
sourceLoader.load(source, context);
publisher.publish(new AfterSourceLoadedEvent(context, source));
//context.set(Source.class, load);
return chain.next(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.download.core.handler.impl;

import com.github.linyuzai.download.core.compress.Compression;
import com.github.linyuzai.download.core.concept.DownloadMode;
import com.github.linyuzai.download.core.concept.Part;
import com.github.linyuzai.download.core.concept.Resource;
import com.github.linyuzai.download.core.context.DownloadContext;
Expand Down Expand Up @@ -37,8 +38,7 @@ public class WriteResponseHandler implements DownloadHandler, DownloadLifecycleL

@Override
public boolean support(DownloadContext context) {
DownloadOptions options = context.get(DownloadOptions.class);
return options.getAsyncConsumer() == null;
return DownloadMode.getMode(context) == DownloadMode.SYNC;
}

/**
Expand All @@ -55,8 +55,8 @@ public boolean support(DownloadContext context) {
@Override
public Object handle(DownloadContext context, DownloadHandlerChain chain) {
Compression compression = context.get(Compression.class);
DownloadOptions options = context.get(DownloadOptions.class);
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadOptions options = DownloadOptions.get(context);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
//获得Request
DownloadRequest request = options.getRequest();
//获得Response
Expand Down Expand Up @@ -98,10 +98,10 @@ public void accept(OutputStream os) {
* 设置 Content-Type,
* 设置自定义的响应头。
*
* @param response {@link DownloadResponse}
* @param response {@link DownloadResponse}
* @param resource {@link Compression}
* @param range {@link Range}
* @param context {@link DownloadContext}
* @param range {@link Range}
* @param context {@link DownloadContext}
* @return 是否继续处理
*/
public boolean applyHeaders(DownloadResponse response, Resource resource, Range range, DownloadContext context) {
Expand Down Expand Up @@ -140,7 +140,7 @@ public boolean applyHeaders(DownloadResponse response, Resource resource, Range
}
}

DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);

//inline 或 attachment
boolean inline = options.isInline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean isCacheExisted() {
@SneakyThrows
@Override
public void load(DownloadContext context) {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
if (inputStream != null) {
//直接使用
publisher.publish(new SourceAlreadyLoadedEvent(context, this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class RemoteLoadableSource extends AbstractLoadableSource {
public void doLoad(OutputStream os, DownloadContext context) throws IOException {
DownloadWriterAdapter writerAdapter = context.get(DownloadWriterAdapter.class);
DownloadWriter writer = writerAdapter.getWriter(this, context);
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
InputStream is = loadRemote(context);
Progress progress = new Progress(length);
writer.write(is, os, null, null, length, (current, increase) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void log(DownloadContext context, String message) {
*/
public void log(DownloadContext context, String tag, String message) {
String info;
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Method method = options.getMethod();
if (method == null) {
info = tag + message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ interface Configurer {
void configure(ConfigurableDownloadOptions options);
}

static DownloadOptions getOptions(DownloadContext context) {
static DownloadOptions get(DownloadContext context) {
return context.get(DownloadOptions.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public boolean support(Object source, DownloadContext context) {

@Override
public Source create(Object source, DownloadContext context) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Charset charset = options.getCharset();
FileSource build = new FileSource.Builder<>()
.file((File) source)
.charset(charset)
.build();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new FileSourceCreatedEvent(context, build));
return build;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean isSingle() {
//@SneakyThrows
@Override
public InputStream loadRemote(DownloadContext context) throws IOException {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new LoadHttpSourceEvent(context, this));
HttpURLConnection connection = getConnection();
int code = connection.getResponseCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HttpSourceFactory extends PrefixSourceFactory {
@Override
public Source create(Object source, DownloadContext context) {
String url = (String) source;
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Charset charset = options.getCharset();
boolean cacheEnabled = options.isSourceCacheEnabled();
String cachePath = options.getSourceCachePath();
Expand All @@ -31,7 +31,7 @@ public Source create(Object source, DownloadContext context) {
.cacheEnabled(cacheEnabled)
.cachePath(cachePath)
.build();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new HttpSourceCreatedEvent(context, build));
return build;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class OkHttpSource extends HttpSource {

@Override
public InputStream loadRemote(DownloadContext context) throws IOException {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new LoadOkHttpSourceEvent(context, this));
Request.Builder rb = new Request.Builder();
rb.url(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public OkHttpSourceFactory() {
@Override
public Source create(Object source, DownloadContext context) {
String url = (String) source;
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Charset charset = options.getCharset();
boolean cacheEnabled = options.isSourceCacheEnabled();
String cachePath = options.getSourceCachePath();
Expand All @@ -43,7 +43,7 @@ public Source create(Object source, DownloadContext context) {
.cacheEnabled(cacheEnabled)
.cachePath(cachePath)
.build();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new OkHttpSourceCreatedEvent(context, build));
return build;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public boolean support(Object source, DownloadContext context) {

@Override
public Source create(Object source, DownloadContext context) {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new OriginalSourceCreatedEvent(context, (Source) source));
return (Source) source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public boolean support(Object source, DownloadContext context) {

@Override
public Source create(Object source, DownloadContext context) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Charset charset = options.getCharset();
TextSource build = new TextSource.Builder<>()
.text((String) source)
.name("text.txt")
.charset(charset)
.build();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new TextSourceCreatedEvent(context, build));
return build;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public boolean support(Object source, DownloadContext context) {

@Override
public Source create(Object source, DownloadContext context) {
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Charset charset = options.getCharset();
ClassPathSource build = new ClassPathSource.Builder<>()
.resource((ClassPathResource) source)
.charset(charset)
.build();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new ClassPathSourceCreatedEvent(context, build));
return build;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Mono<Void> preload(DownloadContext context) {
}

protected Mono<InputStream> loadInputStream(DownloadContext context) {
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new LoadWebClientSourceEvent(context, this));
return WebClient.create()
.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class WebClientSourceFactory extends PrefixSourceFactory {
@Override
public Source create(Object source, DownloadContext context) {
String url = (String) source;
DownloadOptions options = context.get(DownloadOptions.class);
DownloadOptions options = DownloadOptions.get(context);
Charset charset = options.getCharset();
boolean cacheEnabled = options.isSourceCacheEnabled();
String cachePath = options.getSourceCachePath();
Expand All @@ -30,7 +30,7 @@ public Source create(Object source, DownloadContext context) {
.cacheEnabled(cacheEnabled)
.cachePath(cachePath)
.build();
DownloadEventPublisher publisher = context.get(DownloadEventPublisher.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new WebClientSourceCreatedEvent(context, build));
return build;
}
Expand Down
Loading

0 comments on commit f78eb27

Please sign in to comment.