use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method delete.
public void delete(boolean delFile) throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
// 删除任务进度记录文件
synchronized (taskInfo) {
close();
timeoutCheckTask.delBoot(httpDownInfo.getTaskInfo().getId());
FileUtil.deleteIfExists(taskInfo.buildTaskRecordFilePath());
FileUtil.deleteIfExists(taskInfo.buildTaskRecordBakFilePath());
if (delFile) {
FileUtil.deleteIfExists(taskInfo.buildTaskFilePath());
}
if (callback != null) {
callback.onDelete(httpDownInfo);
}
}
}
use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startDown.
public void startDown() throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
taskInfo.buildChunkInfoList();
if (!FileUtil.exists(taskInfo.getFilePath())) {
FileUtil.createDirSmart(taskInfo.getFilePath());
}
if (!FileUtil.canWrite(taskInfo.getFilePath())) {
throw new BootstrapException("无权访问下载路径,请修改路径或开放目录写入权限");
}
// 磁盘空间不足
if (taskInfo.getTotalSize() > FileUtil.getDiskFreeSize(taskInfo.getFilePath())) {
throw new BootstrapException("磁盘空间不足,请修改路径");
}
// 有文件同名
if (new File(taskInfo.buildTaskFilePath()).exists()) {
throw new BootstrapException("文件名已存在,请修改文件名");
}
// 创建文件
try (RandomAccessFile randomAccessFile = new RandomAccessFile(taskInfo.buildTaskFilePath(), "rw")) {
randomAccessFile.setLength(taskInfo.getTotalSize());
}
// 文件下载开始回调
taskInfo.reset();
taskInfo.setStatus(HttpDownStatus.RUNNING);
taskInfo.setStartTime(System.currentTimeMillis());
for (int i = 0; i < taskInfo.getChunkInfoList().size(); i++) {
ChunkInfo chunkInfo = taskInfo.getChunkInfoList().get(i);
// 设置状态和时间
chunkInfo.setStartTime(System.currentTimeMillis());
startChunkDown(chunkInfo, HttpDownStatus.CONNECTING_NORMAL);
}
if (callback != null) {
callback.onStart(httpDownInfo);
}
afterStart();
}
use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.
the class HttpDownUtil method getTaskInfo.
/**
* 检测是否支持断点下载
*/
public static TaskInfo getTaskInfo(HttpRequest httpRequest, HttpHeaders resHeaders, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
HttpResponse httpResponse = null;
if (resHeaders == null) {
httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
// 处理重定向
if ((httpResponse.status().code() + "").indexOf("30") == 0) {
// TODO 302重定向乱码 https://link.gimhoy.com/googledrive/aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL29wZW4/aWQ9MThlVmNKeEhwaE40RUpGTUowSk10bWNXOVhCcWJhVE1k.jpg
String redirectUrl = httpResponse.headers().get(HttpHeaderNames.LOCATION);
HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
requestInfo.headers().remove("Host");
requestInfo.setUri(redirectUrl);
RequestProto requestProto = ProtoUtil.getRequestProto(requestInfo);
requestInfo.headers().set("Host", requestProto.getHost());
requestInfo.setRequestProto(requestProto);
return getTaskInfo(httpRequest, null, proxyConfig, clientSslCtx, loopGroup);
}
resHeaders = httpResponse.headers();
}
TaskInfo taskInfo = new TaskInfo().setId(UUID.randomUUID().toString()).setFileName(getDownFileName(httpRequest, resHeaders)).setTotalSize(getDownFileSize(resHeaders));
// chunked编码不支持断点下载
if (resHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) {
if (httpResponse == null) {
httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
}
// 206表示支持断点下载
if (httpResponse.status().equals(HttpResponseStatus.PARTIAL_CONTENT)) {
taskInfo.setSupportRange(true);
}
}
return taskInfo;
}
use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.
the class DownContent method setUrl.
public static List<TaskInfoForm> setUrl(List<TaskInfo> taskInfoList) {
List<TaskInfoForm> ret = new ArrayList<>();
for (TaskInfo taskInfo : taskInfoList) {
TaskInfoForm taskInfoForm = new TaskInfoForm();
BeanUtils.copyProperties(taskInfo, taskInfoForm);
HttpRequestInfo httpRequest = (HttpRequestInfo) ContentManager.DOWN.getDownInfo(taskInfo.getId()).getRequest();
String uri = httpRequest.uri();
String host = httpRequest.requestProto().getHost();
String url = (uri.indexOf("/") == 0 ? host : "") + uri;
if (url.indexOf("http://") != 0 && url.indexOf("https://") != 0) {
url = (httpRequest.requestProto().getSsl() ? "https://" : "http://") + url;
}
taskInfoForm.setUrl(url);
ret.add(taskInfoForm);
}
return ret;
}
use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.
the class DownContent method init.
/**
* 从配置文件中加载信息
*/
public void init() {
downContent = new ConcurrentHashMap<>();
if (FileUtil.exists(HttpDownConstant.TASK_RECORD_PATH)) {
try {
List<HttpDownInfo> records = (List<HttpDownInfo>) ByteUtil.deserialize(HttpDownConstant.TASK_RECORD_PATH);
for (HttpDownInfo httpDownInfo : records) {
AbstractHttpDownBootstrap bootstrap = HttpDownBootstrapFactory.create(httpDownInfo, ContentManager.CONFIG.get().getRetryCount(), HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup, HttpDownConstant.httpDownCallback);
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
if (taskInfo.getStatus() == HttpDownStatus.WAIT) {
continue;
}
// 下载未完成
if (taskInfo.getStatus() != HttpDownStatus.DONE) {
String taskDetailPath = taskInfo.buildTaskRecordFilePath();
String taskDetailBakPath = taskInfo.buildTaskRecordBakFilePath();
// 存在下载进度信息则更新,否则重新下载
if (FileUtil.existsAny(taskDetailPath, taskDetailBakPath)) {
try {
taskInfo = (TaskInfo) ByteUtil.deserialize(taskDetailPath, taskDetailBakPath);
httpDownInfo.setTaskInfo(taskInfo);
} catch (IOException | ClassNotFoundException e) {
taskInfo.reset();
}
} else {
taskInfo.reset();
}
if (taskInfo.getStatus() != HttpDownStatus.FAIL) {
// 设置为暂停状态
taskInfo.setStatus(HttpDownStatus.PAUSE);
taskInfo.getChunkInfoList().forEach((chunk) -> {
if (chunk.getStatus() != HttpDownStatus.DONE) {
chunk.setStatus(HttpDownStatus.PAUSE);
}
});
}
}
putBoot(bootstrap);
}
} catch (Exception e) {
LOGGER.warn("加载配置文件失败:", e);
}
}
}
Aggregations