优化target代码

v3.6.6
laoyuyu 6 years ago
parent 598c863559
commit 30c41e4cb2
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  2. 8
      Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java
  3. 4
      Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskDelegate.java
  4. 8
      Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java
  5. 4
      Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskDelegate.java
  6. 103
      Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java
  7. 7
      Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java
  8. 406
      Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java
  9. 161
      Aria/src/main/java/com/arialyy/aria/core/download/DNormalDelegate.java
  10. 24
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  11. 34
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  12. 24
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java
  13. 68
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java
  14. 46
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  15. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java
  16. 4
      Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java
  17. 15
      Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHandler.java
  18. 5
      Aria/src/main/java/com/arialyy/aria/core/inf/ITargetHeadDelegate.java
  19. 81
      Aria/src/main/java/com/arialyy/aria/core/inf/ITargetNormal.java
  20. 5
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  21. 27
      Aria/src/main/java/com/arialyy/aria/core/upload/AbsUploadTarget.java
  22. 292
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java
  23. 95
      Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java
  24. 132
      Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java
  25. 57
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  26. 2
      app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java

@ -49,7 +49,7 @@ import com.arialyy.aria.util.ALog;
* //上传 * //上传
* Aria.upload(this) * Aria.upload(this)
* .load(filePath) //文件路径,必填 * .load(filePath) //文件路径,必填
* .setUploadUrl(uploadUrl) //上传路径,必填 * .setTempUrl(uploadUrl) //上传路径,必填
* .setAttachment(fileKey) //服务器读取文件的key,必填 * .setAttachment(fileKey) //服务器读取文件的key,必填
* .start(); * .start();
* </code> * </code>

@ -19,12 +19,12 @@ import android.text.TextUtils;
import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.ProtocolType; import com.arialyy.aria.core.common.ProtocolType;
import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.ITarget; import com.arialyy.aria.core.inf.ITargetHandler;
/** /**
* FTP SSL/TSL 参数委托 * FTP SSL/TSL 参数委托
*/ */
public class FTPSDelegate<TARGET extends AbsTarget> implements ITarget { public class FTPSDelegate<TARGET extends AbsTarget> implements ITargetHandler {
private final String TAG = "FTPSDelegate"; private final String TAG = "FTPSDelegate";
private TARGET mTarget; private TARGET mTarget;
private FtpUrlEntity mUrlEntity; private FtpUrlEntity mUrlEntity;
@ -86,6 +86,10 @@ public class FTPSDelegate<TARGET extends AbsTarget> implements ITarget {
return this; return this;
} }
@Override public void add() {
mTarget.add();
}
@Override public void start() { @Override public void start() {
mTarget.start(); mTarget.start();
} }

@ -16,13 +16,13 @@
package com.arialyy.aria.core.common.ftp; package com.arialyy.aria.core.common.ftp;
import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.inf.ITaskDelegate; import com.arialyy.aria.core.inf.ITargetHeadDelegate;
import java.net.Proxy; import java.net.Proxy;
/** /**
* fTP任务设置的信息用户名密码端口等信息 * fTP任务设置的信息用户名密码端口等信息
*/ */
public class FtpTaskDelegate implements ITaskDelegate { public class FtpTaskDelegate implements ITargetHeadDelegate {
/** /**
* 账号和密码 * 账号和密码

@ -20,7 +20,7 @@ import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadGroupTarget; import com.arialyy.aria.core.download.DownloadGroupTarget;
import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.ITarget; import com.arialyy.aria.core.inf.ITargetHandler;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -29,7 +29,7 @@ import java.util.Map;
* HTTP参数委托 * HTTP参数委托
* @param <TARGET> * @param <TARGET>
*/ */
class HttpDelegate<TARGET extends AbsTarget> implements ITarget { class HttpDelegate<TARGET extends AbsTarget> implements ITargetHandler {
private static final String TAG = "PostDelegate"; private static final String TAG = "PostDelegate";
TARGET mTarget; TARGET mTarget;
@ -66,6 +66,10 @@ class HttpDelegate<TARGET extends AbsTarget> implements ITarget {
return mTarget; return mTarget;
} }
@Override public void add() {
mTarget.add();
}
@Override public void start() { @Override public void start() {
mTarget.start(); mTarget.start();
} }

@ -17,7 +17,7 @@
package com.arialyy.aria.core.common.http; package com.arialyy.aria.core.common.http;
import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.inf.ITaskDelegate; import com.arialyy.aria.core.inf.ITargetHeadDelegate;
import java.net.CookieManager; import java.net.CookieManager;
import java.net.Proxy; import java.net.Proxy;
import java.util.HashMap; import java.util.HashMap;
@ -26,7 +26,7 @@ import java.util.Map;
/** /**
* Http任务设置的信息cookie请求参数 * Http任务设置的信息cookie请求参数
*/ */
public class HttpTaskDelegate implements ITaskDelegate { public class HttpTaskDelegate implements ITargetHeadDelegate {
private CookieManager cookieManager; private CookieManager cookieManager;

@ -17,33 +17,35 @@ package com.arialyy.aria.core.download;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
/** /**
* Created by lyy on 2017/2/28. * Created by lyy on 2017/2/28.
*/ */
abstract class AbsDownloadTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskWrapper> abstract class AbsDownloadTarget<TARGET extends AbsDownloadTarget>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> { extends AbsTarget<TARGET, DownloadEntity, DTaskWrapper> {
static final int HTTP = 1;
static final int FTP = 2;
//HTTP任务组
static final int GROUP_HTTP = 3;
//FTP文件夹
static final int GROUP_FTP_DIR = 4;
/** /**
* 设置的文件保存路径的临时变量 * 设置的文件保存路径的临时变量
*/ */
String mTempFilePath; private String mTempFilePath;
/** /**
* {@code true}强制下载不考虑文件路径是否被占用 * {@code true}强制下载不考虑文件路径是否被占用
*/ */
boolean forceDownload = false; private boolean forceDownload = false;
/**
* 资源地址
*/
private String mUrl, mNewUrl;
/**
* 更新下载url
*
* @param newUrl 新的下载url
*/
public abstract TARGET updateUrl(String newUrl);
/** /**
* 将任务设置为最高优先级任务最高优先级任务有以下特点 * 将任务设置为最高优先级任务最高优先级任务有以下特点
@ -54,49 +56,66 @@ abstract class AbsDownloadTarget<TARGET extends AbsTarget, ENTITY extends AbsEnt
* 5如果下载队列中已经满了则会停止队尾的任务当高优先级任务完成后该队尾任务将自动执行 * 5如果下载队列中已经满了则会停止队尾的任务当高优先级任务完成后该队尾任务将自动执行
* 6把任务设置为最高优先级任务后将自动执行任务不需要重新调用start()启动任务 * 6把任务设置为最高优先级任务后将自动执行任务不需要重新调用start()启动任务
*/ */
protected void setHighestPriority() { public void setHighestPriority() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_HIGHEST_PRIORITY, .setCmd(
checkTaskType())) CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_HIGHEST_PRIORITY,
checkTaskType()))
.exe(); .exe();
} }
} }
/** /**
* 添加任务 * 是否强制下载文件 {@link DownloadTarget#setFilePath(String, boolean)}
* {@link FtpDownloadTarget#setFilePath(String, boolean)}
*
* @return {@code true} 强制下载文件
*/ */
public void add() { boolean isForceDownload() {
if (checkEntity()) { return forceDownload;
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CREATE,
checkTaskType()))
.exe();
}
} }
/** @Override public void setTaskWrapper(DTaskWrapper taskWrapper) {
* 获取任务文件大小 super.setTaskWrapper(taskWrapper);
*
* @return 文件大小
*/
public long getFileSize() {
return getSize();
} }
/** /**
* 获取单位转换后的文件大小 * 文件保存路径的临时变量
*
* @return 文件大小{@code xxx mb}
*/ */
public String getConvertFileSize() { String getTempFilePath() {
return getConvertSize(); return mTempFilePath;
} }
/** void setForceDownload(boolean forceDownload) {
* 设置target类型 this.forceDownload = forceDownload;
* }
* @return {@link #HTTP}{@link #FTP}{@link #GROUP_HTTP}{@link #GROUP_FTP_DIR}
*/ public String getUrl() {
protected abstract int getTargetType(); return mUrl;
}
void setUrl(String url) {
this.mUrl = url;
}
String getNewUrl() {
return mNewUrl;
}
void setNewUrl(String newUrl) {
this.mNewUrl = newUrl;
}
void setTempFilePath(String mTempFilePath) {
this.mTempFilePath = mTempFilePath;
}
public void setEntity(DownloadEntity entity) {
mEntity = entity;
}
@Override public DownloadEntity getEntity() {
return super.getEntity();
}
} }

