All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.download.module.progress.flow.GetProgressListFlow Maven / Gradle / Ivy

The newest version!
package cn.schoolwow.download.module.progress.flow;

import cn.schoolwow.download.domain.progress.DownloadProgress;
import cn.schoolwow.download.module.pool.domain.PoolContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.ArrayList;

public class GetProgressListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        PoolContext poolContext = flowContext.checkInstanceData(PoolContext.class);

        if(poolContext.downloadProgressList.isEmpty()){
            flowContext.putData("downloadProgressList", new ArrayList<>());
            return;
        }
        synchronized (poolContext.downloadProgressList){
            for(DownloadProgress downloadProgress:poolContext.downloadProgressList){
                //如果间隔时间小于1s,直接返回
                long intervalTimes = System.currentTimeMillis()-downloadProgress.lastTime;
                if(intervalTimes>=1000&&"下载中".equals(downloadProgress.state)){
                    downloadProgress.currentFileSize = downloadProgress.getFileDownloadedSize.getFileDownloadedSize();
                    int m3u8SuccessCount = downloadProgress.getFileDownloadedSize.getM3u8SuccessCount();
                    if(downloadProgress.m3u8){
                        downloadProgress.currentFileSizeFormat = m3u8SuccessCount+"("+String.format("%.2fMB",downloadProgress.currentFileSize/1.0/1024/1024)+")";
                    }else{
                        downloadProgress.currentFileSizeFormat = String.format("%.2fMB",downloadProgress.currentFileSize/1.0/1024/1024);
                    }
                    downloadProgress.downloadSpeed = (downloadProgress.currentFileSize - downloadProgress.lastDownloadedFileSize)/(intervalTimes/1000);
                    if(downloadProgress.downloadSpeed<1024*1024){
                        downloadProgress.downloadSpeedFormat = String.format("%.2fKB/s",downloadProgress.downloadSpeed/1.0/1024);
                    }else{
                        downloadProgress.downloadSpeedFormat = String.format("%.2fMB/s",downloadProgress.downloadSpeed/1.0/1024/1024);
                    }
                    if(downloadProgress.m3u8){
                        int m3u8SegmentSize = downloadProgress.getFileDownloadedSize.getM3u8SegmentSize();
                        downloadProgress.percent = m3u8SuccessCount*100/m3u8SegmentSize;
                    }else if(downloadProgress.totalFileSize>0){
                        downloadProgress.percent = (int) (downloadProgress.currentFileSize*100/downloadProgress.totalFileSize);
                    }
                    downloadProgress.lastTime = System.currentTimeMillis();
                    downloadProgress.lastDownloadedFileSize = downloadProgress.currentFileSize;
                }
            }
        }
    }

    @Override
    public String name() {
        return "获取下载进度列表";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy