From 55dfacd3ee010d77cff9affe5e133629c206df40 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sat, 24 Jul 2021 21:50:37 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=AD=E7=82=B9=E7=BB=AD=E4=BC=A0=E5=8A=9F=E8=83=BD=202.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=81=E5=AA=92=E4=BD=93=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/AliYunDriverClient.java | 23 +++++++++++++-- .../store/AliYunDriverClientService.java | 7 +++-- .../store/AliYunDriverFileSystemStore.java | 28 +++++++++++++++---- .../java/net/sf/webdav/methods/DoGet.java | 16 +++++------ 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java b/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java index a57444f..7b5519a 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java +++ b/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.StringUtils; +import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.InputStream; 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/"); - 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); } + + String ifRange = httpServletRequest.getHeader("if-range"); + if (ifRange != null) { + builder.header("if-range", ifRange); + } + + Request request = builder.url(url).build(); Response response = null; try { response = okHttpClient.newCall(request).execute(); - return response.body().byteStream(); + return response; } catch (IOException e) { throw new WebdavException(e); } diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java index 05bfae6..4cc7046 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java +++ b/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 net.sf.webdav.exceptions.WebdavException; import okhttp3.HttpUrl; +import okhttp3.Response; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.InputStream; import java.util.*; @@ -279,14 +281,15 @@ public class AliYunDriverClientService { return getNodeIdByPath2(path); } - public InputStream download(String path, String range) { + public Response download(String path, HttpServletRequest request, long size ) { TFile file = getTFileByPath(path); DownloadRequest downloadRequest = new DownloadRequest(); downloadRequest.setDrive_id(client.getDriveId()); downloadRequest.setFile_id(file.getFile_id()); String json = client.post("/file/get_download_url", downloadRequest); 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) { diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java index 33c1149..2c4f8ea 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java +++ b/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.Transaction; import net.sf.webdav.exceptions.WebdavException; +import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,6 +18,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.security.Principal; +import java.util.Enumeration; import java.util.Set; public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -42,23 +44,23 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @Override public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) { - LOGGER.info("begin"); + LOGGER.debug("begin"); return new Transaction(principal, req, resp); } @Override public void checkAuthentication(ITransaction transaction) { - LOGGER.info("checkAuthentication"); + LOGGER.debug("checkAuthentication"); } @Override public void commit(ITransaction transaction) { - LOGGER.info("commit"); + LOGGER.debug("commit"); } @Override public void rollback(ITransaction transaction) { - LOGGER.info("rollback"); + LOGGER.debug("rollback"); } @@ -78,8 +80,22 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @Override public InputStream getResourceContent(ITransaction transaction, String resourceUri) { LOGGER.info("getResourceContent: {}", resourceUri); - String range = transaction.getRequest().getHeader("range"); - return aliYunDriverClientService.download(resourceUri, range); + Enumeration headerNames = transaction.getRequest().getHeaderNames(); + 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(); } diff --git a/src/main/java/net/sf/webdav/methods/DoGet.java b/src/main/java/net/sf/webdav/methods/DoGet.java index 880a168..e11dafb 100644 --- a/src/main/java/net/sf/webdav/methods/DoGet.java +++ b/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.WebdavStatus; import net.sf.webdav.locking.ResourceLocks; +import org.apache.tomcat.util.http.fileupload.IOUtils; public class DoGet extends DoHead { @@ -62,12 +63,9 @@ public class DoGet extends DoHead { InputStream in = _store.getResourceContent(transaction, path); try { if (in != null) { - int read = -1; - byte[] copyBuffer = new byte[BUF_SIZE]; - - while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) { - out.write(copyBuffer, 0, read); - } + LOG.debug("开始 {}, ", path); + IOUtils.copyLarge(in, out); + LOG.debug("结束 {}", path); } } finally { // flushing causes a IOE if a file is opened on the webserver @@ -77,18 +75,20 @@ public class DoGet extends DoHead { in.close(); } } catch (Exception e) { - LOG.warn("Closing InputStream causes Exception!\n" + LOG.warn("{} Closing InputStream causes Exception!\n", path ,e); } try { out.flush(); out.close(); } catch (Exception e) { - LOG.warn("Flushing OutputStream causes Exception!\n" + LOG.warn("{} Flushing OutputStream causes Exception!\n", path ,e); } } } catch (Exception e) { + LOG.warn("{} doBody causes Exception!\n", path + ,e); LOG.trace(e.toString()); } }