@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
import android.support.annotation.CheckResult; import android.support.annotation.CheckResult;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.core.manager.SubTaskManager;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
@ -30,7 +31,7 @@ import java.util.List;
* Created by lyy on 2017/7/26. * Created by lyy on 2017/7/26.
*/ */
abstract class BaseGroupTarget<TARGET extends BaseGroupTarget> abstract class BaseGroupTarget<TARGET extends BaseGroupTarget>
extends AbsDownloadTarget<TARGET, DownloadGroupEntity, DGTaskWrapper> { extends AbsTarget<TARGET, DownloadGroupEntity, DGTaskWrapper> {
/** /**
* 组任务名 * 组任务名
@ -55,7 +56,7 @@ abstract class BaseGroupTarget<TARGET extends BaseGroupTarget>
@CheckResult @CheckResult
public SubTaskManager getSubTaskManager() { public SubTaskManager getSubTaskManager() {
if (mSubTaskManager == null) { if (mSubTaskManager == null) {
mSubTaskManager = new SubTaskManager(mTargetName, mTaskWrapper); mSubTaskManager = new SubTaskManager(getTargetName(), getTaskWrapper());
} }
return mSubTaskManager; return mSubTaskManager;
} }
@ -122,7 +123,7 @@ abstract class BaseGroupTarget<TARGET extends BaseGroupTarget>
* @param newDirPath 新的文件夹路径 * @param newDirPath 新的文件夹路径
*/ */
void reChangeDirPath(String newDirPath) { void reChangeDirPath(String newDirPath) {
List<DTaskWrapper> subTasks = mTaskWrapper.getSubTaskWrapper(); List<DTaskWrapper> subTasks = getTaskWrapper().getSubTaskWrapper();
if (subTasks != null && !subTasks.isEmpty()) { if (subTasks != null && !subTasks.isEmpty()) {
List<DbEntity> des = new ArrayList<>(); List<DbEntity> des = new ArrayList<>();
for (DTaskWrapper dte : subTasks) { for (DTaskWrapper dte : subTasks) {

@ -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;
}
}

@ -52,7 +52,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
private List<String> mSubNameTemp = new ArrayList<>(); private List<String> mSubNameTemp = new ArrayList<>();
public DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) { public DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) {
this.mTargetName = targetName; setTargetName(targetName);
if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) {
this.mUrls.addAll(groupEntity.getUrls()); this.mUrls.addAll(groupEntity.getUrls());
} }
@ -60,15 +60,15 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
} }
DownloadGroupTarget(List<String> urls, String targetName) { DownloadGroupTarget(List<String> urls, String targetName) {
this.mTargetName = targetName; setTargetName(targetName);
this.mUrls = urls; this.mUrls = urls;
init(); init();
} }
private void init() { private void init() {
mGroupHash = CommonUtil.getMd5Code(mUrls); mGroupHash = CommonUtil.getMd5Code(mUrls);
mTaskWrapper = TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, mUrls); setTaskWrapper(TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, mUrls));
mEntity = mTaskWrapper.getEntity(); mEntity = getEntity();
if (mEntity != null) { if (mEntity != null) {
mDirPathTemp = mEntity.getDirPath(); mDirPathTemp = mEntity.getDirPath();
} }
@ -158,7 +158,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
ALog.e(TAG, "修改子任务的文件名失败:列表为null"); ALog.e(TAG, "修改子任务的文件名失败:列表为null");
return this; return this;
} }
if (subTaskFileName.size() != mTaskWrapper.getSubTaskWrapper().size()) { if (subTaskFileName.size() != getTaskWrapper().getSubTaskWrapper().size()) {
ALog.e(TAG, "修改子任务的文件名失败:子任务文件名列表数量和子任务的数量不匹配"); ALog.e(TAG, "修改子任务的文件名失败:子任务文件名列表数量和子任务的数量不匹配");
return this; return this;
} }
@ -167,7 +167,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
return this; return this;
} }
@Override protected int getTargetType() { @Override public int getTargetType() {
return GROUP_HTTP; return GROUP_HTTP;
} }
@ -185,13 +185,13 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
return false; return false;
} }
if (mTaskWrapper.getEntity().getFileSize() == 0) { if (getTaskWrapper().getEntity().getFileSize() == 0) {
ALog.e(TAG, "组合任务必须设置文件文件大小"); ALog.e(TAG, "组合任务必须设置文件文件大小");
return false; return false;
} }
if (mTaskWrapper.asHttp().getRequestEnum() == RequestEnum.POST) { if (getTaskWrapper().asHttp().getRequestEnum() == RequestEnum.POST) {
for (DTaskWrapper subTask : mTaskWrapper.getSubTaskWrapper()) { for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) {
subTask.asHttp().setRequestEnum(RequestEnum.POST); subTask.asHttp().setRequestEnum(RequestEnum.POST);
} }
} }
@ -214,7 +214,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
* 更新所有改动的子任务文件名 * 更新所有改动的子任务文件名
*/ */
private void updateSingleSubFileName() { private void updateSingleSubFileName() {
List<DTaskWrapper> entities = mTaskWrapper.getSubTaskWrapper(); List<DTaskWrapper> entities = getTaskWrapper().getSubTaskWrapper();
int i = 0; int i = 0;
for (DTaskWrapper entity : entities) { for (DTaskWrapper entity : entities) {
if (i < mSubNameTemp.size()) { if (i < mSubNameTemp.size()) {
@ -316,7 +316,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
@CheckResult @CheckResult
@Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) { @Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) {
for (DTaskWrapper subTask : mTaskWrapper.getSubTaskWrapper()) { for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) {
mDelegate.addHeader(subTask, key, value); mDelegate.addHeader(subTask, key, value);
} }
return mDelegate.addHeader(key, value); return mDelegate.addHeader(key, value);
@ -324,7 +324,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
@CheckResult @CheckResult
@Override public DownloadGroupTarget addHeaders(Map<String, String> headers) { @Override public DownloadGroupTarget addHeaders(Map<String, String> headers) {
for (DTaskWrapper subTask : mTaskWrapper.getSubTaskWrapper()) { for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) {
mDelegate.addHeaders(subTask, headers); mDelegate.addHeaders(subTask, headers);
} }
return mDelegate.addHeaders(headers); return mDelegate.addHeaders(headers);

@ -28,16 +28,17 @@ import java.util.Map;
* Created by lyy on 2016/12/5. * Created by lyy on 2016/12/5.
* https://github.com/AriaLyy/Aria * https://github.com/AriaLyy/Aria
*/ */
public class DownloadTarget extends BaseNormalTarget<DownloadTarget> public class DownloadTarget extends AbsDownloadTarget<DownloadTarget>
implements IHttpHeaderDelegate<DownloadTarget> { implements IHttpHeaderDelegate<DownloadTarget> {
private HttpHeaderDelegate<DownloadTarget> mHeaderDelegate; private HttpHeaderDelegate<DownloadTarget> mHeaderDelegate;
private DNormalDelegate<DownloadTarget> mNormalDelegate;
DownloadTarget(DownloadEntity entity, String targetName) { DownloadTarget(DownloadEntity entity, String targetName) {
this(entity.getUrl(), targetName); this(entity.getUrl(), targetName);
} }
DownloadTarget(String url, String targetName) { DownloadTarget(String url, String targetName) {
initTarget(url, targetName); mNormalDelegate = new DNormalDelegate<>(this, url, targetName);
mHeaderDelegate = new HttpHeaderDelegate<>(this); mHeaderDelegate = new HttpHeaderDelegate<>(this);
} }
@ -53,7 +54,7 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
* get参数传递 * get参数传递
*/ */
@CheckResult @CheckResult
public GetDelegate asGet(){ public GetDelegate asGet() {
return new GetDelegate<>(this); return new GetDelegate<>(this);
} }
@ -65,7 +66,7 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
*/ */
@CheckResult @CheckResult
public DownloadTarget useServerFileName(boolean use) { public DownloadTarget useServerFileName(boolean use) {
mTaskWrapper.asHttp().setUseServerFileName(use); getTaskWrapper().asHttp().setUseServerFileName(use);
return this; return this;
} }
@ -90,7 +91,7 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
*/ */
@CheckResult @CheckResult
public DownloadTarget setFilePath(@NonNull String filePath) { public DownloadTarget setFilePath(@NonNull String filePath) {
mTempFilePath = filePath; setTempFilePath(filePath);
return this; return this;
} }
@ -100,12 +101,12 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
* 如果需要将test.zip改为game.zip只需要重新设置文件路径为/mnt/sdcard/game.zip * 如果需要将test.zip改为game.zip只需要重新设置文件路径为/mnt/sdcard/game.zip
* *
* @param filePath 路径必须为文件路径不能为文件夹路径 * @param filePath 路径必须为文件路径不能为文件夹路径
* @param forceDownload {@code true}强制下载不考虑未见路径是否被占用 * @param forceDownload {@code true}强制下载不考虑文件路径是否被占用
*/ */
@CheckResult @CheckResult
public DownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) { public DownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) {
mTempFilePath = filePath; setTempFilePath(filePath);
this.forceDownload = forceDownload; setForceDownload(forceDownload);
return this; return this;
} }
@ -116,7 +117,11 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
return mEntity.getDisposition(); return mEntity.getDisposition();
} }
@Override protected int getTargetType() { @Override public DownloadTarget updateUrl(String newUrl) {
return mNormalDelegate.updateUrl(newUrl);
}
@Override public int getTargetType() {
return HTTP; return HTTP;
} }
@ -140,4 +145,15 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
return mHeaderDelegate.addHeaders(headers); return mHeaderDelegate.addHeaders(headers);
} }
@Override protected boolean checkEntity() {
return mNormalDelegate.checkEntity();
}
@Override public boolean isRunning() {
return mNormalDelegate.isRunning();
}
@Override public boolean taskExists() {
return mNormalDelegate.taskExists();
}
} }

