Browse Source

1. 增加下载断点续传功能

2. 增加流媒体播放功能
main v2.4.0
zhouxin 4 years ago
parent
commit
55dfacd3ee
  1. 23
      src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java
  2. 7
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java
  3. 28
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java
  4. 16
      src/main/java/net/sf/webdav/methods/DoGet.java

23
src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java

@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -93,16 +94,32 @@ public class AliYunDriverClient {
} }
public InputStream download(String url, String range) { public Response download(String url, HttpServletRequest httpServletRequest, long size ) {
Request.Builder builder = new Request.Builder().header("referer", "https://www.aliyundrive.com/"); Request.Builder builder = new Request.Builder().header("referer", "https://www.aliyundrive.com/");
if (StringUtils.hasLength(range)) { String range = httpServletRequest.getHeader("range");
if (range != null) {
// 如果range最后 >= size, 则去掉
String[] split = range.split("-");
if (split.length == 2) {
String end = split[1];
if (Long.parseLong(end) >= size) {
range = range.substring(0, range.lastIndexOf('-') + 1);
}
}
builder.header("range", range); builder.header("range", range);
} }
String ifRange = httpServletRequest.getHeader("if-range");
if (ifRange != null) {
builder.header("if-range", ifRange);
}
Request request = builder.url(url).build(); Request request = builder.url(url).build();
Response response = null; Response response = null;
try { try {
response = okHttpClient.newCall(request).execute(); response = okHttpClient.newCall(request).execute();
return response.body().byteStream(); return response;
} catch (IOException e) { } catch (IOException e) {
throw new WebdavException(e); throw new WebdavException(e);
} }

7
src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java

@ -12,6 +12,7 @@ import com.github.zxbu.webdavteambition.model.result.UploadPreResult;
import com.github.zxbu.webdavteambition.util.JsonUtil; import com.github.zxbu.webdavteambition.util.JsonUtil;
import net.sf.webdav.exceptions.WebdavException; import net.sf.webdav.exceptions.WebdavException;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Response;
import org.apache.tomcat.util.http.fileupload.IOUtils; import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -19,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
@ -279,14 +281,15 @@ public class AliYunDriverClientService {
return getNodeIdByPath2(path); return getNodeIdByPath2(path);
} }
public InputStream download(String path, String range) { public Response download(String path, HttpServletRequest request, long size ) {
TFile file = getTFileByPath(path); TFile file = getTFileByPath(path);
DownloadRequest downloadRequest = new DownloadRequest(); DownloadRequest downloadRequest = new DownloadRequest();
downloadRequest.setDrive_id(client.getDriveId()); downloadRequest.setDrive_id(client.getDriveId());
downloadRequest.setFile_id(file.getFile_id()); downloadRequest.setFile_id(file.getFile_id());
String json = client.post("/file/get_download_url", downloadRequest); String json = client.post("/file/get_download_url", downloadRequest);
Object url = JsonUtil.getJsonNodeValue(json, "url"); Object url = JsonUtil.getJsonNodeValue(json, "url");
return client.download(url.toString(), range); LOGGER.debug("{} url = {}", path, url);
return client.download(url.toString(), request, size);
} }
private TFile getNodeIdByPath2(String path) { private TFile getNodeIdByPath2(String path) {

28
src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java

@ -8,6 +8,7 @@ import net.sf.webdav.IWebdavStore;
import net.sf.webdav.StoredObject; import net.sf.webdav.StoredObject;
import net.sf.webdav.Transaction; import net.sf.webdav.Transaction;
import net.sf.webdav.exceptions.WebdavException; import net.sf.webdav.exceptions.WebdavException;
import okhttp3.Response;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -17,6 +18,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.Principal; import java.security.Principal;
import java.util.Enumeration;
import java.util.Set; import java.util.Set;
public class AliYunDriverFileSystemStore implements IWebdavStore { public class AliYunDriverFileSystemStore implements IWebdavStore {
@ -42,23 +44,23 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override @Override
public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) { public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) {
LOGGER.info("begin"); LOGGER.debug("begin");
return new Transaction(principal, req, resp); return new Transaction(principal, req, resp);
} }
@Override @Override
public void checkAuthentication(ITransaction transaction) { public void checkAuthentication(ITransaction transaction) {
LOGGER.info("checkAuthentication"); LOGGER.debug("checkAuthentication");
} }
@Override @Override
public void commit(ITransaction transaction) { public void commit(ITransaction transaction) {
LOGGER.info("commit"); LOGGER.debug("commit");
} }
@Override @Override
public void rollback(ITransaction transaction) { public void rollback(ITransaction transaction) {
LOGGER.info("rollback"); LOGGER.debug("rollback");
} }
@ -78,8 +80,22 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override @Override
public InputStream getResourceContent(ITransaction transaction, String resourceUri) { public InputStream getResourceContent(ITransaction transaction, String resourceUri) {
LOGGER.info("getResourceContent: {}", resourceUri); LOGGER.info("getResourceContent: {}", resourceUri);
String range = transaction.getRequest().getHeader("range"); Enumeration<String> headerNames = transaction.getRequest().getHeaderNames();
return aliYunDriverClientService.download(resourceUri, range); while (headerNames.hasMoreElements()) {
String s = headerNames.nextElement();
LOGGER.debug("{} request: {} = {}",resourceUri, s, transaction.getRequest().getHeader(s));
}
HttpServletResponse response = transaction.getResponse();
long size = getResourceLength(transaction, resourceUri);
Response downResponse = aliYunDriverClientService.download(resourceUri, transaction.getRequest(), size);
response.setContentLengthLong(downResponse.body().contentLength());
LOGGER.debug("{} code = {}", resourceUri, downResponse.code());
for (String name : downResponse.headers().names()) {
LOGGER.debug("{} downResponse: {} = {}", resourceUri, name, downResponse.header(name));
response.addHeader(name, downResponse.header(name));
}
response.setStatus(downResponse.code());
return downResponse.body().byteStream();
} }

16
src/main/java/net/sf/webdav/methods/DoGet.java

@ -32,6 +32,7 @@ import net.sf.webdav.IWebdavStore;
import net.sf.webdav.StoredObject; import net.sf.webdav.StoredObject;
import net.sf.webdav.WebdavStatus; import net.sf.webdav.WebdavStatus;
import net.sf.webdav.locking.ResourceLocks; import net.sf.webdav.locking.ResourceLocks;
import org.apache.tomcat.util.http.fileupload.IOUtils;
public class DoGet extends DoHead { public class DoGet extends DoHead {
@ -62,12 +63,9 @@ public class DoGet extends DoHead {
InputStream in = _store.getResourceContent(transaction, path); InputStream in = _store.getResourceContent(transaction, path);
try { try {
if (in != null) { if (in != null) {
int read = -1; LOG.debug("开始 {}, ", path);
byte[] copyBuffer = new byte[BUF_SIZE]; IOUtils.copyLarge(in, out);
LOG.debug("结束 {}", path);
while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
out.write(copyBuffer, 0, read);
}
} }
} finally { } finally {
// flushing causes a IOE if a file is opened on the webserver // flushing causes a IOE if a file is opened on the webserver
@ -77,18 +75,20 @@ public class DoGet extends DoHead {
in.close(); in.close();
} }
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Closing InputStream causes Exception!\n" LOG.warn("{} Closing InputStream causes Exception!\n", path
,e); ,e);
} }
try { try {
out.flush(); out.flush();
out.close(); out.close();
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Flushing OutputStream causes Exception!\n" LOG.warn("{} Flushing OutputStream causes Exception!\n", path
,e); ,e);
} }
} }
} catch (Exception e) { } catch (Exception e) {
LOG.warn("{} doBody causes Exception!\n", path
,e);
LOG.trace(e.toString()); LOG.trace(e.toString());
} }
} }

Loading…
Cancel
Save