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 d8b8bb3 commit 710aa4c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,7 @@
*/
public class SourceCompressingProgressEvent extends ProgressEvent {

private static final String CS = "Compressing source ";

public SourceCompressingProgressEvent(DownloadContext context, Progress progress) {
super(context, progress, CS + progress.getCurrent() + "/" + progress.getTotal());
}

/**
* 返回当前进度的格式化数据。
*
* @return 当前进度
*/
@Override
public String getCurrentMessage() {
return CS + super.getCurrentMessage();
}

/**
* 如果存在总大小则返回比值,
* 否则返回当前进度。
*
* @return 比值或当前进度
*/
@Override
public String getRatioMessage() {
return CS + super.getRatioMessage();
}

/**
* 如果存在总大小则返回百分比,
* 否则返回当前进度。
*
* @return 百分比或当前进度
*/
@Override
public String getPercentageMessage() {
return CS + super.getPercentageMessage();
super(context, progress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@
@Setter
public abstract class AbstractDownloadEvent implements DownloadEvent {

/**
* 事件信息
*/
private String message;

private long timestamp = System.currentTimeMillis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,10 @@
@Getter
public class SourceLoadingProgressEvent extends ProgressEvent {

private static final String L = "Loading ";

private final Source source;

public SourceLoadingProgressEvent(DownloadContext context, Source source, Progress progress) {
super(context, progress, getLS(source) + progress.getCurrent() + "/" + progress.getTotal());
super(context, progress);
this.source = source;
}

/**
* 返回当前进度的格式化数据。
*
* @return 当前进度
*/
@Override
public String getCurrentMessage() {
return getLS() + super.getCurrentMessage();
}

/**
* 如果存在总大小则返回比值,
* 否则返回当前进度。
*
* @return 比值或当前进度
*/
@Override
public String getRatioMessage() {
return getLS() + super.getRatioMessage();
}

/**
* 如果存在总大小则返回百分比,
* 否则返回当前进度。
*
* @return 百分比或当前进度
*/
@Override
public String getPercentageMessage() {
return getLS() + super.getPercentageMessage();
}

public String getLS() {
return getLS(source);
}

public static String getLS(Source source) {
return L + source.getDescription() + " ";
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.compress.SourceCompressingProgressEvent;
import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadStartedEvent;
import com.github.linyuzai.download.core.load.SourceLoadingProgressEvent;
import com.github.linyuzai.download.core.utils.DownloadUtils;
import com.github.linyuzai.download.core.web.ResponseWritingProgressEvent;
import com.github.linyuzai.download.core.write.Progress;
import com.github.linyuzai.download.core.write.ProgressEvent;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -39,7 +42,59 @@ public class ProgressCalculationLogger extends LoggingDownloadEventListener {
* @param event 进度事件
*/
public void onProgress(ProgressEvent event) {
log(event.getContext(), percentage ? event.getPercentageMessage() : event.getRatioMessage());
String s;
if (event instanceof ResponseWritingProgressEvent) {
s = "Writing response ";
} else if (event instanceof SourceCompressingProgressEvent) {
s = "Compressing source ";
} else if (event instanceof SourceLoadingProgressEvent) {
s = "Loading " + ((SourceLoadingProgressEvent) event).getSource().getDescription() + " ";
} else {
s = "Progressing ";
}
String msg = s + (percentage ? getPercentageMessage(event) : getRatioMessage(event));
log(event.getContext(), msg);
}

/**
* 如果存在总大小则返回百分比,
* 否则返回当前进度。
*
* @return 百分比或当前进度
*/
public String getPercentageMessage(ProgressEvent event) {
Progress progress = event.getProgress();
if (progress.hasTotal()) {
double v = (progress.getCurrent() * 1.0 / progress.getTotal()) * 100.0;
String format = String.format("%.2f", v);
return format + "%";
} else {
return getCurrentMessage(progress);
}
}

/**
* 如果存在总大小则返回比值,
* 否则返回当前进度。
*
* @return 比值或当前进度
*/
public String getRatioMessage(ProgressEvent event) {
Progress progress = event.getProgress();
if (progress.hasTotal()) {
return DownloadUtils.format(progress.getCurrent()) + "/" + DownloadUtils.format(progress.getTotal());
} else {
return getCurrentMessage(progress);
}
}

/**
* 返回当前进度的格式化数据。
*
* @return 当前进度
*/
public String getCurrentMessage(Progress progress) {
return DownloadUtils.format(progress.getCurrent());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,7 @@
*/
public class ResponseWritingProgressEvent extends ProgressEvent {

private static final String WR = "Writing response ";

public ResponseWritingProgressEvent(DownloadContext context, Progress progress) {
super(context, progress, WR + progress.getCurrent() + "/" + progress.getTotal());
}

/**
* 返回当前进度的格式化数据。
*
* @return 当前进度
*/
@Override
public String getCurrentMessage() {
return WR + super.getCurrentMessage();
}

/**
* 如果存在总大小则返回比值,
* 否则返回当前进度。
*
* @return 比值或当前进度
*/
@Override
public String getRatioMessage() {
return WR + super.getRatioMessage();
}

/**
* 如果存在总大小则返回百分比,
* 否则返回当前进度。
*
* @return 百分比或当前进度
*/
@Override
public String getPercentageMessage() {
return WR + super.getPercentageMessage();
super(context, progress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadContextEvent;
import com.github.linyuzai.download.core.utils.DownloadUtils;
import lombok.Getter;
import lombok.NonNull;

Expand All @@ -17,59 +16,8 @@ public abstract class ProgressEvent extends DownloadContextEvent {
*/
private final Progress progress;

public ProgressEvent(@NonNull DownloadContext context, Progress progress, String message) {
public ProgressEvent(@NonNull DownloadContext context, Progress progress) {
super(context);
this.progress = progress;
setMessage(message);
}

/**
* 将大小转为B,K,M等单位。
*
* @return 格式化后的数据
*/
public String getBaseCurrentMessage() {
return DownloadUtils.format(progress.getCurrent());
}

/**
* 返回当前进度的格式化数据。
*
* @return 当前进度
*/
public String getCurrentMessage() {
return getBaseCurrentMessage();
}

/**
* 如果存在总大小则返回比值,
* 否则返回当前进度。
*
* @return 比值或当前进度
*/
public String getRatioMessage() {
Progress progress = getProgress();
if (progress.hasTotal()) {
return DownloadUtils.format(progress.getCurrent()) + "/" + DownloadUtils.format(progress.getTotal());
} else {
return getBaseCurrentMessage();
}
}

/**
* 如果存在总大小则返回百分比,
* 否则返回当前进度。
*
* @return 百分比或当前进度
*/
public String getPercentageMessage() {
Progress progress = getProgress();
if (progress.hasTotal()) {
double v = (progress.getCurrent() * 1.0 / progress.getTotal()) * 100.0;
String format = String.format("%.2f", v);
return format + "%";
} else {
return getBaseCurrentMessage();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
public class WebClientSource extends HttpSource implements ReactorSource {

/**
* 不会被调用
* 请求资源
*
* @param context {@link DownloadContext}
* @return {@link Mono#empty()}
*/
@SneakyThrows
@Override
Expand Down

0 comments on commit 710aa4c

Please sign in to comment.