use of lee.study.down.exception.BootstrapException 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.exception.BootstrapException in project proxyee-down by monkeyWie.
the class HttpDownController method commonStartTask.
public static ResultInfo commonStartTask(NewTaskForm taskForm) throws Exception {
ResultInfo resultInfo = new ResultInfo();
AbstractHttpDownBootstrap bootstrap = ContentManager.DOWN.getBoot(taskForm.getId());
HttpDownInfo httpDownInfo = bootstrap.getHttpDownInfo();
// 覆盖下载
if (!StringUtils.isEmpty(taskForm.getOldId())) {
AbstractHttpDownBootstrap oldBootstrap = ContentManager.DOWN.getBoot(taskForm.getOldId());
if (oldBootstrap == null) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("任务不存在");
return resultInfo;
} else {
// 暂停之前的下载任务
oldBootstrap.pauseDown();
// 修改request
oldBootstrap.getHttpDownInfo().setRequest(httpDownInfo.getRequest());
oldBootstrap.getHttpDownInfo().setProxyConfig(httpDownInfo.getProxyConfig());
Map<String, Object> attr = oldBootstrap.getHttpDownInfo().getAttrs();
if (attr == null) {
attr = new HashMap<>();
oldBootstrap.getHttpDownInfo().setAttrs(attr);
}
attr.put(NewTaskForm.KEY_UNZIP_FLAG, taskForm.isUnzip());
attr.put(NewTaskForm.KEY_UNZIP_PATH, taskForm.getUnzipPath());
// 移除新的下载任务
ContentManager.DOWN.removeBoot(taskForm.getId());
// 持久化
ContentManager.DOWN.save();
// 用新链接继续下载
oldBootstrap.continueDown();
}
} else {
if (StringUtils.isEmpty(taskForm.getFileName())) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("文件名不能为空");
return resultInfo;
}
if (StringUtils.isEmpty(taskForm.getFilePath())) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("路径不能为空");
return resultInfo;
}
buildDefaultValues(taskForm);
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
synchronized (taskInfo) {
if (taskInfo.getStatus() != HttpDownStatus.WAIT) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("任务已添加至下载列表");
}
taskInfo.setFileName(taskForm.getFileName());
taskInfo.setFilePath(taskForm.getFilePath());
Map<String, Object> attr = httpDownInfo.getAttrs();
if (attr == null) {
attr = new HashMap<>();
httpDownInfo.setAttrs(attr);
}
attr.put(NewTaskForm.KEY_UNZIP_FLAG, taskForm.isUnzip());
attr.put(NewTaskForm.KEY_UNZIP_PATH, taskForm.getUnzipPath());
if (taskInfo.isSupportRange()) {
taskInfo.setConnections(taskForm.getConnections());
} else {
taskInfo.setConnections(1);
}
try {
bootstrap.startDown();
} catch (BootstrapException e) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg(e.getMessage());
return resultInfo;
}
// 记录存储路径
String lastPath = ContentManager.CONFIG.get().getLastPath();
if (!taskForm.getFilePath().equalsIgnoreCase(lastPath)) {
ContentManager.CONFIG.get().setLastPath(taskForm.getFilePath());
ContentManager.CONFIG.save();
}
}
}
return resultInfo;
}
Aggregations