Browse Source

修复大文件上传失败的BUG

main v2.1.0
zhouxin 4 years ago
parent
commit
e52568a8ae
  1. 4
      src/main/java/com/github/zxbu/webdavteambition/filter/ErrorFilter.java
  2. 9
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java
  3. 33
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java
  4. 5
      src/main/java/net/sf/webdav/ITransaction.java
  5. 14
      src/main/java/net/sf/webdav/IWebdavStore.java
  6. 18
      src/main/java/net/sf/webdav/LocalFileSystemStore.java
  7. 32
      src/main/java/net/sf/webdav/Transaction.java
  8. 35
      src/main/java/net/sf/webdav/WebDavServletBean.java

4
src/main/java/com/github/zxbu/webdavteambition/filter/ErrorFilter.java

@ -22,7 +22,9 @@ public class ErrorFilter extends OncePerRequestFilter {
filterChain.doFilter(httpServletRequest, wrapperResponse); filterChain.doFilter(httpServletRequest, wrapperResponse);
if (wrapperResponse.hasErrorToSend()) { if (wrapperResponse.hasErrorToSend()) {
httpServletResponse.setStatus(wrapperResponse.getStatus()); httpServletResponse.setStatus(wrapperResponse.getStatus());
httpServletResponse.getWriter().write(wrapperResponse.getMessage()); if (wrapperResponse.getMessage() != null) {
httpServletResponse.getWriter().write(wrapperResponse.getMessage());
}
} }
httpServletResponse.flushBuffer(); httpServletResponse.flushBuffer();
} catch (Throwable t) { } catch (Throwable t) {

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

@ -4,9 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.zxbu.webdavteambition.client.AliYunDriverClient; import com.github.zxbu.webdavteambition.client.AliYunDriverClient;
import com.github.zxbu.webdavteambition.model.*; import com.github.zxbu.webdavteambition.model.*;
import com.github.zxbu.webdavteambition.model.result.CreateFileResult;
import com.github.zxbu.webdavteambition.model.result.TFileListResult;
import com.github.zxbu.webdavteambition.model.result.TFile; import com.github.zxbu.webdavteambition.model.result.TFile;
import com.github.zxbu.webdavteambition.model.result.TFileListResult;
import com.github.zxbu.webdavteambition.model.result.UploadPreResult; 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;
@ -86,10 +85,10 @@ public class AliYunDriverClientService {
} }
} }
public void uploadPre(String path, int size, InputStream inputStream) { public void uploadPre(String path, long size, InputStream inputStream) {
path = normalizingPath(path); path = normalizingPath(path);
PathInfo pathInfo = getPathInfo(path); PathInfo pathInfo = getPathInfo(path);
TFile parent = getTFileByPath(pathInfo.getParentPath()); TFile parent = getTFileByPath(pathInfo.getParentPath());
if (parent == null) { if (parent == null) {
return; return;
} }
@ -111,7 +110,7 @@ public class AliYunDriverClientService {
uploadPreRequest.setDrive_id(client.getDriveId()); uploadPreRequest.setDrive_id(client.getDriveId());
uploadPreRequest.setName(pathInfo.getName()); uploadPreRequest.setName(pathInfo.getName());
uploadPreRequest.setParent_file_id(parent.getFile_id()); uploadPreRequest.setParent_file_id(parent.getFile_id());
uploadPreRequest.setSize((long) size); uploadPreRequest.setSize(size);
List<UploadPreRequest.PartInfo> part_info_list = new ArrayList<>(); List<UploadPreRequest.PartInfo> part_info_list = new ArrayList<>();
for (int i = 0; i < chunkCount; i++) { for (int i = 0; i < chunkCount; i++) {
UploadPreRequest.PartInfo partInfo = new UploadPreRequest.PartInfo(); UploadPreRequest.PartInfo partInfo = new UploadPreRequest.PartInfo();

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

@ -6,14 +6,15 @@ import com.github.zxbu.webdavteambition.model.result.TFile;
import net.sf.webdav.ITransaction; import net.sf.webdav.ITransaction;
import net.sf.webdav.IWebdavStore; import net.sf.webdav.IWebdavStore;
import net.sf.webdav.StoredObject; import net.sf.webdav.StoredObject;
import net.sf.webdav.Transaction;
import net.sf.webdav.exceptions.WebdavException; import net.sf.webdav.exceptions.WebdavException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.Principal; import java.security.Principal;
import java.util.Set; import java.util.Set;
@ -40,16 +41,19 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
} }
@Override @Override
public ITransaction begin(Principal principal) { public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) {
LOGGER.debug("begin"); LOGGER.debug("begin");
aliYunDriverClientService.clearCache(); aliYunDriverClientService.clearCache();
return null; return new Transaction(principal, req, resp);
} }
@Override @Override
public void checkAuthentication(ITransaction transaction) { public void checkAuthentication(ITransaction transaction) {
LOGGER.debug("checkAuthentication"); LOGGER.debug("checkAuthentication");
// if (transaction.getPrincipal() == null) {
// throw new UnauthenticatedException(WebdavStatus.SC_UNAUTHORIZED);
// }
} }
@Override @Override
@ -86,21 +90,28 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override @Override
public long setResourceContent(ITransaction transaction, String resourceUri, InputStream content, String contentType, String characterEncoding) { public long setResourceContent(ITransaction transaction, String resourceUri, InputStream content, String contentType, String characterEncoding) {
LOGGER.info("setResourceContent {}", resourceUri); LOGGER.info("setResourceContent {}", resourceUri);
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = transaction.getRequest();
HttpServletRequest request = requestAttributes.getRequest(); HttpServletResponse response = transaction.getResponse();
int contentLength = request.getContentLength();
long contentLength = request.getContentLength();
if (contentLength < 0) { if (contentLength < 0) {
contentLength = 0; contentLength = Long.parseLong(request.getHeader("content-length"));
} }
aliYunDriverClientService.uploadPre(resourceUri, contentLength, content);
if (contentLength == 0) { if (contentLength == 0) {
String expect = request.getHeader("Expect"); String expect = request.getHeader("Expect");
// 支持大文件上传 // 支持大文件上传
if ("100-continue".equalsIgnoreCase(expect)) { if ("100-continue".equalsIgnoreCase(expect)) {
try {
response.sendError(100, "Continue");
} catch (IOException e) {
e.printStackTrace();
}
return 0; return 0;
} }
} }
aliYunDriverClientService.uploadPre(resourceUri, contentLength, content);
return contentLength; return contentLength;
} }
@ -155,6 +166,8 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override @Override
public StoredObject getStoredObject(ITransaction transaction, String uri) { public StoredObject getStoredObject(ITransaction transaction, String uri) {
LOGGER.debug("getStoredObject: {}", uri); LOGGER.debug("getStoredObject: {}", uri);
TFile tFile = aliYunDriverClientService.getTFileByPath(uri); TFile tFile = aliYunDriverClientService.getTFileByPath(uri);
if (tFile != null) { if (tFile != null) {

5
src/main/java/net/sf/webdav/ITransaction.java

@ -1,9 +1,14 @@
package net.sf.webdav; package net.sf.webdav;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal; import java.security.Principal;
public interface ITransaction { public interface ITransaction {
Principal getPrincipal(); Principal getPrincipal();
HttpServletRequest getRequest();
HttpServletResponse getResponse();
} }

14
src/main/java/net/sf/webdav/IWebdavStore.java

@ -23,11 +23,13 @@
package net.sf.webdav; package net.sf.webdav;
import net.sf.webdav.exceptions.WebdavException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream; import java.io.InputStream;
import java.security.Principal; import java.security.Principal;
import net.sf.webdav.exceptions.WebdavException;
/** /**
* Interface for simple implementation of any store for the WebdavServlet * Interface for simple implementation of any store for the WebdavServlet
* <p> * <p>
@ -48,15 +50,15 @@ public interface IWebdavStore {
* or {@link #rollback()}. If only non-read methods have been called, the * or {@link #rollback()}. If only non-read methods have been called, the
* request will be terminated by a {@link #commit()}. This method will be * request will be terminated by a {@link #commit()}. This method will be
* called by (@link WebdavStoreAdapter} at the beginning of each request. * called by (@link WebdavStoreAdapter} at the beginning of each request.
* *
* *
* @param principal * @param principal
* the principal that started this request or <code>null</code> if * the principal that started this request or <code>null</code> if
* there is non available * there is non available
* *
* @throws WebdavException * @throws WebdavException
*/ */
ITransaction begin(Principal principal); ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp);
/** /**
* Checks if authentication information passed in is valid. If not throws an * Checks if authentication information passed in is valid. If not throws an

18
src/main/java/net/sf/webdav/LocalFileSystemStore.java

@ -15,22 +15,16 @@
*/ */
package net.sf.webdav; package net.sf.webdav;
import java.io.BufferedInputStream; import net.sf.webdav.exceptions.WebdavException;
import java.io.BufferedOutputStream;
import java.io.File; import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream; import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import net.sf.webdav.exceptions.UnauthenticatedException;
import net.sf.webdav.exceptions.WebdavException;
/** /**
* Reference Implementation of WebdavStore * Reference Implementation of WebdavStore
* *
@ -54,7 +48,7 @@ public class LocalFileSystemStore implements IWebdavStore {
; ;
} }
public ITransaction begin(Principal principal) throws WebdavException { public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) throws WebdavException {
LOG.info("LocalFileSystemStore.begin()"); LOG.info("LocalFileSystemStore.begin()");
if (!_root.exists()) { if (!_root.exists()) {
if (!_root.mkdirs()) { if (!_root.mkdirs()) {

32
src/main/java/net/sf/webdav/Transaction.java

@ -0,0 +1,32 @@
package net.sf.webdav;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
public class Transaction implements ITransaction {
private final Principal principal;
private final HttpServletRequest request;
private final HttpServletResponse response;
public Transaction(Principal principal, HttpServletRequest request, HttpServletResponse response) {
this.principal = principal;
this.request = request;
this.response = response;
}
@Override
public Principal getPrincipal() {
return principal;
}
@Override
public HttpServletRequest getRequest() {
return request;
}
@Override
public HttpServletResponse getResponse() {
return response;
}
}

35
src/main/java/net/sf/webdav/WebDavServletBean.java

@ -1,5 +1,15 @@
package net.sf.webdav; package net.sf.webdav;
import net.sf.webdav.exceptions.UnauthenticatedException;
import net.sf.webdav.exceptions.WebdavException;
import net.sf.webdav.fromcatalina.MD5Encoder;
import net.sf.webdav.locking.ResourceLocks;
import net.sf.webdav.methods.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -7,29 +17,6 @@ import java.security.Principal;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.webdav.exceptions.UnauthenticatedException;
import net.sf.webdav.exceptions.WebdavException;
import net.sf.webdav.fromcatalina.MD5Encoder;
import net.sf.webdav.locking.ResourceLocks;
import net.sf.webdav.methods.DoCopy;
import net.sf.webdav.methods.DoDelete;
import net.sf.webdav.methods.DoGet;
import net.sf.webdav.methods.DoHead;
import net.sf.webdav.methods.DoLock;
import net.sf.webdav.methods.DoMkcol;
import net.sf.webdav.methods.DoMove;
import net.sf.webdav.methods.DoNotImplemented;
import net.sf.webdav.methods.DoOptions;
import net.sf.webdav.methods.DoPropfind;
import net.sf.webdav.methods.DoProppatch;
import net.sf.webdav.methods.DoPut;
import net.sf.webdav.methods.DoUnlock;
public class WebDavServletBean extends HttpServlet { public class WebDavServletBean extends HttpServlet {
private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory
@ -124,7 +111,7 @@ public class WebDavServletBean extends HttpServlet {
try { try {
Principal userPrincipal = getUserPrincipal(req); Principal userPrincipal = getUserPrincipal(req);
transaction = _store.begin(userPrincipal); transaction = _store.begin(userPrincipal, req, resp);
needRollback = true; needRollback = true;
_store.checkAuthentication(transaction); _store.checkAuthentication(transaction);
resp.setStatus(WebdavStatus.SC_OK); resp.setStatus(WebdavStatus.SC_OK);

Loading…
Cancel
Save