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 { @@ -22,7 +22,9 @@ public class ErrorFilter extends OncePerRequestFilter {
filterChain.doFilter(httpServletRequest, wrapperResponse);
if (wrapperResponse.hasErrorToSend()) {
httpServletResponse.setStatus(wrapperResponse.getStatus());
httpServletResponse.getWriter().write(wrapperResponse.getMessage());
if (wrapperResponse.getMessage() != null) {
httpServletResponse.getWriter().write(wrapperResponse.getMessage());
}
}
httpServletResponse.flushBuffer();
} 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; @@ -4,9 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.zxbu.webdavteambition.client.AliYunDriverClient;
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.TFileListResult;
import com.github.zxbu.webdavteambition.model.result.UploadPreResult;
import com.github.zxbu.webdavteambition.util.JsonUtil;
import net.sf.webdav.exceptions.WebdavException;
@ -86,10 +85,10 @@ public class AliYunDriverClientService { @@ -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);
PathInfo pathInfo = getPathInfo(path);
TFile parent = getTFileByPath(pathInfo.getParentPath());
TFile parent = getTFileByPath(pathInfo.getParentPath());
if (parent == null) {
return;
}
@ -111,7 +110,7 @@ public class AliYunDriverClientService { @@ -111,7 +110,7 @@ public class AliYunDriverClientService {
uploadPreRequest.setDrive_id(client.getDriveId());
uploadPreRequest.setName(pathInfo.getName());
uploadPreRequest.setParent_file_id(parent.getFile_id());
uploadPreRequest.setSize((long) size);
uploadPreRequest.setSize(size);
List<UploadPreRequest.PartInfo> part_info_list = new ArrayList<>();
for (int i = 0; i < chunkCount; i++) {
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; @@ -6,14 +6,15 @@ import com.github.zxbu.webdavteambition.model.result.TFile;
import net.sf.webdav.ITransaction;
import net.sf.webdav.IWebdavStore;
import net.sf.webdav.StoredObject;
import net.sf.webdav.Transaction;
import net.sf.webdav.exceptions.WebdavException;
import org.slf4j.Logger;
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.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.Principal;
import java.util.Set;
@ -40,16 +41,19 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -40,16 +41,19 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
}
@Override
public ITransaction begin(Principal principal) {
public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) {
LOGGER.debug("begin");
aliYunDriverClientService.clearCache();
return null;
return new Transaction(principal, req, resp);
}
@Override
public void checkAuthentication(ITransaction transaction) {
LOGGER.debug("checkAuthentication");
// if (transaction.getPrincipal() == null) {
// throw new UnauthenticatedException(WebdavStatus.SC_UNAUTHORIZED);
// }
}
@Override
@ -86,21 +90,28 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -86,21 +90,28 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public long setResourceContent(ITransaction transaction, String resourceUri, InputStream content, String contentType, String characterEncoding) {
LOGGER.info("setResourceContent {}", resourceUri);
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
int contentLength = request.getContentLength();
HttpServletRequest request = transaction.getRequest();
HttpServletResponse response = transaction.getResponse();
long contentLength = request.getContentLength();
if (contentLength < 0) {
contentLength = 0;
contentLength = Long.parseLong(request.getHeader("content-length"));
}
aliYunDriverClientService.uploadPre(resourceUri, contentLength, content);
if (contentLength == 0) {
String expect = request.getHeader("Expect");
// 支持大文件上传
if ("100-continue".equalsIgnoreCase(expect)) {
try {
response.sendError(100, "Continue");
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
aliYunDriverClientService.uploadPre(resourceUri, contentLength, content);
return contentLength;
}
@ -155,6 +166,8 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -155,6 +166,8 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public StoredObject getStoredObject(ITransaction transaction, String uri) {
LOGGER.debug("getStoredObject: {}", uri);
TFile tFile = aliYunDriverClientService.getTFileByPath(uri);
if (tFile != null) {

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

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

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

@ -23,11 +23,13 @@ @@ -23,11 +23,13 @@
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.security.Principal;
import net.sf.webdav.exceptions.WebdavException;
/**
* Interface for simple implementation of any store for the WebdavServlet
* <p>
@ -48,15 +50,15 @@ public interface IWebdavStore { @@ -48,15 +50,15 @@ public interface IWebdavStore {
* or {@link #rollback()}. If only non-read methods have been called, the
* request will be terminated by a {@link #commit()}. This method will be
* called by (@link WebdavStoreAdapter} at the beginning of each request.
*
*
*
*
* @param principal
* the principal that started this request or <code>null</code> if
* there is non available
*
*
* @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

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

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

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

@ -0,0 +1,32 @@ @@ -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 @@ @@ -1,5 +1,15 @@
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.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -7,29 +17,6 @@ import java.security.Principal; @@ -7,29 +17,6 @@ import java.security.Principal;
import java.util.Enumeration;
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 {
private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory
@ -124,7 +111,7 @@ public class WebDavServletBean extends HttpServlet { @@ -124,7 +111,7 @@ public class WebDavServletBean extends HttpServlet {
try {
Principal userPrincipal = getUserPrincipal(req);
transaction = _store.begin(userPrincipal);
transaction = _store.begin(userPrincipal, req, resp);
needRollback = true;
_store.checkAuthentication(transaction);
resp.setStatus(WebdavStatus.SC_OK);

Loading…
Cancel
Save