优化FtpFileInfoTask的代码逻辑
This commit is contained in:
@@ -37,7 +37,6 @@ import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.util.SSLContextUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
@@ -57,7 +56,6 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
|
||||
private int mConnectTimeOut;
|
||||
protected long mSize = 0;
|
||||
protected String charSet = "UTF-8";
|
||||
protected boolean isUpload = false;
|
||||
protected Callback callback;
|
||||
|
||||
public AbsFtpInfoTask(TASK_WRAPPER taskWrapper) {
|
||||
@@ -74,6 +72,16 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
|
||||
*/
|
||||
protected abstract String getRemotePath();
|
||||
|
||||
/**
|
||||
* 处理ftp列表信息
|
||||
*
|
||||
* @param client ftp 客户端对象
|
||||
* @param files remotePath 对应的文件列表
|
||||
* @param convertedRemotePath 已转换的可被服务器识别的remotePath
|
||||
*/
|
||||
protected abstract void handelFileInfo(FTPClient client, FTPFile[] files,
|
||||
String convertedRemotePath) throws IOException;
|
||||
|
||||
@Override public void setCallback(Callback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
@@ -89,67 +97,9 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
|
||||
ALog.e(TAG, String.format("任务【%s】失败", mTaskOption.getUrlEntity().url));
|
||||
return;
|
||||
}
|
||||
String remotePath = CommonUtil.convertFtpChar(charSet, getRemotePath());
|
||||
|
||||
FTPFile[] files = client.listFiles(remotePath);
|
||||
boolean isExist = files.length != 0;
|
||||
if (!isExist && !isUpload) {
|
||||
int i = remotePath.lastIndexOf(File.separator);
|
||||
FTPFile[] files1;
|
||||
if (i == -1) {
|
||||
files1 = client.listFiles();
|
||||
} else {
|
||||
files1 = client.listFiles(remotePath.substring(0, i + 1));
|
||||
}
|
||||
if (files1.length > 0) {
|
||||
ALog.i(TAG,
|
||||
String.format("路径【%s】下的文件列表 ===================================", getRemotePath()));
|
||||
for (FTPFile file : files1) {
|
||||
ALog.d(TAG, file.toString());
|
||||
}
|
||||
ALog.i(TAG,
|
||||
"================================= --end-- ===================================");
|
||||
} else {
|
||||
ALog.w(TAG, String.format("获取文件列表失败,msg:%s", client.getReplyString()));
|
||||
}
|
||||
closeClient(client);
|
||||
|
||||
failDownload(client,
|
||||
String.format("文件不存在,url: %s, remotePath:%s", mTaskOption.getUrlEntity().url,
|
||||
remotePath), null, false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理拦截功能
|
||||
if (!onInterceptor(client, files)) {
|
||||
closeClient(client);
|
||||
ALog.d(TAG, "拦截器处理完成任务");
|
||||
return;
|
||||
}
|
||||
|
||||
//为了防止编码错乱,需要使用原始字符串
|
||||
if (isUpload && files.length == 0){
|
||||
handleFile(getRemotePath(), null);
|
||||
}else {
|
||||
mSize = getFileSize(files, client, getRemotePath());
|
||||
}
|
||||
int reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
if (isUpload) {
|
||||
//服务器上没有该文件路径,表示该任务为新的上传任务
|
||||
mTaskWrapper.setNewTask(true);
|
||||
} else {
|
||||
closeClient(client);
|
||||
failDownload(client, "获取文件信息错误,url: " + mTaskOption.getUrlEntity().url, null, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
mTaskWrapper.setCode(reply);
|
||||
if (mSize != 0 && !isUpload) {
|
||||
mEntity.setFileSize(mSize);
|
||||
}
|
||||
onPreComplete(reply);
|
||||
mEntity.update();
|
||||
String convertedRemotePath = CommonUtil.convertFtpChar(charSet, getRemotePath());
|
||||
FTPFile[] files = client.listFiles(convertedRemotePath);
|
||||
handelFileInfo(client, files, convertedRemotePath);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
failDownload(client, "FTP错误信息", e, true);
|
||||
@@ -171,20 +121,6 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查文件是否存在
|
||||
*
|
||||
* @return {@code true}存在
|
||||
*/
|
||||
private boolean checkFileExist(FTPFile[] ftpFiles, String fileName) {
|
||||
for (FTPFile ff : ftpFiles) {
|
||||
if (ff.getName().equals(fileName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void onPreComplete(int code) {
|
||||
|
||||
}
|
||||
@@ -358,7 +294,7 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
|
||||
*
|
||||
* @throws IOException 字符串编码转换错误
|
||||
*/
|
||||
private long getFileSize(FTPFile[] files, FTPClient client, String dirName) throws IOException {
|
||||
protected long getFileSize(FTPFile[] files, FTPClient client, String dirName) throws IOException {
|
||||
long size = 0;
|
||||
String path = dirName + "/";
|
||||
for (FTPFile file : files) {
|
||||
|
||||
@@ -72,11 +72,6 @@ public final class FtpTaskOption implements ITaskOption {
|
||||
*/
|
||||
private String activeExternalIPAddress;
|
||||
|
||||
/**
|
||||
* 服务器文件是否存在,true 文件存在,用于ftp上传
|
||||
*/
|
||||
private boolean serveFileIsExist = true;
|
||||
|
||||
//---------------- ftp client 配置信息 start
|
||||
private String defaultDateFormatStr = null;
|
||||
private String recentDateFormatStr = null;
|
||||
@@ -86,14 +81,6 @@ public final class FtpTaskOption implements ITaskOption {
|
||||
private String systemKey = FTPClientConfig.SYST_UNIX;
|
||||
//---------------- ftp client 配置信息 end
|
||||
|
||||
public boolean isServeFileIsExist() {
|
||||
return serveFileIsExist;
|
||||
}
|
||||
|
||||
public void setServeFileIsExist(boolean serveFileIsExist) {
|
||||
this.serveFileIsExist = serveFileIsExist;
|
||||
}
|
||||
|
||||
public String getActiveExternalIPAddress() {
|
||||
return activeExternalIPAddress;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package com.arialyy.aria.ftp.download;
|
||||
|
||||
import aria.apache.commons.net.ftp.FTPClient;
|
||||
import aria.apache.commons.net.ftp.FTPFile;
|
||||
import aria.apache.commons.net.ftp.FTPReply;
|
||||
import com.arialyy.aria.core.common.CompleteInfo;
|
||||
import com.arialyy.aria.core.download.DTaskWrapper;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
@@ -23,6 +25,8 @@ import com.arialyy.aria.exception.AriaIOException;
|
||||
import com.arialyy.aria.ftp.AbsFtpInfoTask;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.FileUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/7/25.
|
||||
@@ -43,6 +47,60 @@ final class FtpDFileInfoTask extends AbsFtpInfoTask<DownloadEntity, DTaskWrapper
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handelFileInfo(FTPClient client, FTPFile[] files, String convertedRemotePath)
|
||||
throws IOException {
|
||||
boolean isExist = files.length != 0;
|
||||
if (!isExist) {
|
||||
int i = convertedRemotePath.lastIndexOf(File.separator);
|
||||
FTPFile[] files1;
|
||||
if (i == -1) {
|
||||
files1 = client.listFiles();
|
||||
} else {
|
||||
files1 = client.listFiles(convertedRemotePath.substring(0, i + 1));
|
||||
}
|
||||
if (files1.length > 0) {
|
||||
ALog.i(TAG,
|
||||
String.format("路径【%s】下的文件列表 ===================================", getRemotePath()));
|
||||
for (FTPFile file : files1) {
|
||||
ALog.d(TAG, file.toString());
|
||||
}
|
||||
ALog.i(TAG,
|
||||
"================================= --end-- ===================================");
|
||||
} else {
|
||||
ALog.w(TAG, String.format("获取文件列表失败,msg:%s", client.getReplyString()));
|
||||
}
|
||||
closeClient(client);
|
||||
|
||||
failDownload(client,
|
||||
String.format("文件不存在,url: %s, remotePath:%s", mTaskOption.getUrlEntity().url,
|
||||
getRemotePath()), null, false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理拦截功能
|
||||
if (!onInterceptor(client, files)) {
|
||||
closeClient(client);
|
||||
ALog.d(TAG, "拦截器处理完成任务");
|
||||
return;
|
||||
}
|
||||
|
||||
//为了防止编码错乱,需要使用原始字符串
|
||||
mSize = getFileSize(files, client, getRemotePath());
|
||||
int reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
closeClient(client);
|
||||
failDownload(client, "获取文件信息错误,url: " + mTaskOption.getUrlEntity().url, null, true);
|
||||
return;
|
||||
}
|
||||
mTaskWrapper.setCode(reply);
|
||||
if (mSize != 0) {
|
||||
mEntity.setFileSize(mSize);
|
||||
}
|
||||
onPreComplete(reply);
|
||||
mEntity.update();
|
||||
}
|
||||
|
||||
@Override protected String getRemotePath() {
|
||||
return mTaskOption.getUrlEntity().remotePath;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
import aria.apache.commons.net.ftp.FTPClient;
|
||||
import aria.apache.commons.net.ftp.FTPFile;
|
||||
import aria.apache.commons.net.ftp.FTPReply;
|
||||
import com.arialyy.aria.core.FtpUrlEntity;
|
||||
import com.arialyy.aria.core.common.CompleteInfo;
|
||||
import com.arialyy.aria.core.download.DGTaskWrapper;
|
||||
@@ -32,6 +33,8 @@ import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.util.RecordUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -52,6 +55,60 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handelFileInfo(FTPClient client, FTPFile[] files, String convertedRemotePath)
|
||||
throws IOException {
|
||||
boolean isExist = files.length != 0;
|
||||
if (!isExist) {
|
||||
int i = convertedRemotePath.lastIndexOf(File.separator);
|
||||
FTPFile[] files1;
|
||||
if (i == -1) {
|
||||
files1 = client.listFiles();
|
||||
} else {
|
||||
files1 = client.listFiles(convertedRemotePath.substring(0, i + 1));
|
||||
}
|
||||
if (files1.length > 0) {
|
||||
ALog.i(TAG,
|
||||
String.format("路径【%s】下的文件列表 ===================================", getRemotePath()));
|
||||
for (FTPFile file : files1) {
|
||||
ALog.d(TAG, file.toString());
|
||||
}
|
||||
ALog.i(TAG,
|
||||
"================================= --end-- ===================================");
|
||||
} else {
|
||||
ALog.w(TAG, String.format("获取文件列表失败,msg:%s", client.getReplyString()));
|
||||
}
|
||||
closeClient(client);
|
||||
|
||||
failDownload(client,
|
||||
String.format("文件不存在,url: %s, remotePath:%s", mTaskOption.getUrlEntity().url,
|
||||
getRemotePath()), null, false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理拦截功能
|
||||
if (!onInterceptor(client, files)) {
|
||||
closeClient(client);
|
||||
ALog.d(TAG, "拦截器处理完成任务");
|
||||
return;
|
||||
}
|
||||
|
||||
//为了防止编码错乱,需要使用原始字符串
|
||||
mSize = getFileSize(files, client, getRemotePath());
|
||||
int reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
closeClient(client);
|
||||
failDownload(client, "获取文件信息错误,url: " + mTaskOption.getUrlEntity().url, null, true);
|
||||
return;
|
||||
}
|
||||
mTaskWrapper.setCode(reply);
|
||||
if (mSize != 0) {
|
||||
mEntity.setFileSize(mSize);
|
||||
}
|
||||
onPreComplete(reply);
|
||||
mEntity.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查子任务的任务设置
|
||||
*
|
||||
@@ -110,7 +167,7 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
|
||||
final FtpUrlEntity urlEntity = mTaskOption.getUrlEntity().clone();
|
||||
String url =
|
||||
urlEntity.scheme + "://" + urlEntity.hostName + ":" + urlEntity.port + "/" + remotePath;
|
||||
if (checkEntityExist(url)){
|
||||
if (checkEntityExist(url)) {
|
||||
ALog.w(TAG, "子任务已存在,取消子任务的添加,url = " + url);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.arialyy.aria.core.loader.LoaderStructure;
|
||||
import com.arialyy.aria.core.loader.NormalLoader;
|
||||
import com.arialyy.aria.core.loader.NormalThreadStateManager;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import com.arialyy.aria.ftp.FtpRecordHandler;
|
||||
import com.arialyy.aria.ftp.FtpTaskOption;
|
||||
|
||||
/**
|
||||
@@ -43,7 +42,7 @@ public final class FtpDLoaderUtil extends AbsNormalLoaderUtil {
|
||||
|
||||
public LoaderStructure BuildLoaderStructure() {
|
||||
LoaderStructure structure = new LoaderStructure();
|
||||
structure.addComponent(new FtpRecordHandler(getTaskWrapper()))
|
||||
structure.addComponent(new FtpDRecordHandler(getTaskWrapper()))
|
||||
.addComponent(new NormalThreadStateManager(getListener()))
|
||||
.addComponent(new FtpDFileInfoTask((DTaskWrapper) getTaskWrapper()))
|
||||
.addComponent(new FtpDTTBuilder(getTaskWrapper()));
|
||||
|
||||
@@ -13,16 +13,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.aria.ftp;
|
||||
package com.arialyy.aria.ftp.download;
|
||||
|
||||
import com.arialyy.aria.core.TaskRecord;
|
||||
import com.arialyy.aria.core.ThreadRecord;
|
||||
import com.arialyy.aria.core.common.RecordHandler;
|
||||
import com.arialyy.aria.core.common.RecordHelper;
|
||||
import com.arialyy.aria.core.config.Configuration;
|
||||
import com.arialyy.aria.core.download.DTaskWrapper;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.loader.IRecordHandler;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import com.arialyy.aria.core.wrapper.ITaskWrapper;
|
||||
import com.arialyy.aria.util.RecordUtil;
|
||||
import java.util.ArrayList;
|
||||
@@ -31,17 +31,13 @@ import java.util.ArrayList;
|
||||
* @Author lyy
|
||||
* @Date 2019-09-19
|
||||
*/
|
||||
public final class FtpRecordHandler extends RecordHandler {
|
||||
final class FtpDRecordHandler extends RecordHandler {
|
||||
|
||||
public FtpRecordHandler(AbsTaskWrapper wrapper) {
|
||||
FtpDRecordHandler(DTaskWrapper wrapper) {
|
||||
super(wrapper);
|
||||
}
|
||||
|
||||
@Override public void handlerTaskRecord(TaskRecord record) {
|
||||
if (getWrapper().getRequestType() == ITaskWrapper.U_FTP) {
|
||||
// 上传任务记录的处理交由FtpUFileInfoTask 完成
|
||||
return;
|
||||
}
|
||||
RecordHelper helper = new RecordHelper(getWrapper(), record);
|
||||
if (record.threadNum == 1) {
|
||||
helper.handleSingleThreadRecord();
|
||||
@@ -20,7 +20,6 @@ import com.arialyy.aria.core.download.DTaskWrapper;
|
||||
import com.arialyy.aria.core.group.AbsSubDLoadUtil;
|
||||
import com.arialyy.aria.core.loader.LoaderStructure;
|
||||
import com.arialyy.aria.core.loader.SubLoader;
|
||||
import com.arialyy.aria.ftp.FtpRecordHandler;
|
||||
|
||||
/**
|
||||
* @Author lyy
|
||||
@@ -47,7 +46,7 @@ final class FtpSubDLoaderUtil extends AbsSubDLoadUtil {
|
||||
|
||||
@Override protected LoaderStructure buildLoaderStructure() {
|
||||
LoaderStructure structure = new LoaderStructure();
|
||||
structure.addComponent(new FtpRecordHandler(getWrapper()))
|
||||
structure.addComponent(new FtpDRecordHandler(getWrapper()))
|
||||
.addComponent(new FtpDTTBuilder(getWrapper()))
|
||||
.addComponent(new FtpDFileInfoTask(getWrapper()));
|
||||
structure.accept(getLoader());
|
||||
|
||||
@@ -18,19 +18,15 @@ package com.arialyy.aria.ftp.upload;
|
||||
import android.text.TextUtils;
|
||||
import aria.apache.commons.net.ftp.FTPClient;
|
||||
import aria.apache.commons.net.ftp.FTPFile;
|
||||
import com.arialyy.aria.core.TaskRecord;
|
||||
import com.arialyy.aria.core.ThreadRecord;
|
||||
import aria.apache.commons.net.ftp.FTPReply;
|
||||
import com.arialyy.aria.core.common.CompleteInfo;
|
||||
import com.arialyy.aria.core.processor.FtpInterceptHandler;
|
||||
import com.arialyy.aria.core.processor.IFtpUploadInterceptor;
|
||||
import com.arialyy.aria.core.upload.UTaskWrapper;
|
||||
import com.arialyy.aria.core.upload.UploadEntity;
|
||||
import com.arialyy.aria.ftp.AbsFtpInfoTask;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.util.DbDataHelper;
|
||||
import com.arialyy.aria.util.RecordUtil;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -43,10 +39,10 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
|
||||
static final int CODE_COMPLETE = 0xab1;
|
||||
private boolean isComplete = false;
|
||||
private String remotePath;
|
||||
private FTPFile ftpFile;
|
||||
|
||||
FtpUFileInfoTask(UTaskWrapper taskEntity) {
|
||||
super(taskEntity);
|
||||
isUpload = true;
|
||||
}
|
||||
|
||||
@Override protected String getRemotePath() {
|
||||
@@ -54,6 +50,30 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
|
||||
mTaskOption.getUrlEntity().remotePath + "/" + mEntity.getFileName() : remotePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handelFileInfo(FTPClient client, FTPFile[] files, String convertedRemotePath)
|
||||
throws IOException {
|
||||
// 处理拦截功能
|
||||
if (!onInterceptor(client, files)) {
|
||||
closeClient(client);
|
||||
ALog.d(TAG, "拦截器处理完成任务");
|
||||
return;
|
||||
}
|
||||
|
||||
//为了防止编码错乱,需要使用原始字符串
|
||||
if (files.length == 0) {
|
||||
handleFile(getRemotePath(), null);
|
||||
}
|
||||
int reply = client.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
//服务器上没有该文件路径,表示该任务为新的上传任务
|
||||
mTaskWrapper.setNewTask(true);
|
||||
}
|
||||
mTaskWrapper.setCode(reply);
|
||||
onPreComplete(reply);
|
||||
mEntity.update();
|
||||
}
|
||||
|
||||
@Override protected boolean onInterceptor(FTPClient client, FTPFile[] ftpFiles) {
|
||||
// 旧任务将不做处理,否则断点续传上传将失效
|
||||
//if (!mTaskWrapper.isNewTask()) {
|
||||
@@ -86,7 +106,6 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
|
||||
ALog.d(TAG,
|
||||
String.format("删除文件%s,code: %s, msg: %s", b ? "成功" : "失败", client.getReplyCode(),
|
||||
client.getReplyString()));
|
||||
mTaskOption.setServeFileIsExist(false);
|
||||
} else if (!TextUtils.isEmpty(interceptHandler.getNewFileName())) {
|
||||
ALog.i(TAG, String.format("远端已拥有同名文件,将修改remotePath,原文件名:%s,新文件名:%s",
|
||||
mEntity.getFileName(), interceptHandler.getNewFileName()));
|
||||
@@ -95,7 +114,6 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
|
||||
+ interceptHandler.getNewFileName();
|
||||
mTaskOption.setNewFileName(interceptHandler.getNewFileName());
|
||||
|
||||
mTaskOption.setServeFileIsExist(false);
|
||||
closeClient(client);
|
||||
run();
|
||||
return false;
|
||||
@@ -120,84 +138,16 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
|
||||
*/
|
||||
@Override protected void handleFile(String remotePath, FTPFile ftpFile) {
|
||||
super.handleFile(remotePath, ftpFile);
|
||||
if (ftpFile != null) {
|
||||
//远程文件已完成
|
||||
if (ftpFile.getSize() == mEntity.getFileSize()) {
|
||||
isComplete = true;
|
||||
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】");
|
||||
} else if (ftpFile.getSize() == 0) {
|
||||
mTaskWrapper.setNewTask(true);
|
||||
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】,但文件长度为0,重新上传该文件");
|
||||
} else {
|
||||
ALog.w(TAG, "FTP服务器已存在未完成的文件【"
|
||||
+ ftpFile.getName()
|
||||
+ ",size: "
|
||||
+ ftpFile.getSize()
|
||||
+ "】"
|
||||
+ "尝试从位置:"
|
||||
+ (ftpFile.getSize() - 1)
|
||||
+ "开始上传");
|
||||
mTaskWrapper.setNewTask(false);
|
||||
|
||||
// 修改记录
|
||||
TaskRecord record = DbDataHelper.getTaskRecord(mTaskWrapper.getKey(),
|
||||
mTaskWrapper.getEntity().getTaskType());
|
||||
if (record == null) {
|
||||
record = new TaskRecord();
|
||||
record.fileName = mEntity.getFileName();
|
||||
record.filePath = mTaskWrapper.getKey();
|
||||
record.threadRecords = new ArrayList<>();
|
||||
}
|
||||
ThreadRecord threadRecord;
|
||||
if (record.threadRecords == null || record.threadRecords.isEmpty()) {
|
||||
threadRecord = new ThreadRecord();
|
||||
threadRecord.taskKey = record.filePath;
|
||||
} else {
|
||||
threadRecord = record.threadRecords.get(0);
|
||||
}
|
||||
//修改本地保存的停止地址为服务器上对应文件的大小
|
||||
threadRecord.startLocation = ftpFile.getSize() - 1;
|
||||
record.save();
|
||||
threadRecord.save();
|
||||
}
|
||||
}else {
|
||||
ALog.d(TAG, "FTP服务器上不存在该文件");
|
||||
mTaskWrapper.setNewTask(false);
|
||||
TaskRecord record = DbDataHelper.getTaskRecord(mTaskWrapper.getKey(),
|
||||
mTaskWrapper.getEntity().getTaskType());
|
||||
if (record != null){
|
||||
ALog.w(TAG, String.format("任务记录【%s】已存在,将删除该记录", mTaskWrapper.getKey()));
|
||||
delTaskRecord(record);
|
||||
}
|
||||
mTaskWrapper.setNewTask(true);
|
||||
record = new TaskRecord();
|
||||
record.fileName = mEntity.getFileName();
|
||||
record.filePath = mTaskWrapper.getKey();
|
||||
record.threadRecords = new ArrayList<>();
|
||||
ThreadRecord threadRecord = new ThreadRecord();
|
||||
threadRecord.taskKey = record.filePath;
|
||||
threadRecord.startLocation = 0;
|
||||
threadRecord.endLocation = mEntity.getFileSize();
|
||||
threadRecord.isComplete = false;
|
||||
|
||||
record.save();
|
||||
threadRecord.save();
|
||||
this.ftpFile = ftpFile;
|
||||
if (ftpFile != null && ftpFile.getSize() == mEntity.getFileSize()) {
|
||||
isComplete = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void delTaskRecord(TaskRecord record){
|
||||
List<ThreadRecord> threadRecords = record.threadRecords;
|
||||
if (threadRecords != null && !threadRecords.isEmpty()){
|
||||
for (ThreadRecord tr : threadRecords){
|
||||
tr.deleteData();
|
||||
}
|
||||
}
|
||||
record.deleteData();
|
||||
}
|
||||
|
||||
@Override protected void onPreComplete(int code) {
|
||||
super.onPreComplete(code);
|
||||
callback.onSucceed(mEntity.getKey(),
|
||||
new CompleteInfo(isComplete ? CODE_COMPLETE : code, mTaskWrapper));
|
||||
CompleteInfo info = new CompleteInfo(isComplete ? CODE_COMPLETE : code, mTaskWrapper);
|
||||
info.obj = ftpFile;
|
||||
callback.onSucceed(mEntity.getKey(), info);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,19 +15,56 @@
|
||||
*/
|
||||
package com.arialyy.aria.ftp.upload;
|
||||
|
||||
import android.os.Handler;
|
||||
import aria.apache.commons.net.ftp.FTPFile;
|
||||
import com.arialyy.aria.core.common.AbsEntity;
|
||||
import com.arialyy.aria.core.common.CompleteInfo;
|
||||
import com.arialyy.aria.core.listener.IEventListener;
|
||||
import com.arialyy.aria.core.loader.IInfoTask;
|
||||
import com.arialyy.aria.core.loader.IRecordHandler;
|
||||
import com.arialyy.aria.core.loader.NormalLoader;
|
||||
import com.arialyy.aria.core.manager.ThreadTaskManager;
|
||||
import com.arialyy.aria.core.task.IThreadTask;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import com.arialyy.aria.exception.BaseException;
|
||||
|
||||
final class FtpULoader extends NormalLoader {
|
||||
private FTPFile ftpFile;
|
||||
|
||||
FtpULoader(AbsTaskWrapper wrapper, IEventListener listener) {
|
||||
super(wrapper, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startThreadTask() {
|
||||
// 检查记录
|
||||
((FtpURecordHandler) mRecordHandler).setFtpFile(ftpFile);
|
||||
if (mRecordHandler.checkTaskCompleted()) {
|
||||
mRecord.deleteData();
|
||||
isComplete = true;
|
||||
getListener().onComplete();
|
||||
return;
|
||||
}
|
||||
mRecord = mRecordHandler.getRecord(getFileSize());
|
||||
|
||||
// 初始化线程状态管理器
|
||||
mStateManager.setLooper(mRecord, getLooper());
|
||||
getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord,
|
||||
new Handler(getLooper(), mStateManager.getHandlerCallback())));
|
||||
mStateManager.updateCurrentProgress(getEntity().getCurrentProgress());
|
||||
|
||||
if (mStateManager.getCurrentProgress() > 0) {
|
||||
getListener().onResume(mStateManager.getCurrentProgress());
|
||||
} else {
|
||||
getListener().onStart(mStateManager.getCurrentProgress());
|
||||
}
|
||||
|
||||
// 启动线程任务
|
||||
for (IThreadTask threadTask : getTaskList()) {
|
||||
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void addComponent(IInfoTask infoTask) {
|
||||
mInfoTask = infoTask;
|
||||
infoTask.setCallback(new IInfoTask.Callback() {
|
||||
@@ -35,6 +72,7 @@ final class FtpULoader extends NormalLoader {
|
||||
if (info.code == FtpUFileInfoTask.CODE_COMPLETE) {
|
||||
getListener().onComplete();
|
||||
} else {
|
||||
ftpFile = (FTPFile) info.obj;
|
||||
startThreadTask();
|
||||
}
|
||||
}
|
||||
@@ -44,4 +82,8 @@ final class FtpULoader extends NormalLoader {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override public void addComponent(IRecordHandler recordHandler) {
|
||||
mRecordHandler = recordHandler;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import com.arialyy.aria.core.loader.LoaderStructure;
|
||||
import com.arialyy.aria.core.loader.NormalThreadStateManager;
|
||||
import com.arialyy.aria.core.upload.UTaskWrapper;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import com.arialyy.aria.ftp.FtpRecordHandler;
|
||||
import com.arialyy.aria.ftp.FtpTaskOption;
|
||||
|
||||
/**
|
||||
@@ -42,7 +41,7 @@ public final class FtpULoaderUtil extends AbsNormalLoaderUtil {
|
||||
|
||||
@Override public LoaderStructure BuildLoaderStructure() {
|
||||
LoaderStructure structure = new LoaderStructure();
|
||||
structure.addComponent(new FtpRecordHandler(getTaskWrapper()))
|
||||
structure.addComponent(new FtpURecordHandler((UTaskWrapper) getTaskWrapper()))
|
||||
.addComponent(new NormalThreadStateManager(getListener()))
|
||||
.addComponent(new FtpUFileInfoTask((UTaskWrapper) getTaskWrapper()))
|
||||
.addComponent(new FtpUTTBuilder(getTaskWrapper()));
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.ftp.upload;
|
||||
|
||||
import aria.apache.commons.net.ftp.FTPFile;
|
||||
import com.arialyy.aria.core.TaskRecord;
|
||||
import com.arialyy.aria.core.ThreadRecord;
|
||||
import com.arialyy.aria.core.common.RecordHandler;
|
||||
import com.arialyy.aria.core.upload.UTaskWrapper;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.RecordUtil;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 上传任务记录处理器
|
||||
*/
|
||||
final class FtpURecordHandler extends RecordHandler {
|
||||
private FTPFile ftpFile;
|
||||
|
||||
FtpURecordHandler(UTaskWrapper wrapper) {
|
||||
super(wrapper);
|
||||
}
|
||||
|
||||
void setFtpFile(FTPFile ftpFile) {
|
||||
this.ftpFile = ftpFile;
|
||||
}
|
||||
|
||||
@Override public void handlerTaskRecord(TaskRecord record) {
|
||||
if (record.threadRecords == null || record.threadRecords.isEmpty()) {
|
||||
record.threadRecords = new ArrayList<>();
|
||||
record.threadRecords.add(
|
||||
createThreadRecord(record, 0, ftpFile == null ? 0 : ftpFile.getSize(), getFileSize()));
|
||||
}
|
||||
|
||||
if (ftpFile != null) {
|
||||
//远程文件已完成
|
||||
if (ftpFile.getSize() == getFileSize()) {
|
||||
record.threadRecords.get(0).isComplete = true;
|
||||
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】");
|
||||
} else if (ftpFile.getSize() == 0) {
|
||||
getWrapper().setNewTask(true);
|
||||
ALog.d(TAG, "FTP服务器上已存在该文件【" + ftpFile.getName() + "】,但文件长度为0,重新上传该文件");
|
||||
} else {
|
||||
ALog.w(TAG, "FTP服务器已存在未完成的文件【"
|
||||
+ ftpFile.getName()
|
||||
+ ",size: "
|
||||
+ ftpFile.getSize()
|
||||
+ "】"
|
||||
+ "尝试从位置:"
|
||||
+ (ftpFile.getSize() - 1)
|
||||
+ "开始上传");
|
||||
getWrapper().setNewTask(false);
|
||||
|
||||
// 修改记录
|
||||
ThreadRecord threadRecord = record.threadRecords.get(0);
|
||||
//修改本地保存的停止地址为服务器上对应文件的大小
|
||||
threadRecord.startLocation = ftpFile.getSize() - 1;
|
||||
}
|
||||
} else {
|
||||
ALog.d(TAG, "FTP服务器上不存在该文件");
|
||||
getWrapper().setNewTask(true);
|
||||
ThreadRecord tr = record.threadRecords.get(0);
|
||||
tr.startLocation = 0;
|
||||
tr.endLocation = getFileSize();
|
||||
tr.isComplete = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThreadRecord createThreadRecord(TaskRecord record, int threadId, long startL, long endL) {
|
||||
ThreadRecord tr;
|
||||
tr = new ThreadRecord();
|
||||
tr.taskKey = record.filePath;
|
||||
tr.threadId = threadId;
|
||||
tr.startLocation = startL;
|
||||
tr.isComplete = false;
|
||||
tr.threadType = getWrapper().getEntity().getTaskType();
|
||||
tr.endLocation = getFileSize();
|
||||
tr.blockLen = RecordUtil.getBlockLen(getFileSize(), threadId, record.threadNum);
|
||||
return tr;
|
||||
}
|
||||
|
||||
@Override public TaskRecord createTaskRecord(int threadNum) {
|
||||
TaskRecord record = new TaskRecord();
|
||||
record.fileName = getEntity().getFileName();
|
||||
record.filePath = getEntity().getFilePath();
|
||||
record.threadRecords = new ArrayList<>();
|
||||
record.threadNum = threadNum;
|
||||
record.isBlock = false;
|
||||
record.taskType = getWrapper().getEntity().getTaskType();
|
||||
record.isGroupRecord = getEntity().isGroupChild();
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Override public int initTaskThreadNum() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user