@ -35,22 +35,22 @@ public class FtpDirDownloadTarget extends BaseGroupTarget<FtpDirDownloadTarget>
private FtpDelegate<FtpDirDownloadTarget> mDelegate; private FtpDelegate<FtpDirDownloadTarget> mDelegate;
FtpDirDownloadTarget(String url, String targetName) { FtpDirDownloadTarget(String url, String targetName) {
mTargetName = targetName; setTargetName(targetName);
init(url); init(url);
} }
private void init(String key) { private void init(String key) {
mGroupHash = key; mGroupHash = key;
mTaskWrapper = TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, key); setTaskWrapper(TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, key));
mTaskWrapper.setRequestType(AbsTaskWrapper.D_FTP_DIR); getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP_DIR);
mEntity = mTaskWrapper.getEntity(); mEntity = getEntity();
if (mEntity != null) { if (mEntity != null) {
mDirPathTemp = mEntity.getDirPath(); mDirPathTemp = mEntity.getDirPath();
} }
mDelegate = new FtpDelegate<>(this); mDelegate = new FtpDelegate<>(this);
} }
@Override protected int getTargetType() { @Override public int getTargetType() {
return GROUP_FTP_DIR; return GROUP_FTP_DIR;
} }
@ -58,10 +58,10 @@ public class FtpDirDownloadTarget extends BaseGroupTarget<FtpDirDownloadTarget>
boolean b = getTargetType() == GROUP_FTP_DIR && checkDirPath() && checkUrl(); boolean b = getTargetType() == GROUP_FTP_DIR && checkDirPath() && checkUrl();
if (b) { if (b) {
mEntity.save(); mEntity.save();
if (mTaskWrapper.getSubTaskWrapper() != null) { if (getTaskWrapper().getSubTaskWrapper() != null) {
//初始化子项的登录信息 //初始化子项的登录信息
FtpUrlEntity tUrlEntity = mTaskWrapper.asFtp().getUrlEntity(); FtpUrlEntity tUrlEntity = getTaskWrapper().asFtp().getUrlEntity();
for (DTaskWrapper wrapper : mTaskWrapper.getSubTaskWrapper()) { for (DTaskWrapper wrapper : getTaskWrapper().getSubTaskWrapper()) {
FtpUrlEntity urlEntity = wrapper.asFtp().getUrlEntity(); FtpUrlEntity urlEntity = wrapper.asFtp().getUrlEntity();
urlEntity.needLogin = tUrlEntity.needLogin; urlEntity.needLogin = tUrlEntity.needLogin;
urlEntity.account = tUrlEntity.account; urlEntity.account = tUrlEntity.account;
@ -78,12 +78,12 @@ public class FtpDirDownloadTarget extends BaseGroupTarget<FtpDirDownloadTarget>
} }
} }
} }
if (mTaskWrapper.asFtp().getUrlEntity().isFtps) { if (getTaskWrapper().asFtp().getUrlEntity().isFtps) {
if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().storePath)) { if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().storePath)) {
ALog.e(TAG, "证书路径为空"); ALog.e(TAG, "证书路径为空");
return false; return false;
} }
if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) { if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().keyAlias)) {
ALog.e(TAG, "证书别名为空"); ALog.e(TAG, "证书别名为空");
return false; return false;
} }
@ -120,7 +120,7 @@ public class FtpDirDownloadTarget extends BaseGroupTarget<FtpDirDownloadTarget>
*/ */
@CheckResult @CheckResult
public FTPSDelegate<FtpDirDownloadTarget> asFtps() { public FTPSDelegate<FtpDirDownloadTarget> asFtps() {
mTaskWrapper.asFtp().getUrlEntity().isFtps = true; getTaskWrapper().asFtp().getUrlEntity().isFtps = true;
return new FTPSDelegate<>(this); return new FTPSDelegate<>(this);
} }

