parent
598c863559
commit
30c41e4cb2
@ -1,207 +1,199 @@ |
|||||||
/* |
///*
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
// * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
* |
// *
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
// * Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License. |
// * you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at |
// * You may obtain a copy of the License at
|
||||||
* |
// *
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
// * http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
// *
|
||||||
* Unless required by applicable law or agreed to in writing, software |
// * Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
// * distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and |
// * See the License for the specific language governing permissions and
|
||||||
* limitations under the License. |
// * limitations under the License.
|
||||||
*/ |
// */
|
||||||
package com.arialyy.aria.core.download; |
//package com.arialyy.aria.core.download;
|
||||||
|
//
|
||||||
import android.text.TextUtils; |
//import android.text.TextUtils;
|
||||||
import com.arialyy.aria.core.manager.TaskWrapperManager; |
//import com.arialyy.aria.core.manager.TaskWrapperManager;
|
||||||
import com.arialyy.aria.core.queue.DownloadTaskQueue; |
//import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||||
import com.arialyy.aria.orm.DbEntity; |
//import com.arialyy.aria.orm.DbEntity;
|
||||||
import com.arialyy.aria.util.ALog; |
//import com.arialyy.aria.util.ALog;
|
||||||
import com.arialyy.aria.util.CommonUtil; |
//import com.arialyy.aria.util.CommonUtil;
|
||||||
import java.io.File; |
//import java.io.File;
|
||||||
|
//
|
||||||
/** |
///**
|
||||||
* Created by Aria.Lao on 2017/7/26. |
// * Created by Aria.Lao on 2017/7/26.
|
||||||
*/ |
// */
|
||||||
abstract class BaseNormalTarget<TARGET extends BaseNormalTarget> |
//abstract class BaseNormalTarget<TARGET extends BaseNormalTarget>
|
||||||
extends AbsDownloadTarget<TARGET, DownloadEntity, DTaskWrapper> { |
// extends AbsDownloadTarget<TARGET, DownloadEntity, DTaskWrapper> {
|
||||||
|
//
|
||||||
/** |
// /**
|
||||||
* 资源地址 |
// * 通过地址初始化target
|
||||||
*/ |
// */
|
||||||
protected String url, newUrl; |
// void initTarget(String url, String targetName) {
|
||||||
|
// setUrl(url);
|
||||||
/** |
// setTargetName(targetName);
|
||||||
* 通过地址初始化target |
// setTaskWrapper(TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url));
|
||||||
*/ |
// mEntity = getTaskWrapper().getEntity();
|
||||||
void initTarget(String url, String targetName) { |
//
|
||||||
this.url = url; |
// if (mEntity != null) {
|
||||||
mTargetName = targetName; |
// setTempFilePath(mEntity.getDownloadPath());
|
||||||
mTaskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); |
// }
|
||||||
mEntity = mTaskWrapper.getEntity(); |
// }
|
||||||
|
//
|
||||||
if (mEntity != null) { |
// /**
|
||||||
mTempFilePath = mEntity.getDownloadPath(); |
// * 更新下载url
|
||||||
} |
// *
|
||||||
} |
// * @param newUrl 新的下载url
|
||||||
|
// */
|
||||||
/** |
// public TARGET updateUrl(String newUrl) {
|
||||||
* 更新下载url |
// if (TextUtils.isEmpty(newUrl)) {
|
||||||
* |
// ALog.e(TAG, "下载url更新失败,newUrl为null");
|
||||||
* @param newUrl 新的下载url |
// return (TARGET) this;
|
||||||
*/ |
// }
|
||||||
public TARGET updateUrl(String newUrl) { |
// if (getUrl().equals(newUrl)) {
|
||||||
if (TextUtils.isEmpty(newUrl)) { |
// ALog.e(TAG, "下载url更新失败,新的下载url和旧的url一致");
|
||||||
ALog.e(TAG, "下载url更新失败,newUrl为null"); |
// return (TARGET) this;
|
||||||
return (TARGET) this; |
// }
|
||||||
} |
// setNewUrl(newUrl);
|
||||||
if (url.equals(newUrl)) { |
// getTaskWrapper().setRefreshInfo(true);
|
||||||
ALog.e(TAG, "下载url更新失败,新的下载url和旧的url一致"); |
// return (TARGET) this;
|
||||||
return (TARGET) this; |
// }
|
||||||
} |
//
|
||||||
this.newUrl = newUrl; |
// /**
|
||||||
mTaskWrapper.setRefreshInfo(true); |
// * 将任务设置为最高优先级任务,最高优先级任务有以下特点:
|
||||||
return (TARGET) this; |
// * 1、在下载队列中,有且只有一个最高优先级任务
|
||||||
} |
// * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成
|
||||||
|
// * 3、任务调度器不会暂停最高优先级任务
|
||||||
/** |
// * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效
|
||||||
* 将任务设置为最高优先级任务,最高优先级任务有以下特点: 1、在下载队列中,有且只有一个最高优先级任务 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 |
// * 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行
|
||||||
* 3、任务调度器不会暂停最高优先级任务 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 5、如果下载队列中已经满了,则会停止队尾的任务,当高优先级任务完成后,该队尾任务将自动执行 |
// * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务
|
||||||
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 |
// */
|
||||||
*/ |
// @Override public void setHighestPriority() {
|
||||||
@Override public void setHighestPriority() { |
// super.setHighestPriority();
|
||||||
super.setHighestPriority(); |
// }
|
||||||
} |
//
|
||||||
|
// /**
|
||||||
/** |
// * 下载任务是否存在
|
||||||
* 下载任务是否存在 |
// *
|
||||||
* |
// * @return {@code true}任务存在
|
||||||
* @return {@code true}任务存在 |
// */
|
||||||
*/ |
// @Override public boolean taskExists() {
|
||||||
@Override public boolean taskExists() { |
// return DbEntity.checkDataExist(DownloadEntity.class, "url=?", getUrl());
|
||||||
return DbEntity.checkDataExist(DownloadEntity.class, "url=?", url); |
// }
|
||||||
} |
//
|
||||||
|
// /**
|
||||||
/** |
// * 获取下载实体
|
||||||
* 获取下载实体 |
// */
|
||||||
*/ |
// public DownloadEntity getDownloadEntity() {
|
||||||
public DownloadEntity getDownloadEntity() { |
// return mEntity;
|
||||||
return mEntity; |
// }
|
||||||
} |
//
|
||||||
|
// /**
|
||||||
/** |
// * 是否在下载
|
||||||
* 是否在下载,该api后续版本会删除 |
// *
|
||||||
* |
// * @return {@code true}任务正在下载
|
||||||
* @deprecated {@link #isRunning()} |
// */
|
||||||
*/ |
// @Override public boolean isRunning() {
|
||||||
@Deprecated public boolean isDownloading() { |
// DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey());
|
||||||
return isRunning(); |
// return task != null && task.isRunning();
|
||||||
} |
// }
|
||||||
|
//
|
||||||
/** |
// /**
|
||||||
* 是否在下载 |
// * 检查下载实体,判断实体是否合法 合法标准为:
|
||||||
* |
// * 1、下载路径不为null,并且下载路径是正常的http或ftp路径
|
||||||
* @return {@code true}任务正在下载 |
// * 2、保存路径不为null,并且保存路径是android文件系统路径
|
||||||
*/ |
// * 3、保存路径不能重复
|
||||||
@Override public boolean isRunning() { |
// *
|
||||||
DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey()); |
// * @return {@code true}合法
|
||||||
return task != null && task.isRunning(); |
// */
|
||||||
} |
// @Override protected boolean checkEntity() {
|
||||||
|
// boolean b = getTargetType() < GROUP_HTTP && checkUrl() && checkFilePath();
|
||||||
/** |
// if (b) {
|
||||||
* 检查下载实体,判断实体是否合法 合法标准为: 1、下载路径不为null,并且下载路径是正常的http或ftp路径 2、保存路径不为null,并且保存路径是android文件系统路径 |
// mEntity.save();
|
||||||
* 3、保存路径不能重复 |
// }
|
||||||
* |
// return b;
|
||||||
* @return {@code true}合法 |
// }
|
||||||
*/ |
//
|
||||||
@Override protected boolean checkEntity() { |
// /**
|
||||||
boolean b = getTargetType() < GROUP_HTTP && checkUrl() && checkFilePath(); |
// * 检查并设置普通任务的文件保存路径
|
||||||
if (b) { |
// *
|
||||||
mEntity.save(); |
// * @return {@code true}保存路径合法
|
||||||
} |
// */
|
||||||
return b; |
// private boolean checkFilePath() {
|
||||||
} |
// String filePath = getTempFilePath();
|
||||||
|
// if (TextUtils.isEmpty(filePath)) {
|
||||||
/** |
// ALog.e(TAG, "下载失败,文件保存路径为null");
|
||||||
* 检查并设置普通任务的文件保存路径 |
// return false;
|
||||||
* |
// } else if (!filePath.startsWith("/")) {
|
||||||
* @return {@code true}保存路径合法 |
// ALog.e(TAG, "下载失败,文件保存路径【" + filePath + "】错误");
|
||||||
*/ |
// return false;
|
||||||
private boolean checkFilePath() { |
// }
|
||||||
String filePath = mTempFilePath; |
// File file = new File(filePath);
|
||||||
if (TextUtils.isEmpty(filePath)) { |
// if (file.isDirectory()) {
|
||||||
ALog.e(TAG, "下载失败,文件保存路径为null"); |
// if (getTargetType() == HTTP) {
|
||||||
return false; |
// ALog.e(TAG, "下载失败,保存路径【" + filePath + "】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip");
|
||||||
} else if (!filePath.startsWith("/")) { |
// return false;
|
||||||
ALog.e(TAG, "下载失败,文件保存路径【" + filePath + "】错误"); |
// } else if (getTargetType() == FTP) {
|
||||||
return false; |
// filePath += mEntity.getFileName();
|
||||||
} |
// }
|
||||||
File file = new File(filePath); |
// } else {
|
||||||
if (file.isDirectory()) { |
// // http文件名设置
|
||||||
if (getTargetType() == HTTP) { |
// if (TextUtils.isEmpty(mEntity.getFileName())) {
|
||||||
ALog.e(TAG, "下载失败,保存路径【" + filePath + "】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip"); |
// mEntity.setFileName(file.getName());
|
||||||
return false; |
// }
|
||||||
} else if (getTargetType() == FTP) { |
// }
|
||||||
filePath += mEntity.getFileName(); |
//
|
||||||
} |
// //设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
|
||||||
} else { |
// if (!filePath.equals(mEntity.getDownloadPath())) {
|
||||||
// http文件名设置
|
// // 检查路径冲突
|
||||||
if (TextUtils.isEmpty(mEntity.getFileName())) { |
// if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) {
|
||||||
mEntity.setFileName(file.getName()); |
// if (!isForceDownload()) {
|
||||||
} |
// ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径");
|
||||||
} |
// return false;
|
||||||
|
// } else {
|
||||||
//设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
|
// ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件");
|
||||||
if (!filePath.equals(mEntity.getDownloadPath())) { |
// CommonUtil.delTaskRecord(filePath, 1);
|
||||||
// 检查路径冲突
|
// setTaskWrapper(
|
||||||
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { |
// TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, getUrl()));
|
||||||
if (!forceDownload) { |
// }
|
||||||
ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径"); |
// }
|
||||||
return false; |
// File oldFile = new File(mEntity.getDownloadPath());
|
||||||
} else { |
// File newFile = new File(filePath);
|
||||||
ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); |
// mEntity.setDownloadPath(filePath);
|
||||||
CommonUtil.delTaskRecord(filePath, 1); |
// mEntity.setFileName(newFile.getName());
|
||||||
mTaskWrapper = |
// if (oldFile.exists()) {
|
||||||
TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); |
// oldFile.renameTo(newFile);
|
||||||
} |
// CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath());
|
||||||
} |
// }
|
||||||
File oldFile = new File(mEntity.getDownloadPath()); |
// }
|
||||||
File newFile = new File(filePath); |
// return true;
|
||||||
mEntity.setDownloadPath(filePath); |
// }
|
||||||
mEntity.setFileName(newFile.getName()); |
//
|
||||||
if (oldFile.exists()) { |
// /**
|
||||||
oldFile.renameTo(newFile); |
// * 检查普通任务的下载地址
|
||||||
CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); |
// *
|
||||||
} |
// * @return {@code true}地址合法
|
||||||
} |
// */
|
||||||
return true; |
// private boolean checkUrl() {
|
||||||
} |
// final String url = mEntity.getUrl();
|
||||||
|
// if (TextUtils.isEmpty(url)) {
|
||||||
/** |
// ALog.e(TAG, "下载失败,url为null");
|
||||||
* 检查普通任务的下载地址 |
// return false;
|
||||||
* |
// } else if (!url.startsWith("http") && !url.startsWith("ftp")) {
|
||||||
* @return {@code true}地址合法 |
// ALog.e(TAG, "下载失败,url【" + url + "】错误");
|
||||||
*/ |
// return false;
|
||||||
private boolean checkUrl() { |
// }
|
||||||
final String url = mEntity.getUrl(); |
// int index = url.indexOf("://");
|
||||||
if (TextUtils.isEmpty(url)) { |
// if (index == -1) {
|
||||||
ALog.e(TAG, "下载失败,url为null"); |
// ALog.e(TAG, "下载失败,url【" + url + "】不合法");
|
||||||
return false; |
// return false;
|
||||||
} else if (!url.startsWith("http") && !url.startsWith("ftp")) { |
// }
|
||||||
ALog.e(TAG, "下载失败,url【" + url + "】错误"); |
// if (!TextUtils.isEmpty(getNewUrl())) {
|
||||||
return false; |
// mEntity.setUrl(getNewUrl());
|
||||||
} |
// }
|
||||||
int index = url.indexOf("://"); |
// return true;
|
||||||
if (index == -1) { |
// }
|
||||||
ALog.e(TAG, "下载失败,url【" + url + "】不合法"); |
//}
|
||||||
return false; |
|
||||||
} |
|
||||||
if (!TextUtils.isEmpty(newUrl)) { |
|
||||||
mEntity.setUrl(newUrl); |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
} |
|
||||||
|
@ -0,0 +1,161 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.inf.ITargetHandler; |
||||||
|
import com.arialyy.aria.core.inf.ITargetNormal; |
||||||
|
import com.arialyy.aria.core.manager.TaskWrapperManager; |
||||||
|
import com.arialyy.aria.core.queue.DownloadTaskQueue; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by AriaL on 2019/4/5. |
||||||
|
* 普通下载任务通用功能处理 |
||||||
|
*/ |
||||||
|
class DNormalDelegate<TARGET extends AbsDownloadTarget> implements ITargetNormal<TARGET> { |
||||||
|
private final String TAG = "DNormalDelegate"; |
||||||
|
private DownloadEntity mEntity; |
||||||
|
|
||||||
|
private TARGET target; |
||||||
|
|
||||||
|
DNormalDelegate(TARGET target, String url, String targetName) { |
||||||
|
this.target = target; |
||||||
|
initTarget(url, targetName); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void initTarget(String url, String targetName) { |
||||||
|
DTaskWrapper taskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); |
||||||
|
mEntity = taskWrapper.getEntity(); |
||||||
|
|
||||||
|
target.setUrl(url); |
||||||
|
target.setTargetName(targetName); |
||||||
|
target.setTaskWrapper(taskWrapper); |
||||||
|
target.setEntity(mEntity); |
||||||
|
if (mEntity != null) { |
||||||
|
target.setTempFilePath(mEntity.getDownloadPath()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public TARGET updateUrl(String newUrl) { |
||||||
|
if (TextUtils.isEmpty(newUrl)) { |
||||||
|
ALog.e(TAG, "下载url更新失败,newUrl为null"); |
||||||
|
return target; |
||||||
|
} |
||||||
|
if (target.getUrl().equals(newUrl)) { |
||||||
|
ALog.e(TAG, "下载url更新失败,新的下载url和旧的url一致"); |
||||||
|
return target; |
||||||
|
} |
||||||
|
target.setNewUrl(newUrl); |
||||||
|
target.getTaskWrapper().setRefreshInfo(true); |
||||||
|
return target; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public DownloadEntity getEntity() { |
||||||
|
return target.getEntity(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return DbEntity.checkDataExist(DownloadEntity.class, "url=?", target.getUrl()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey()); |
||||||
|
return task != null && task.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean checkEntity() { |
||||||
|
boolean b = checkUrl() && checkFilePath(); |
||||||
|
if (b) { |
||||||
|
mEntity.save(); |
||||||
|
} |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean checkFilePath() { |
||||||
|
String filePath = target.getTempFilePath(); |
||||||
|
if (TextUtils.isEmpty(filePath)) { |
||||||
|
ALog.e(TAG, "下载失败,文件保存路径为null"); |
||||||
|
return false; |
||||||
|
} else if (!filePath.startsWith("/")) { |
||||||
|
ALog.e(TAG, "下载失败,文件保存路径【" + filePath + "】错误"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
File file = new File(filePath); |
||||||
|
if (file.isDirectory()) { |
||||||
|
if (target.getTargetType() == ITargetHandler.HTTP) { |
||||||
|
ALog.e(TAG, "下载失败,保存路径【" + filePath + "】不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip"); |
||||||
|
return false; |
||||||
|
} else if (target.getTargetType() == ITargetHandler.FTP) { |
||||||
|
filePath += mEntity.getFileName(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
// http文件名设置
|
||||||
|
if (TextUtils.isEmpty(mEntity.getFileName())) { |
||||||
|
mEntity.setFileName(file.getName()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
|
||||||
|
if (!filePath.equals(mEntity.getDownloadPath())) { |
||||||
|
// 检查路径冲突
|
||||||
|
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { |
||||||
|
if (!target.isForceDownload()) { |
||||||
|
ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径"); |
||||||
|
return false; |
||||||
|
} else { |
||||||
|
ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); |
||||||
|
CommonUtil.delTaskRecord(filePath, 1); |
||||||
|
target.setTaskWrapper( |
||||||
|
TaskWrapperManager.getInstance() |
||||||
|
.getHttpTaskWrapper(DTaskWrapper.class, target.getUrl())); |
||||||
|
} |
||||||
|
} |
||||||
|
File oldFile = new File(mEntity.getDownloadPath()); |
||||||
|
File newFile = new File(filePath); |
||||||
|
mEntity.setDownloadPath(filePath); |
||||||
|
mEntity.setFileName(newFile.getName()); |
||||||
|
if (oldFile.exists()) { |
||||||
|
oldFile.renameTo(newFile); |
||||||
|
CommonUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean checkUrl() { |
||||||
|
final String url = mEntity.getUrl(); |
||||||
|
if (TextUtils.isEmpty(url)) { |
||||||
|
ALog.e(TAG, "下载失败,url为null"); |
||||||
|
return false; |
||||||
|
} else if (!url.startsWith("http") && !url.startsWith("ftp")) { |
||||||
|
ALog.e(TAG, "下载失败,url【" + url + "】错误"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
int index = url.indexOf("://"); |
||||||
|
if (index == -1) { |
||||||
|
ALog.e(TAG, "下载失败,url【" + url + "】不合法"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (!TextUtils.isEmpty(target.getNewUrl())) { |
||||||
|
mEntity.setUrl(target.getNewUrl()); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,81 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.inf; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2019/4/5. |
||||||
|
* 普通任务接收器功能接口 |
||||||
|
*/ |
||||||
|
public interface ITargetNormal<TARGET extends AbsTarget> { |
||||||
|
|
||||||
|
/** |
||||||
|
* 通过地址初始化target |
||||||
|
* |
||||||
|
* @param url 下载url、上传url |
||||||
|
* @param targetName 接收器名称 |
||||||
|
*/ |
||||||
|
void initTarget(String url, String targetName); |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新下载url |
||||||
|
* |
||||||
|
* @param newUrl 新的下载url |
||||||
|
*/ |
||||||
|
TARGET updateUrl(String newUrl); |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取实体 |
||||||
|
*/ |
||||||
|
AbsEntity getEntity(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务是否存在 |
||||||
|
* |
||||||
|
* @return {@code true}任务存在,{@code false} 任务不存在 |
||||||
|
*/ |
||||||
|
boolean taskExists(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 任务是否在执行 |
||||||
|
* |
||||||
|
* @return {@code true} 任务正在执行,{@code false} 任务没有执行 |
||||||
|
*/ |
||||||
|
boolean isRunning(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查下载实体,判断实体是否合法 合法标准为: |
||||||
|
* 1、下载路径不为null,并且下载路径是正常的http或ftp路径 |
||||||
|
* 2、保存路径不为null,并且保存路径是android文件系统路径 |
||||||
|
* 3、保存路径不能重复 |
||||||
|
* |
||||||
|
* @return {@code true}合法 |
||||||
|
*/ |
||||||
|
boolean checkEntity(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查并设置普通任务的文件保存路径 |
||||||
|
* |
||||||
|
* @return {@code true}保存路径合法 |
||||||
|
*/ |
||||||
|
boolean checkFilePath(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查普通任务的下载地址 |
||||||
|
* |
||||||
|
* @return {@code true}地址合法 |
||||||
|
*/ |
||||||
|
boolean checkUrl(); |
||||||
|
} |
@ -1,146 +1,146 @@ |
|||||||
/* |
///*
|
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
// * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
* |
// *
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
// * Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License. |
// * you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at |
// * You may obtain a copy of the License at
|
||||||
* |
// *
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
// * http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
// *
|
||||||
* Unless required by applicable law or agreed to in writing, software |
// * Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
// * distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and |
// * See the License for the specific language governing permissions and
|
||||||
* limitations under the License. |
// * limitations under the License.
|
||||||
*/ |
// */
|
||||||
package com.arialyy.aria.core.upload; |
//package com.arialyy.aria.core.upload;
|
||||||
|
//
|
||||||
import android.support.annotation.CheckResult; |
//import android.support.annotation.CheckResult;
|
||||||
import android.support.annotation.NonNull; |
//import android.support.annotation.NonNull;
|
||||||
import android.text.TextUtils; |
//import android.text.TextUtils;
|
||||||
import com.arialyy.aria.core.manager.TaskWrapperManager; |
//import com.arialyy.aria.core.manager.TaskWrapperManager;
|
||||||
import com.arialyy.aria.core.queue.UploadTaskQueue; |
//import com.arialyy.aria.core.queue.UploadTaskQueue;
|
||||||
import com.arialyy.aria.orm.DbEntity; |
//import com.arialyy.aria.orm.DbEntity;
|
||||||
import com.arialyy.aria.util.ALog; |
//import com.arialyy.aria.util.ALog;
|
||||||
import java.io.File; |
//import java.io.File;
|
||||||
|
//
|
||||||
/** |
///**
|
||||||
* Created by AriaL on 2018/3/9. |
// * Created by AriaL on 2018/3/9.
|
||||||
*/ |
// */
|
||||||
abstract class BaseNormalTarget<TARGET extends AbsUploadTarget> |
//abstract class BaseNormalTarget<TARGET extends AbsUploadTarget>
|
||||||
extends AbsUploadTarget<TARGET, UploadEntity, UTaskWrapper> { |
// extends AbsUploadTarget<TARGET, UploadEntity, UTaskWrapper> {
|
||||||
|
//
|
||||||
protected String mTempUrl; |
// protected String mTempUrl;
|
||||||
|
//
|
||||||
void initTarget(String filePath) { |
// void initTarget(String filePath) {
|
||||||
mTaskWrapper = |
// mTaskWrapper =
|
||||||
TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath); |
// TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath);
|
||||||
mEntity = mTaskWrapper.getEntity(); |
// mEntity = mTaskWrapper.getEntity();
|
||||||
File file = new File(filePath); |
// File file = new File(filePath);
|
||||||
mEntity.setFileName(file.getName()); |
// mEntity.setFileName(file.getName());
|
||||||
mEntity.setFileSize(file.length()); |
// mEntity.setFileSize(file.length());
|
||||||
mTempUrl = mEntity.getUrl(); |
// mTempUrl = mEntity.getUrl();
|
||||||
} |
// }
|
||||||
|
//
|
||||||
/** |
// /**
|
||||||
* 设置上传路径 |
// * 设置上传路径
|
||||||
* |
// *
|
||||||
* @param uploadUrl 上传路径 |
// * @param uploadUrl 上传路径
|
||||||
*/ |
// */
|
||||||
@CheckResult |
// @CheckResult
|
||||||
public TARGET setUploadUrl(@NonNull String uploadUrl) { |
// public TARGET setTempUrl(@NonNull String uploadUrl) {
|
||||||
mTempUrl = uploadUrl; |
// mTempUrl = uploadUrl;
|
||||||
return (TARGET) this; |
// return (TARGET) this;
|
||||||
} |
// }
|
||||||
|
//
|
||||||
/** |
// /**
|
||||||
* 上传任务是否存在 |
// * 上传任务是否存在
|
||||||
* |
// *
|
||||||
* @return {@code true}存在 |
// * @return {@code true}存在
|
||||||
*/ |
// */
|
||||||
@Override public boolean taskExists() { |
// @Override public boolean taskExists() {
|
||||||
return DbEntity.checkDataExist(UploadEntity.class, "key=?", mEntity.getFilePath()); |
// return DbEntity.checkDataExist(UploadEntity.class, "key=?", mEntity.getFilePath());
|
||||||
} |
// }
|
||||||
|
//
|
||||||
/** |
// /**
|
||||||
* 是否在上传 |
// * 是否在上传
|
||||||
* |
// *
|
||||||
* @deprecated {@link #isRunning()} |
// * @deprecated {@link #isRunning()}
|
||||||
*/ |
// */
|
||||||
public boolean isUploading() { |
// public boolean isUploading() {
|
||||||
return isRunning(); |
// return isRunning();
|
||||||
} |
// }
|
||||||
|
//
|
||||||
@Override public boolean isRunning() { |
// @Override public boolean isRunning() {
|
||||||
UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey()); |
// UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey());
|
||||||
return task != null && task.isRunning(); |
// return task != null && task.isRunning();
|
||||||
} |
// }
|
||||||
|
//
|
||||||
@Override protected boolean checkEntity() { |
// @Override protected boolean checkEntity() {
|
||||||
boolean b = checkUrl() && checkFilePath(); |
// boolean b = checkUrl() && checkFilePath();
|
||||||
if (b) { |
// if (b) {
|
||||||
mEntity.save(); |
// mEntity.save();
|
||||||
} |
// }
|
||||||
if (mTaskWrapper.asFtp().getUrlEntity() != null && mTaskWrapper.asFtp().getUrlEntity().isFtps) { |
// if (mTaskWrapper.asFtp().getUrlEntity() != null && mTaskWrapper.asFtp().getUrlEntity().isFtps) {
|
||||||
//if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) {
|
// //if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) {
|
||||||
// ALog.e(TAG, "证书路径为空");
|
// // ALog.e(TAG, "证书路径为空");
|
||||||
|
// // return false;
|
||||||
|
// //}
|
||||||
|
// if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) {
|
||||||
|
// ALog.e(TAG, "证书别名为空");
|
||||||
// return false;
|
// return false;
|
||||||
// }
|
// }
|
||||||
if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) { |
// }
|
||||||
ALog.e(TAG, "证书别名为空"); |
// return b;
|
||||||
return false; |
// }
|
||||||
} |
//
|
||||||
} |
// /**
|
||||||
return b; |
// * 检查上传文件路径是否合法
|
||||||
} |
// *
|
||||||
|
// * @return {@code true} 合法
|
||||||
/** |
// */
|
||||||
* 检查上传文件路径是否合法 |
// private boolean checkFilePath() {
|
||||||
* |
// String filePath = mEntity.getFilePath();
|
||||||
* @return {@code true} 合法 |
// if (TextUtils.isEmpty(filePath)) {
|
||||||
*/ |
// ALog.e(TAG, "上传失败,文件路径为null");
|
||||||
private boolean checkFilePath() { |
// return false;
|
||||||
String filePath = mEntity.getFilePath(); |
// } else if (!filePath.startsWith("/")) {
|
||||||
if (TextUtils.isEmpty(filePath)) { |
// ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法");
|
||||||
ALog.e(TAG, "上传失败,文件路径为null"); |
// return false;
|
||||||
return false; |
// }
|
||||||
} else if (!filePath.startsWith("/")) { |
//
|
||||||
ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法"); |
// File file = new File(mEntity.getFilePath());
|
||||||
return false; |
// if (!file.exists()) {
|
||||||
} |
// ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在");
|
||||||
|
// return false;
|
||||||
File file = new File(mEntity.getFilePath()); |
// }
|
||||||
if (!file.exists()) { |
// if (file.isDirectory()) {
|
||||||
ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在"); |
// ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹");
|
||||||
return false; |
// return false;
|
||||||
} |
// }
|
||||||
if (file.isDirectory()) { |
// return true;
|
||||||
ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹"); |
// }
|
||||||
return false; |
//
|
||||||
} |
// /**
|
||||||
return true; |
// * 检查普通任务的下载地址
|
||||||
} |
// *
|
||||||
|
// * @return {@code true}地址合法
|
||||||
/** |
// */
|
||||||
* 检查普通任务的下载地址 |
// protected boolean checkUrl() {
|
||||||
* |
// final String url = mTempUrl;
|
||||||
* @return {@code true}地址合法 |
// if (TextUtils.isEmpty(url)) {
|
||||||
*/ |
// ALog.e(TAG, "上传失败,url为null");
|
||||||
protected boolean checkUrl() { |
// return false;
|
||||||
final String url = mTempUrl; |
// } else if (!url.startsWith("http") && !url.startsWith("ftp")) {
|
||||||
if (TextUtils.isEmpty(url)) { |
// ALog.e(TAG, "上传失败,url【" + url + "】错误");
|
||||||
ALog.e(TAG, "上传失败,url为null"); |
// return false;
|
||||||
return false; |
// }
|
||||||
} else if (!url.startsWith("http") && !url.startsWith("ftp")) { |
// int index = url.indexOf("://");
|
||||||
ALog.e(TAG, "上传失败,url【" + url + "】错误"); |
// if (index == -1) {
|
||||||
return false; |
// ALog.e(TAG, "上传失败,url【" + url + "】不合法");
|
||||||
} |
// return false;
|
||||||
int index = url.indexOf("://"); |
// }
|
||||||
if (index == -1) { |
// mEntity.setUrl(url);
|
||||||
ALog.e(TAG, "上传失败,url【" + url + "】不合法"); |
// return true;
|
||||||
return false; |
// }
|
||||||
} |
//}
|
||||||
mEntity.setUrl(url); |
|
||||||
return true; |
|
||||||
} |
|
||||||
} |
|
||||||
|
@ -0,0 +1,132 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.arialyy.aria.core.upload; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.inf.AbsEntity; |
||||||
|
import com.arialyy.aria.core.inf.ITargetNormal; |
||||||
|
import com.arialyy.aria.core.manager.TaskWrapperManager; |
||||||
|
import com.arialyy.aria.core.queue.UploadTaskQueue; |
||||||
|
import com.arialyy.aria.orm.DbEntity; |
||||||
|
import com.arialyy.aria.util.ALog; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2019/4/5. |
||||||
|
* 普通上传任务通用功能处理 |
||||||
|
*/ |
||||||
|
public class UNormalDelegate<TARGET extends AbsUploadTarget> implements ITargetNormal<TARGET> { |
||||||
|
private String TAG = "UNormalDelegate"; |
||||||
|
private UploadEntity mEntity; |
||||||
|
private TARGET mTarget; |
||||||
|
|
||||||
|
UNormalDelegate(TARGET target, String filePath, String targetName) { |
||||||
|
mTarget = target; |
||||||
|
initTarget(filePath, targetName); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void initTarget(String filePath, String targetName) { |
||||||
|
UTaskWrapper taskWrapper = |
||||||
|
TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath); |
||||||
|
mEntity = taskWrapper.getEntity(); |
||||||
|
File file = new File(filePath); |
||||||
|
mEntity.setFileName(file.getName()); |
||||||
|
mEntity.setFileSize(file.length()); |
||||||
|
mTarget.setTargetName(targetName); |
||||||
|
mTarget.setTaskWrapper(taskWrapper); |
||||||
|
mTarget.setEntity(mEntity); |
||||||
|
mTarget.setTempUrl(mEntity.getUrl()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public TARGET updateUrl(String newUrl) { |
||||||
|
mTarget.setTempUrl(mEntity.getUrl()); |
||||||
|
return mTarget; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public AbsEntity getEntity() { |
||||||
|
return mEntity; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean taskExists() { |
||||||
|
return DbEntity.checkDataExist(UploadEntity.class, "key=?", mEntity.getFilePath()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean isRunning() { |
||||||
|
UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey()); |
||||||
|
return task != null && task.isRunning(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean checkEntity() { |
||||||
|
boolean b = checkUrl() && checkFilePath(); |
||||||
|
if (b) { |
||||||
|
mEntity.save(); |
||||||
|
} |
||||||
|
if (mTarget.getTaskWrapper().asFtp().getUrlEntity() != null && mTarget.getTaskWrapper() |
||||||
|
.asFtp() |
||||||
|
.getUrlEntity().isFtps) { |
||||||
|
//if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) {
|
||||||
|
// ALog.e(TAG, "证书路径为空");
|
||||||
|
// return false;
|
||||||
|
//}
|
||||||
|
if (TextUtils.isEmpty(mTarget.getTaskWrapper().asFtp().getUrlEntity().keyAlias)) { |
||||||
|
ALog.e(TAG, "证书别名为空"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean checkFilePath() { |
||||||
|
String filePath = mEntity.getFilePath(); |
||||||
|
if (TextUtils.isEmpty(filePath)) { |
||||||
|
ALog.e(TAG, "上传失败,文件路径为null"); |
||||||
|
return false; |
||||||
|
} else if (!filePath.startsWith("/")) { |
||||||
|
ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
File file = new File(mEntity.getFilePath()); |
||||||
|
if (!file.exists()) { |
||||||
|
ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (file.isDirectory()) { |
||||||
|
ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public boolean checkUrl() { |
||||||
|
|
||||||
|
final String url = mTarget.getTempUrl(); |
||||||
|
if (TextUtils.isEmpty(url)) { |
||||||
|
ALog.e(TAG, "上传失败,url为null"); |
||||||
|
return false; |
||||||
|
} else if (!url.startsWith("http") && !url.startsWith("ftp")) { |
||||||
|
ALog.e(TAG, "上传失败,url【" + url + "】错误"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
int index = url.indexOf("://"); |
||||||
|
if (index == -1) { |
||||||
|
ALog.e(TAG, "上传失败,url【" + url + "】不合法"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
mEntity.setUrl(url); |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue