添加文件名适配器接口,修复group中useServerFileName未同步到子task的问题

pull/617/head
DaveBoy1994 5 years ago
parent e015c2a2f7
commit 63ec23f241
  1. 10
      Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java
  2. 13
      HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java
  3. 15
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java
  4. 5
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java
  5. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java
  6. 37
      PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileNameAdapter.java
  7. 6
      PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java
  8. 2
      app/src/main/java/com/arialyy/simple/MainActivity.java

@ -17,6 +17,7 @@ package com.arialyy.aria.core.common;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.processor.IHttpFileLenAdapter; import com.arialyy.aria.core.processor.IHttpFileLenAdapter;
import com.arialyy.aria.core.processor.IHttpFileNameAdapter;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import java.net.Proxy; import java.net.Proxy;
@ -35,6 +36,7 @@ public class HttpOption extends BaseOption {
private Proxy proxy; private Proxy proxy;
private boolean useServerFileName = false; private boolean useServerFileName = false;
private IHttpFileLenAdapter fileLenAdapter; private IHttpFileLenAdapter fileLenAdapter;
private IHttpFileNameAdapter fileNameAdapter;
private String attachment; private String attachment;
public HttpOption() { public HttpOption() {
@ -168,4 +170,12 @@ public class HttpOption extends BaseOption {
this.fileLenAdapter = fileLenAdapter; this.fileLenAdapter = fileLenAdapter;
return this; return this;
} }
public HttpOption setFilNameAdapter(IHttpFileNameAdapter fileNameAdapter) {
if (fileNameAdapter == null) {
throw new IllegalArgumentException("adapter为空");
}
CheckUtil.checkMemberClass(fileNameAdapter.getClass());
this.fileNameAdapter = fileNameAdapter;
return this;
}
} }

@ -20,6 +20,8 @@ import android.text.TextUtils;
import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.inf.ITaskOption; import com.arialyy.aria.core.inf.ITaskOption;
import com.arialyy.aria.core.processor.IHttpFileLenAdapter; import com.arialyy.aria.core.processor.IHttpFileLenAdapter;
import com.arialyy.aria.core.processor.IHttpFileNameAdapter;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.net.CookieManager; import java.net.CookieManager;
import java.net.Proxy; import java.net.Proxy;
@ -81,17 +83,23 @@ public final class HttpTaskOption implements ITaskOption {
private SoftReference<IHttpFileLenAdapter> fileLenAdapter; private SoftReference<IHttpFileLenAdapter> fileLenAdapter;
private SoftReference<IHttpFileNameAdapter> fileNameAdapter;
public IHttpFileLenAdapter getFileLenAdapter() { public IHttpFileLenAdapter getFileLenAdapter() {
return fileLenAdapter == null ? null : fileLenAdapter.get(); return fileLenAdapter == null ? null : fileLenAdapter.get();
} }
public IHttpFileNameAdapter getFileNameAdapter() {
return fileNameAdapter == null ? null : fileNameAdapter.get();
}
/** /**
* 如果是匿名内部类完成后需要将adapter设置为空否则会出现内存泄漏 * 如果是匿名内部类完成后需要将adapter设置为空否则会出现内存泄漏
*/ */
public void setFileLenAdapter(IHttpFileLenAdapter fileLenAdapter) { public void setFileLenAdapter(IHttpFileLenAdapter fileLenAdapter) {
this.fileLenAdapter = new SoftReference<>(fileLenAdapter); this.fileLenAdapter = new SoftReference<>(fileLenAdapter);
} }
public void setFileNameAdapter(IHttpFileNameAdapter fileNameAdapter) {
this.fileNameAdapter = new SoftReference<>(fileNameAdapter);
}
public Map<String, String> getFormFields() { public Map<String, String> getFormFields() {
return formFields; return formFields;
} }
@ -179,4 +187,5 @@ public final class HttpTaskOption implements ITaskOption {
public void setParams(Map<String, String> params) { public void setParams(Map<String, String> params) {
this.params = params; this.params = params;
} }
} }

@ -158,10 +158,21 @@ final class HttpDFileInfoTask implements IInfoTask, Runnable {
if (!TextUtils.isEmpty(disposition)) { if (!TextUtils.isEmpty(disposition)) {
mEntity.setDisposition(CommonUtil.encryptBASE64(disposition)); mEntity.setDisposition(CommonUtil.encryptBASE64(disposition));
handleContentDisposition(disposition); handleContentDisposition(disposition);
} else { } else{
ALog.w(TAG, "Content-Disposition对于端字段为空,使用服务器端文件名失败"); if(taskOption.getFileNameAdapter()!=null) {
String newName =taskOption.getFileNameAdapter().handleFileName(headers,mEntity.getKey());
mEntity.setServerFileName(newName);
renameFile(newName);
}else if(conn.getHeaderField("Content-Type")!=null){
String contentType=conn.getHeaderField("Content-Type");
String type=contentType.substring(contentType.indexOf(File.separator)+1);
String newName =mEntity.getFileName()+File.separator+type;
mEntity.setServerFileName(newName);
renameFile(newName);
}
} }
} }
CookieManager msCookieManager = new CookieManager(); CookieManager msCookieManager = new CookieManager();
List<String> cookiesHeader = headers.get("Set-Cookie"); List<String> cookiesHeader = headers.get("Set-Cookie");