@ -30,26 +30,27 @@ import java.net.Proxy;
* Created by lyy on 2016/12/5. * Created by lyy on 2016/12/5.
* https://github.com/AriaLyy/Aria * https://github.com/AriaLyy/Aria
*/ */
public class FtpDownloadTarget extends BaseNormalTarget<FtpDownloadTarget> public class FtpDownloadTarget extends AbsDownloadTarget<FtpDownloadTarget>
implements IFtpTarget<FtpDownloadTarget> { implements IFtpTarget<FtpDownloadTarget> {
private FtpDelegate<FtpDownloadTarget> mDelegate; private FtpDelegate<FtpDownloadTarget> mFtpDelegate;
private DNormalDelegate<FtpDownloadTarget> mNormalDelegate;
public FtpDownloadTarget(DownloadEntity entity, String targetName) { FtpDownloadTarget(DownloadEntity entity, String targetName) {
this(entity.getUrl(), targetName); this(entity.getUrl(), targetName);
} }
FtpDownloadTarget(String url, String targetName) { FtpDownloadTarget(String url, String targetName) {
initTarget(url, targetName); mNormalDelegate = new DNormalDelegate<>(this, url, targetName);
init(); init();
} }
private void init() { private void init() {
int lastIndex = url.lastIndexOf("/"); int lastIndex = getUrl().lastIndexOf("/");
mEntity.setFileName(url.substring(lastIndex + 1)); mEntity.setFileName(getUrl().substring(lastIndex + 1));
mTaskWrapper.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getUrl()));
mTaskWrapper.setRequestType(AbsTaskWrapper.D_FTP); getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP);
mDelegate = new FtpDelegate<>(this); mFtpDelegate = new FtpDelegate<>(this);
} }
/** /**
@ -59,22 +60,30 @@ public class FtpDownloadTarget extends BaseNormalTarget<FtpDownloadTarget>
*/ */
@CheckResult @CheckResult
public FTPSDelegate<FtpDownloadTarget> asFtps() { public FTPSDelegate<FtpDownloadTarget> asFtps() {
mTaskWrapper.asFtp().getUrlEntity().isFtps = true; getTaskWrapper().asFtp().getUrlEntity().isFtps = true;
return new FTPSDelegate<>(this); return new FTPSDelegate<>(this);
} }
@Override protected boolean checkEntity() { @Override protected boolean checkEntity() {
if (mTaskWrapper.asFtp().getUrlEntity().isFtps){ if (getTaskWrapper().asFtp().getUrlEntity().isFtps) {
if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().storePath)){ if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().storePath)) {
ALog.e(TAG, "证书路径为空"); ALog.e(TAG, "证书路径为空");
return false; return false;
} }
if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)){ if (TextUtils.isEmpty(getTaskWrapper().asFtp().getUrlEntity().keyAlias)) {
ALog.e(TAG, "证书别名为空"); ALog.e(TAG, "证书别名为空");
return false; return false;
} }
} }
return super.checkEntity(); return mNormalDelegate.checkEntity();
}
@Override public boolean isRunning() {
return mNormalDelegate.isRunning();
}
@Override public boolean taskExists() {
return mNormalDelegate.taskExists();
} }
/** /**
@ -97,31 +106,50 @@ public class FtpDownloadTarget extends BaseNormalTarget<FtpDownloadTarget>
*/ */
@CheckResult @CheckResult
public FtpDownloadTarget setFilePath(@NonNull String filePath) { public FtpDownloadTarget setFilePath(@NonNull String filePath) {
mTempFilePath = filePath; setTempFilePath(filePath);
return this; return this;
} }
@Override protected int getTargetType() { /**
* 设置文件存储路径如果需要修改新的文件名修改路径便可
* 原文件路径 /mnt/sdcard/test.zip
* 如果需要将test.zip改为game.zip只需要重新设置文件路径为/mnt/sdcard/game.zip
*
* @param filePath 路径必须为文件路径不能为文件夹路径
* @param forceDownload {@code true}强制下载不考虑文件路径是否被占用
*/
@CheckResult
public FtpDownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) {
setTempFilePath(filePath);
setForceDownload(forceDownload);
return this;
}
@Override public int getTargetType() {
return FTP; return FTP;
} }
@CheckResult @CheckResult
@Override public FtpDownloadTarget charSet(String charSet) { @Override public FtpDownloadTarget charSet(String charSet) {
return mDelegate.charSet(charSet); return mFtpDelegate.charSet(charSet);
} }
@CheckResult @CheckResult
@Override public FtpDownloadTarget login(String userName, String password) { @Override public FtpDownloadTarget login(String userName, String password) {
return mDelegate.login(userName, password); return mFtpDelegate.login(userName, password);
} }
@CheckResult @CheckResult
@Override public FtpDownloadTarget login(String userName, String password, String account) { @Override public FtpDownloadTarget login(String userName, String password, String account) {
return mDelegate.login(userName, password, account); return mFtpDelegate.login(userName, password, account);
} }
@CheckResult @CheckResult
@Override public FtpDownloadTarget setProxy(Proxy proxy) { @Override public FtpDownloadTarget setProxy(Proxy proxy) {
return mDelegate.setProxy(proxy); return mFtpDelegate.setProxy(proxy);
}
@Override public FtpDownloadTarget updateUrl(String newUrl) {
return mNormalDelegate.updateUrl(newUrl);
} }
} }

@ -37,11 +37,12 @@ import java.util.List;
* Created by AriaL on 2017/7/3. * Created by AriaL on 2017/7/3.
*/ */
public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper> public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper>
implements ITarget { implements ITargetHandler {
protected String TAG; protected String TAG;
protected ENTITY mEntity; protected ENTITY mEntity;
protected TASK_WRAPPER mTaskWrapper; private TASK_WRAPPER mTaskWrapper;
protected String mTargetName; private String mTargetName;
protected AbsTarget() { protected AbsTarget() {
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
@ -82,6 +83,22 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
} }
} }
public ENTITY getEntity() {
return mEntity;
}
public void setTaskWrapper(TASK_WRAPPER mTaskWrapper) {
this.mTaskWrapper = mTaskWrapper;
}
public String getTargetName() {
return mTargetName;
}
public void setTargetName(String mTargetName) {
this.mTargetName = mTargetName;
}
/** /**
* 获取任务实体 * 获取任务实体
*/ */
@ -103,7 +120,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
* *
* @return 文件大小 * @return 文件大小
*/ */
public long getSize() { public long getFileSize() {
return mEntity == null ? 0 : mEntity.getFileSize(); return mEntity == null ? 0 : mEntity.getFileSize();
} }
@ -112,7 +129,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
* *
* @return 文件大小{@code xxx mb} * @return 文件大小{@code xxx mb}
*/ */
public String getConvertSize() { public String getConvertFileSize() {
return mEntity == null ? "0b" : CommonUtil.formatFileSize(mEntity.getFileSize()); return mEntity == null ? "0b" : CommonUtil.formatFileSize(mEntity.getFileSize());
} }
@ -212,6 +229,25 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
*/ */
public abstract boolean taskExists(); public abstract boolean taskExists();
/**
* 设置target类型
*
* @return {@link #HTTP}{@link #FTP}{@link #GROUP_HTTP}{@link #GROUP_FTP_DIR}
*/
public abstract int getTargetType();
/**
* 添加任务
*/
@Override public void add() {
if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CREATE,
checkTaskType()))
.exe();
}
}
/** /**
* 开始任务 * 开始任务
*/ */