@ -155,6 +155,11 @@ public final class HttpDGInfoTask implements IInfoTask {
// 设置属性 // 设置属性
subOption.setFileLenAdapter(groupOption.getFileLenAdapter()); subOption.setFileLenAdapter(groupOption.getFileLenAdapter());
subOption.setFileNameAdapter(groupOption.getFileNameAdapter());
subOption.setUseServerFileName(groupOption.isUseServerFileName());
subOption.setFileNameAdapter(groupOption.getFileNameAdapter());
subOption.setRequestEnum(groupOption.getRequestEnum()); subOption.setRequestEnum(groupOption.getRequestEnum());
subOption.setHeaders(groupOption.getHeaders()); subOption.setHeaders(groupOption.getHeaders());
subOption.setProxy(groupOption.getProxy()); subOption.setProxy(groupOption.getProxy());

@ -22,6 +22,7 @@ import com.arialyy.aria.core.processor.FtpInterceptHandler;
import com.arialyy.aria.core.processor.IBandWidthUrlConverter; import com.arialyy.aria.core.processor.IBandWidthUrlConverter;
import com.arialyy.aria.core.processor.IFtpUploadInterceptor; import com.arialyy.aria.core.processor.IFtpUploadInterceptor;
import com.arialyy.aria.core.processor.IHttpFileLenAdapter; import com.arialyy.aria.core.processor.IHttpFileLenAdapter;
import com.arialyy.aria.core.processor.IHttpFileNameAdapter;
import com.arialyy.aria.core.processor.IKeyUrlConverter; import com.arialyy.aria.core.processor.IKeyUrlConverter;
import com.arialyy.aria.core.processor.ILiveTsUrlConverter; import com.arialyy.aria.core.processor.ILiveTsUrlConverter;
import com.arialyy.aria.core.processor.ITsMergeHandler; import com.arialyy.aria.core.processor.ITsMergeHandler;
@ -59,6 +60,7 @@ public class TaskOptionParams {
PROCESSORES.add(IBandWidthUrlConverter.class); PROCESSORES.add(IBandWidthUrlConverter.class);
PROCESSORES.add(IFtpUploadInterceptor.class); PROCESSORES.add(IFtpUploadInterceptor.class);
PROCESSORES.add(IHttpFileLenAdapter.class); PROCESSORES.add(IHttpFileLenAdapter.class);
PROCESSORES.add(IHttpFileNameAdapter.class);
PROCESSORES.add(ILiveTsUrlConverter.class); PROCESSORES.add(ILiveTsUrlConverter.class);
PROCESSORES.add(ITsMergeHandler.class); PROCESSORES.add(ITsMergeHandler.class);
PROCESSORES.add(IVodTsUrlConverter.class); PROCESSORES.add(IVodTsUrlConverter.class);

@ -0,0 +1,37 @@
/*
* 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.processor;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IEventHandler;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* Http文件名适配器
*/
public interface IHttpFileNameAdapter extends IEventHandler {
/**
* 根据header中的数据获取文件名字
* @param headers header参数{@link URLConnection#getHeaderFields()}
* @param key 这里如果传entity可能会导致entity属性变更而其他地方没有改变所以就传了个key 减少可操作范围
* @return 文件长度
*/
String handleFileName(Map<String, List<String>> headers, String key);
}

@ -105,7 +105,11 @@ public class DbDataHelper {
entity.setUrl(url); entity.setUrl(url);
entity.setFilePath(groupHash + "_" + i); entity.setFilePath(groupHash + "_" + i);
int lastIndex = url.lastIndexOf(File.separator); int lastIndex = url.lastIndexOf(File.separator);
entity.setFileName(url.substring(lastIndex + 1)); //去除url末尾携带的的参数
int endIndex = url.lastIndexOf("?");
if(endIndex<0)endIndex=url.length();
entity.setFileName(url.substring(lastIndex + 1,endIndex));
entity.setGroupHash(groupHash); entity.setGroupHash(groupHash);
entity.setGroupChild(true); entity.setGroupChild(true);
list.add(entity); list.add(entity);

@ -142,7 +142,7 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
T.showShort(MainActivity.this, "没有文件读写权限"); T.showShort(MainActivity.this, "没有文件读写权限");
finish(); finish();
} }
}, Manifest.permission.WRITE_EXTERNAL_STORAGE); }, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);
} }
} }
} }

Loading…
Cancel
Save