@ -21,7 +21,7 @@ import java.net.Proxy;
/** /**
* Created by laoyuyu on 2018/3/9. * Created by laoyuyu on 2018/3/9.
*/ */
public interface IFtpTarget<TARGET extends ITarget> { public interface IFtpTarget<TARGET extends ITargetHandler> {
/** /**
* 设置字符编码 * 设置字符编码
*/ */

@ -17,8 +17,6 @@ package com.arialyy.aria.core.inf;
import android.support.annotation.CheckResult; import android.support.annotation.CheckResult;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.download.DownloadEntity;
import java.net.Proxy; import java.net.Proxy;
import java.util.Map; import java.util.Map;
@ -26,7 +24,7 @@ import java.util.Map;
* Created by laoyuyu on 2018/3/9. * Created by laoyuyu on 2018/3/9.
* HTTP Header功能接口 * HTTP Header功能接口
*/ */
public interface IHttpHeaderDelegate<TARGET extends ITarget> { public interface IHttpHeaderDelegate<TARGET extends ITargetHandler> {
/** /**
* 给url请求添加Header数据 * 给url请求添加Header数据

@ -17,8 +17,21 @@ package com.arialyy.aria.core.inf;
/** /**
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
* 任务接收器的控制接口处理添加任务开始任务停止任务删除任务等功能
*/ */
public interface ITarget { public interface ITargetHandler {
int HTTP = 1;
int FTP = 2;
//HTTP任务组
int GROUP_HTTP = 3;
//FTP文件夹
int GROUP_FTP_DIR = 4;
/**
* 添加任务
*/
void add();
/** /**
* 开始下载 * 开始下载
*/ */

@ -16,8 +16,9 @@
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
/** /**
* 任务信息 * Created by AriaL on 2017/6/29.
* 处理任务头部信息等设置等接口
*/ */
public interface ITaskDelegate { public interface ITargetHeadDelegate {
} }

@ -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();
}

@ -15,8 +15,13 @@
*/ */
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.upload.UploadTask;
/** /**
* Created by lyy on 2017/2/13. * Created by lyy on 2017/2/13.
* 任务接口{@link DownloadTask}{@link UploadTask}{@link DownloadGroupTask}
*/ */
public interface ITask<TASK_WRAPPER extends AbsTaskWrapper> { public interface ITask<TASK_WRAPPER extends AbsTaskWrapper> {

@ -15,14 +15,33 @@
*/ */
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
/** /**
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
* 普通上传任务接收器
*/ */
abstract class AbsUploadTarget<TARGET extends AbsUploadTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskWrapper> abstract class AbsUploadTarget<TARGET extends AbsUploadTarget>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> { extends AbsTarget<TARGET, UploadEntity, UTaskWrapper> {
/**
* 上传路径
*/
private String mTempUrl;
@Override public void setTaskWrapper(UTaskWrapper mTaskWrapper) {
super.setTaskWrapper(mTaskWrapper);
}
String getTempUrl() {
return mTempUrl;
}
void setTempUrl(String tempUrl) {
this.mTempUrl = tempUrl;
}
void setEntity(UploadEntity entity) {
this.mEntity = entity;
}
} }

@ -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; // // return false;
//} // //}
if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) { // if (TextUtils.isEmpty(mTaskWrapper.asFtp().getUrlEntity().keyAlias)) {
ALog.e(TAG, "证书别名为空"); // ALog.e(TAG, "证书别名为空");
return false; // return false;
} // }
} // }
return b; // return b;
} // }
//
/** // /**
* 检查上传文件路径是否合法 // * 检查上传文件路径是否合法
* // *
* @return {@code true} 合法 // * @return {@code true} 合法
*/ // */
private boolean checkFilePath() { // private boolean checkFilePath() {
String filePath = mEntity.getFilePath(); // String filePath = mEntity.getFilePath();
if (TextUtils.isEmpty(filePath)) { // if (TextUtils.isEmpty(filePath)) {
ALog.e(TAG, "上传失败,文件路径为null"); // ALog.e(TAG, "上传失败,文件路径为null");
return false; // return false;
} else if (!filePath.startsWith("/")) { // } else if (!filePath.startsWith("/")) {
ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法"); // ALog.e(TAG, "上传失败,文件路径【" + filePath + "】不合法");
return false; // return false;
} // }
//
File file = new File(mEntity.getFilePath()); // File file = new File(mEntity.getFilePath());
if (!file.exists()) { // if (!file.exists()) {
ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在"); // ALog.e(TAG, "上传失败,文件【" + filePath + "】不存在");
return false; // return false;
} // }
if (file.isDirectory()) { // if (file.isDirectory()) {
ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹"); // ALog.e(TAG, "上传失败,文件【" + filePath + "】不能是文件夹");
return false; // return false;
} // }
return true; // return true;
} // }
//
/** // /**
* 检查普通任务的下载地址 // * 检查普通任务的下载地址
* // *
* @return {@code true}地址合法 // * @return {@code true}地址合法
*/ // */
protected boolean checkUrl() { // protected boolean checkUrl() {
final String url = mTempUrl; // final String url = mTempUrl;
if (TextUtils.isEmpty(url)) { // if (TextUtils.isEmpty(url)) {
ALog.e(TAG, "上传失败,url为null"); // ALog.e(TAG, "上传失败,url为null");
return false; // return false;
} else if (!url.startsWith("http") && !url.startsWith("ftp")) { // } else if (!url.startsWith("http") && !url.startsWith("ftp")) {
ALog.e(TAG, "上传失败,url【" + url + "】错误"); // ALog.e(TAG, "上传失败,url【" + url + "】错误");
return false; // return false;
} // }
int index = url.indexOf("://"); // int index = url.indexOf("://");
if (index == -1) { // if (index == -1) {
ALog.e(TAG, "上传失败,url【" + url + "】不合法"); // ALog.e(TAG, "上传失败,url【" + url + "】不合法");
return false; // return false;
} // }
mEntity.setUrl(url); // mEntity.setUrl(url);
return true; // return true;
} // }
} //}

@ -16,72 +16,40 @@
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import android.support.annotation.CheckResult; import android.support.annotation.CheckResult;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ftp.FTPSDelegate; import com.arialyy.aria.core.common.ftp.FTPSDelegate;
import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.common.ftp.FtpDelegate;
import com.arialyy.aria.core.common.ftp.FtpTaskDelegate;
import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.inf.IFtpTarget; import com.arialyy.aria.core.inf.IFtpTarget;
import com.arialyy.aria.util.CommonUtil;
import java.net.Proxy; import java.net.Proxy;
/** /**
* Created by Aria.Lao on 2017/7/27. * Created by Aria.Lao on 2017/7/27.
* ftp单任务上传 * ftp单任务上传
*/ */
public class FtpUploadTarget extends BaseNormalTarget<FtpUploadTarget> public class FtpUploadTarget extends AbsUploadTarget<FtpUploadTarget>
implements IFtpTarget<FtpUploadTarget> { implements IFtpTarget<FtpUploadTarget> {
private FtpDelegate<FtpUploadTarget> mDelegate; private FtpDelegate<FtpUploadTarget> mFtpDelegate;
private UNormalDelegate<FtpUploadTarget> mNormalDelegate;
private String mAccount, mUser, mPw;
private boolean needLogin = false;
FtpUploadTarget(String filePath, String targetName) { FtpUploadTarget(String filePath, String targetName) {
this.mTargetName = targetName; mNormalDelegate = new UNormalDelegate<>(this, filePath, targetName);
initTask(filePath); initTask();
} }
private void initTask(String filePath) { private void initTask() {
initTarget(filePath); getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP);
mTaskWrapper.setRequestType(AbsTaskWrapper.U_FTP); mFtpDelegate = new FtpDelegate<>(this);
mDelegate = new FtpDelegate<>(this);
} }
/** /**
* 添加任务 * 设置上传路径
*
* @param tempUrl 上传路径
*/ */
public void add() { public FtpUploadTarget setUploadUrl(String tempUrl) {
if (checkEntity()) { setTempUrl(tempUrl);
AriaManager.getInstance(AriaManager.APP) return this;
.setCmd(CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_CREATE,
checkTaskType()))
.exe();
}
}
@Override protected boolean checkUrl() {
boolean b = super.checkUrl();
if (!b) {
return false;
}
FtpTaskDelegate taskDelegate = mTaskWrapper.asFtp();
FtpUrlEntity temp = taskDelegate.getUrlEntity();
FtpUrlEntity newEntity = CommonUtil.getFtpUrlInfo(mTempUrl);
if (temp != null) { //处理FTPS的信息
newEntity.isFtps = temp.isFtps;
newEntity.storePass = temp.storePass;
newEntity.keyAlias = temp.keyAlias;
newEntity.protocol = temp.protocol;
newEntity.storePath = temp.storePath;
}
taskDelegate.setUrlEntity(newEntity);
taskDelegate.getUrlEntity().account = mAccount;
taskDelegate.getUrlEntity().user = mUser;
taskDelegate.getUrlEntity().password = mPw;
taskDelegate.getUrlEntity().needLogin = needLogin;
return true;
} }
/** /**
@ -91,35 +59,44 @@ public class FtpUploadTarget extends BaseNormalTarget<FtpUploadTarget>
*/ */
@CheckResult @CheckResult
public FTPSDelegate<FtpUploadTarget> asFtps() { public FTPSDelegate<FtpUploadTarget> asFtps() {
if (mTaskWrapper.asFtp().getUrlEntity() == null) { if (getTaskWrapper().asFtp().getUrlEntity() == null) {
FtpUrlEntity urlEntity = new FtpUrlEntity(); FtpUrlEntity urlEntity = new FtpUrlEntity();
urlEntity.isFtps = true; urlEntity.isFtps = true;
mTaskWrapper.asFtp().setUrlEntity(urlEntity); getTaskWrapper().asFtp().setUrlEntity(urlEntity);
} }
return new FTPSDelegate<>(this); return new FTPSDelegate<>(this);
} }
@CheckResult @CheckResult
@Override public FtpUploadTarget charSet(String charSet) { @Override public FtpUploadTarget charSet(String charSet) {
return mDelegate.charSet(charSet); return mFtpDelegate.charSet(charSet);
} }
@Override public FtpUploadTarget login(String userName, String password) { @Override public FtpUploadTarget login(String userName, String password) {
needLogin = true; return mFtpDelegate.login(userName, password);
mUser = userName;
mPw = password;
return this;
} }
@Override public FtpUploadTarget login(String userName, String password, String account) { @Override public FtpUploadTarget login(String userName, String password, String account) {
needLogin = true; return mFtpDelegate.login(userName, password, account);
mUser = userName;
mPw = password;
mAccount = account;
return this;
} }
@Override public FtpUploadTarget setProxy(Proxy proxy) { @Override public FtpUploadTarget setProxy(Proxy proxy) {
return mDelegate.setProxy(proxy); return mFtpDelegate.setProxy(proxy);
}
@Override protected boolean checkEntity() {
return mNormalDelegate.checkEntity();
}
@Override public boolean isRunning() {
return mNormalDelegate.isRunning();
}
@Override public boolean taskExists() {
return mNormalDelegate.taskExists();
}
@Override public int getTargetType() {
return FTP;
} }
} }

@ -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;
}
}

@ -28,22 +28,31 @@ import java.util.Map;
* Created by lyy on 2017/2/28. * Created by lyy on 2017/2/28.
* http 单文件上传 * http 单文件上传
*/ */
public class UploadTarget extends BaseNormalTarget<UploadTarget> public class UploadTarget extends AbsUploadTarget<UploadTarget>
implements IHttpHeaderDelegate<UploadTarget> { implements IHttpHeaderDelegate<UploadTarget> {
private HttpHeaderDelegate<UploadTarget> mDelegate; private HttpHeaderDelegate<UploadTarget> mHeaderDelegate;
private UNormalDelegate<UploadTarget> mNormalDelegate;
UploadTarget(String filePath, String targetName) { UploadTarget(String filePath, String targetName) {
this.mTargetName = targetName; mNormalDelegate = new UNormalDelegate<>(this, filePath, targetName);
initTask(filePath); initTask();
} }
private void initTask(String filePath) { private void initTask() {
initTarget(filePath);
//http暂时不支持断点上传 //http暂时不支持断点上传
mTaskWrapper.setSupportBP(false); getTaskWrapper().setSupportBP(false);
mTaskWrapper.setRequestType(AbsTaskWrapper.U_HTTP); getTaskWrapper().setRequestType(AbsTaskWrapper.U_HTTP);
mDelegate = new HttpHeaderDelegate<>(this); mHeaderDelegate = new HttpHeaderDelegate<>(this);
}
/**
* 设置上传路径
*
* @param tempUrl 上传路径
*/
public UploadTarget setUploadUrl(String tempUrl) {
setTempUrl(tempUrl);
return this;
} }
/** /**
@ -58,7 +67,7 @@ public class UploadTarget extends BaseNormalTarget<UploadTarget>
*/ */
@CheckResult @CheckResult
public UploadTarget setUserAngent(@NonNull String userAgent) { public UploadTarget setUserAngent(@NonNull String userAgent) {
mTaskWrapper.asHttp().setUserAgent(userAgent); getTaskWrapper().asHttp().setUserAgent(userAgent);
return this; return this;
} }
@ -69,7 +78,7 @@ public class UploadTarget extends BaseNormalTarget<UploadTarget>
*/ */
@CheckResult @CheckResult
public UploadTarget setAttachment(@NonNull String attachment) { public UploadTarget setAttachment(@NonNull String attachment) {
mTaskWrapper.asHttp().setAttachment(attachment); getTaskWrapper().asHttp().setAttachment(attachment);
return this; return this;
} }
@ -80,21 +89,37 @@ public class UploadTarget extends BaseNormalTarget<UploadTarget>
*/ */
@CheckResult @CheckResult
public UploadTarget setContentType(String contentType) { public UploadTarget setContentType(String contentType) {
mTaskWrapper.asHttp().setContentType(contentType); getTaskWrapper().asHttp().setContentType(contentType);
return this; return this;
} }
@CheckResult @CheckResult
@Override public UploadTarget addHeader(@NonNull String key, @NonNull String value) { @Override public UploadTarget addHeader(@NonNull String key, @NonNull String value) {
return mDelegate.addHeader(key, value); return mHeaderDelegate.addHeader(key, value);
} }
@CheckResult @CheckResult
@Override public UploadTarget addHeaders(Map<String, String> headers) { @Override public UploadTarget addHeaders(Map<String, String> headers) {
return mDelegate.addHeaders(headers); return mHeaderDelegate.addHeaders(headers);
} }
@Override public UploadTarget setUrlProxy(Proxy proxy) { @Override public UploadTarget setUrlProxy(Proxy proxy) {
return mDelegate.setUrlProxy(proxy); return mHeaderDelegate.setUrlProxy(proxy);
}
@Override protected boolean checkEntity() {
return mNormalDelegate.checkEntity();
}
@Override public boolean isRunning() {
return mNormalDelegate.isRunning();
}
@Override public boolean taskExists() {
return mNormalDelegate.taskExists();
}
@Override public int getTargetType() {
return HTTP;
} }
} }

@ -68,7 +68,7 @@ public class HttpUploadActivity extends BaseActivity<ActivityUploadBinding> {
Aria.upload(HttpUploadActivity.this).load(FILE_PATH) Aria.upload(HttpUploadActivity.this).load(FILE_PATH)
.setUploadUrl( .setUploadUrl(
"http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}") "http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}")
//.setUploadUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file") //.setTempUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file")
//.addHeader("iplanetdirectorypro", "11a09102fb934ad0bc206f9c611d7933") //.addHeader("iplanetdirectorypro", "11a09102fb934ad0bc206f9c611d7933")
.asPost() .asPost()
.start(); .start();

Loading…
Cancel
Save