Compare commits

..

No commits in common. 'develop' and 'main' have entirely different histories.

  1. 32
      src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java
  2. 2
      src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverCronTask.java
  3. 20
      src/main/java/com/github/zxbu/webdavteambition/model/DownloadRequest.java
  4. 10
      src/main/java/com/github/zxbu/webdavteambition/model/FileListRequest.java
  5. 24
      src/main/java/com/github/zxbu/webdavteambition/model/ShareTokenRequest.java
  6. 36
      src/main/java/com/github/zxbu/webdavteambition/model/result/ShareTokenResult.java
  7. 30
      src/main/java/com/github/zxbu/webdavteambition/model/result/TFile.java
  8. 95
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java
  9. 25
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java

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

@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
package com.github.zxbu.webdavteambition.client;
import com.github.zxbu.webdavteambition.config.AliYunDriveProperties;
import com.github.zxbu.webdavteambition.model.ShareTokenRequest;
import com.github.zxbu.webdavteambition.model.result.ShareTokenResult;
import com.github.zxbu.webdavteambition.util.JsonUtil;
import net.sf.webdav.exceptions.WebdavException;
import okhttp3.*;
@ -20,8 +18,6 @@ import java.nio.file.LinkOption; @@ -20,8 +18,6 @@ import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -29,7 +25,6 @@ public class AliYunDriverClient { @@ -29,7 +25,6 @@ public class AliYunDriverClient {
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverClient.class);
private OkHttpClient okHttpClient;
private AliYunDriveProperties aliYunDriveProperties;
private Map<String, ShareTokenResult> shareTokenMapping = new HashMap<>();
public AliYunDriverClient(AliYunDriveProperties aliYunDriveProperties) {
@ -146,18 +141,10 @@ public class AliYunDriverClient { @@ -146,18 +141,10 @@ public class AliYunDriverClient {
}
public String post(String url, Object body) {
return post(url, body, null, null);
}
public String post(String url, Object body, String shareId, String sharePassword) {
String bodyAsJson = JsonUtil.toJson(body);
Request.Builder requestBuilder = new Request.Builder()
Request request = new Request.Builder()
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), bodyAsJson))
.url(getTotalUrl(url));
if (shareId != null){
requestBuilder.header("X-Share-Token", readShareToken(shareId, sharePassword));
}
Request request = requestBuilder.build();
.url(getTotalUrl(url)).build();
try (Response response = okHttpClient.newCall(request).execute()){
LOGGER.info("post {}, body {}, code {}", url, bodyAsJson, response.code());
if (!response.isSuccessful()) {
@ -220,21 +207,6 @@ public class AliYunDriverClient { @@ -220,21 +207,6 @@ public class AliYunDriverClient {
return aliYunDriveProperties.getUrl() + url;
}
public synchronized String readShareToken(String shareId, String sharePassword){
// shareKey 为 sharePassword == null ? shareId : shareId + ":" + sharePassword
ShareTokenResult result = shareTokenMapping.get(shareId + ":" + sharePassword);
if (result != null && result.getExpire_time().after(new Date())){
return result.getShare_token();
}
ShareTokenRequest request = new ShareTokenRequest();
request.setShare_id(shareId);
request.setShare_pwd(sharePassword == null ? "" : sharePassword);
String resultString = post("/share_link/get_share_token", request);
result = JsonUtil.readValue(resultString, ShareTokenResult.class);
shareTokenMapping.put(shareId, result);
return result.getShare_token();
}
private void deleteRefreshTokenFile() {
String refreshTokenPath = aliYunDriveProperties.getWorkDir() + "refresh-token";
Path path = Paths.get(refreshTokenPath);

2
src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverCronTask.java

@ -19,7 +19,7 @@ public class AliYunDriverCronTask { @@ -19,7 +19,7 @@ public class AliYunDriverCronTask {
public void refreshToken() {
try {
TFile root = aliYunDriverClientService.getTFileByPath("/");
aliYunDriverClientService.getTFiles(root.getFile_id(), null, null);
aliYunDriverClientService.getTFiles(root.getFile_id());
} catch (Exception e) {
// nothing
}

20
src/main/java/com/github/zxbu/webdavteambition/model/DownloadRequest.java

@ -2,9 +2,7 @@ package com.github.zxbu.webdavteambition.model; @@ -2,9 +2,7 @@ package com.github.zxbu.webdavteambition.model;
public class DownloadRequest {
private String drive_id;
private String share_id;
private String file_id;
private String shareToken;
private Integer expire_sec = 14400;
public String getDrive_id() {
@ -30,22 +28,4 @@ public class DownloadRequest { @@ -30,22 +28,4 @@ public class DownloadRequest {
public void setExpire_sec(Integer expire_sec) {
this.expire_sec = expire_sec;
}
public String getShare_id() {
return share_id;
}
public DownloadRequest setShare_id(String share_id) {
this.share_id = share_id;
return this;
}
public String getShareToken() {
return shareToken;
}
public DownloadRequest setShareToken(String shareToken) {
this.shareToken = shareToken;
return this;
}
}

10
src/main/java/com/github/zxbu/webdavteambition/model/FileListRequest.java

@ -2,7 +2,6 @@ package com.github.zxbu.webdavteambition.model; @@ -2,7 +2,6 @@ package com.github.zxbu.webdavteambition.model;
public class FileListRequest extends Page{
private String drive_id;
private String share_id;
private Boolean all = false;
private String fields = "*";
private String image_thumbnail_process = "image/resize,w_400/format,jpeg";
@ -10,15 +9,6 @@ public class FileListRequest extends Page{ @@ -10,15 +9,6 @@ public class FileListRequest extends Page{
private String parent_file_id;
private String video_thumbnail_process = "video/snapshot,t_0,f_jpg,ar_auto,w_300";
public String getShare_id() {
return share_id;
}
public FileListRequest setShare_id(String share_id) {
this.share_id = share_id;
return this;
}
public String getDrive_id() {
return drive_id;
}

24
src/main/java/com/github/zxbu/webdavteambition/model/ShareTokenRequest.java

@ -1,24 +0,0 @@ @@ -1,24 +0,0 @@
package com.github.zxbu.webdavteambition.model;
public class ShareTokenRequest {
private String share_id;
private String share_pwd;
public String getShare_id() {
return share_id;
}
public ShareTokenRequest setShare_id(String share_id) {
this.share_id = share_id;
return this;
}
public String getShare_pwd() {
return share_pwd;
}
public ShareTokenRequest setShare_pwd(String share_pwd) {
this.share_pwd = share_pwd;
return this;
}
}

36
src/main/java/com/github/zxbu/webdavteambition/model/result/ShareTokenResult.java

@ -1,36 +0,0 @@ @@ -1,36 +0,0 @@
package com.github.zxbu.webdavteambition.model.result;
import java.util.Date;
public class ShareTokenResult {
private String share_token;
private Integer expires_in;
private Date expire_time;
public Date getExpire_time() {
return expire_time;
}
public ShareTokenResult setExpire_time(Date expire_time) {
this.expire_time = expire_time;
return this;
}
public String getShare_token() {
return share_token;
}
public ShareTokenResult setShare_token(String share_token) {
this.share_token = share_token;
return this;
}
public Integer getExpires_in() {
return expires_in;
}
public ShareTokenResult setExpires_in(Integer expires_in) {
this.expires_in = expires_in;
return this;
}
}

30
src/main/java/com/github/zxbu/webdavteambition/model/result/TFile.java

@ -20,9 +20,6 @@ public class TFile { @@ -20,9 +20,6 @@ public class TFile {
private String url;
private Long size;
private String download_url;
private String share_id;
private String share_password;
private String origin_file_id;
public Date getCreated_at() {
return created_at;
@ -151,31 +148,4 @@ public class TFile { @@ -151,31 +148,4 @@ public class TFile {
public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
public String getShare_id() {
return share_id;
}
public TFile setShare_id(String share_id) {
this.share_id = share_id;
return this;
}
public String getOrigin_file_id() {
return origin_file_id;
}
public TFile setOrigin_file_id(String origin_file_id) {
this.origin_file_id = origin_file_id;
return this;
}
public String getShare_password() {
return share_password;
}
public TFile setShare_password(String share_password) {
this.share_password = share_password;
return this;
}
}

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

@ -26,8 +26,6 @@ import java.io.InputStream; @@ -26,8 +26,6 @@ import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class AliYunDriverClientService {
@ -36,7 +34,6 @@ public class AliYunDriverClientService { @@ -36,7 +34,6 @@ public class AliYunDriverClientService {
private static String rootPath = "/";
private static int chunkSize = 10485760; // 10MB
private TFile rootTFile = null;
private final Pattern shareNamePatten = Pattern.compile("^.*!(?<shareId>[a-zA-Z0-9]{11})(?>$|:(?<password>[a-zA-Z0-9]{4})$)");
private static Cache<String, Set<TFile>> tFilesCache = Caffeine.newBuilder()
.initialCapacity(128)
@ -54,10 +51,10 @@ public class AliYunDriverClientService { @@ -54,10 +51,10 @@ public class AliYunDriverClientService {
AliYunDriverFileSystemStore.setBean(this);
}
public Set<TFile> getTFiles(String nodeId, String shareId, String sharePassword) {
Set<TFile> tFiles = tFilesCache.get(shareId + ":" + nodeId, key -> {
public Set<TFile> getTFiles(String nodeId) {
Set<TFile> tFiles = tFilesCache.get(nodeId, key -> {
// 获取真实的文件列表
return getTFiles2(nodeId, shareId, sharePassword);
return getTFiles2(nodeId);
});
Set<TFile> all = new LinkedHashSet<>(tFiles);
// 获取上传中的文件列表
@ -66,12 +63,11 @@ public class AliYunDriverClientService { @@ -66,12 +63,11 @@ public class AliYunDriverClientService {
return all;
}
private Set<TFile> getTFiles2(String nodeId, String shareId, String sharePassword) {
List<TFile> tFileList = fileListFromApi(nodeId, null, new ArrayList<>(), shareId, sharePassword);
private Set<TFile> getTFiles2(String nodeId) {
List<TFile> tFileList = fileListFromApi(nodeId, null, new ArrayList<>());
tFileList.sort(Comparator.comparing(TFile::getUpdated_at).reversed());
Set<TFile> tFileSets = new LinkedHashSet<>();
for (TFile tFile : tFileList) {
tFile.setShare_password(sharePassword);
if (!tFileSets.add(tFile)) {
LOGGER.info("当前目录下{} 存在同名文件:{},文件大小:{}", nodeId, tFile.getName(), tFile.getSize());
}
@ -80,32 +76,22 @@ public class AliYunDriverClientService { @@ -80,32 +76,22 @@ public class AliYunDriverClientService {
return tFileSets;
}
private List<TFile> fileListFromApi(String nodeId, String marker, List<TFile> all, String shareId, String sharePassword) {
private List<TFile> fileListFromApi(String nodeId, String marker, List<TFile> all) {
FileListRequest listQuery = new FileListRequest();
listQuery.setMarker(marker);
listQuery.setLimit(100);
listQuery.setOrder_by("updated_at");
listQuery.setOrder_direction("DESC");
listQuery.setDrive_id(client.getDriveId());
listQuery.setParent_file_id(nodeId);
if (shareId != null){
listQuery.setShare_id(shareId);
} else {
listQuery.setDrive_id(client.getDriveId());
}
String json;
try {
json = client.post("/file/list", listQuery, shareId, sharePassword);
} catch (WebdavException e){
// 无法获取列表可能是 shareId 已经失效,与其报错不如返回空,特别是使用 dav 挂载时候,报错会导致 IO 错误无法删除目录
e.printStackTrace();
return Collections.emptyList();
}
TFileListResult<TFile> tFileListResult = JsonUtil.readValue(json, new TypeReference<TFileListResult<TFile>>() {});
String json = client.post("/file/list", listQuery);
TFileListResult<TFile> tFileListResult = JsonUtil.readValue(json, new TypeReference<TFileListResult<TFile>>() {
});
all.addAll(tFileListResult.getItems());
if (!StringUtils.hasLength(tFileListResult.getNext_marker())) {
return all;
}
return fileListFromApi(nodeId, tFileListResult.getNext_marker(), all, shareId, sharePassword);
return fileListFromApi(nodeId, tFileListResult.getNext_marker(), all);
}
@ -227,7 +213,7 @@ public class AliYunDriverClientService { @@ -227,7 +213,7 @@ public class AliYunDriverClientService {
TFile tFile = getTFileByPath(sourcePath);
RenameRequest renameRequest = new RenameRequest();
renameRequest.setDrive_id(client.getDriveId());
renameRequest.setFile_id(tFile.getOrigin_file_id() != null ? tFile.getOrigin_file_id() : tFile.getFile_id());
renameRequest.setFile_id(tFile.getFile_id());
renameRequest.setName(newName);
client.post("/file/update", renameRequest);
clearCache();
@ -241,7 +227,7 @@ public class AliYunDriverClientService { @@ -241,7 +227,7 @@ public class AliYunDriverClientService {
TFile targetTFile = getTFileByPath(targetPath);
MoveRequest moveRequest = new MoveRequest();
moveRequest.setDrive_id(client.getDriveId());
moveRequest.setFile_id(sourceTFile.getOrigin_file_id() != null ? sourceTFile.getOrigin_file_id() : sourceTFile.getFile_id());
moveRequest.setFile_id(sourceTFile.getFile_id());
moveRequest.setTo_parent_file_id(targetTFile.getFile_id());
client.post("/file/move", moveRequest);
clearCache();
@ -255,7 +241,7 @@ public class AliYunDriverClientService { @@ -255,7 +241,7 @@ public class AliYunDriverClientService {
}
RemoveRequest removeRequest = new RemoveRequest();
removeRequest.setDrive_id(client.getDriveId());
removeRequest.setFile_id(tFile.getOrigin_file_id() != null ? tFile.getOrigin_file_id() : tFile.getFile_id());
removeRequest.setFile_id(tFile.getFile_id());
client.post("/recyclebin/trash", removeRequest);
clearCache();
}
@ -297,25 +283,13 @@ public class AliYunDriverClientService { @@ -297,25 +283,13 @@ public class AliYunDriverClientService {
public Response download(String path, HttpServletRequest request, long size ) {
TFile file = getTFileByPath(path);
if (file.getShare_id() == null){
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");
LOGGER.debug("{} url = {}", path, url);
return client.download(url.toString(), request, size);
} else {
DownloadRequest downloadRequest = new DownloadRequest();
downloadRequest.setFile_id(file.getFile_id());
downloadRequest.setShare_id(file.getShare_id());
downloadRequest.setShareToken(client.readShareToken(file.getShare_id(), file.getShare_password()));
downloadRequest.setExpire_sec(600);
String json = client.post("/file/get_share_link_download_url", downloadRequest, file.getShare_id(), file.getShare_password());
Object url = JsonUtil.getJsonNodeValue(json, "url");
LOGGER.debug("{} url = {}", path, url);
return client.download(url.toString(), request, size);
}
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");
LOGGER.debug("{} url = {}", path, url);
return client.download(url.toString(), request, size);
}
private TFile getNodeIdByPath2(String path) {
@ -330,7 +304,7 @@ public class AliYunDriverClientService { @@ -330,7 +304,7 @@ public class AliYunDriverClientService {
if (tFile == null ) {
return null;
}
return getNodeIdByParentId(tFile.getFile_id(), pathInfo.getName(), tFile.getShare_id(), tFile.getShare_password());
return getNodeIdByParentId(tFile.getFile_id(), pathInfo.getName());
}
@ -369,31 +343,14 @@ public class AliYunDriverClientService { @@ -369,31 +343,14 @@ public class AliYunDriverClientService {
return rootTFile;
}
private TFile getShareRootTFile(String shareId, String name, String originFileId, String sharePassword){
TFile rootTFile = new TFile();
rootTFile.setName(name);
rootTFile.setFile_id("root");
rootTFile.setCreated_at(new Date());
rootTFile.setUpdated_at(new Date());
rootTFile.setType("folder");
rootTFile.setShare_id(shareId);
rootTFile.setOrigin_file_id(originFileId);
rootTFile.setShare_password(sharePassword);
return rootTFile;
}
private TFile getNodeIdByParentId(String parentId, String name, String shareId, String sharePassword) {
Set<TFile> tFiles = getTFiles(parentId, shareId, sharePassword);
private TFile getNodeIdByParentId(String parentId, String name) {
Set<TFile> tFiles = getTFiles(parentId);
for (TFile tFile : tFiles) {
if (tFile.getName().equals(name)) {
Matcher matcher = shareNamePatten.matcher(name);
if (matcher.find()){
return getShareRootTFile(matcher.group("shareId"), name, tFile.getFile_id(), matcher.group("password"));
} else {
return tFile;
}
return tFile;
}
}
return null;
}

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

@ -21,15 +21,12 @@ import java.security.Principal; @@ -21,15 +21,12 @@ import java.security.Principal;
import java.util.Enumeration;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
public class AliYunDriverFileSystemStore implements IWebdavStore {
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverFileSystemStore.class);
private static AliYunDriverClientService aliYunDriverClientService;
private final Pattern inSharePatten = Pattern.compile(".*!(?<shareId>[a-zA-Z0-9]{11})(?>\\/.|:(?<password>[a-zA-Z0-9]{4})\\/.)");
public AliYunDriverFileSystemStore(File file) {
}
@ -71,18 +68,14 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -71,18 +68,14 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public void createFolder(ITransaction transaction, String folderUri) {
LOGGER.info("createFolder {}", folderUri);
if (inSharePatten.matcher(folderUri).find()){
throw new WebdavException("共享目录不可写");
}
aliYunDriverClientService.createFolder(folderUri);
}
@Override
public void createResource(ITransaction transaction, String resourceUri) {
LOGGER.info("createResource {}", resourceUri);
if (inSharePatten.matcher(resourceUri).find()){
throw new WebdavException("共享目录不可写");
}
}
@Override
@ -110,9 +103,6 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -110,9 +103,6 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public long setResourceContent(ITransaction transaction, String resourceUri, InputStream content, String contentType, String characterEncoding) {
LOGGER.info("setResourceContent {}", resourceUri);
if (inSharePatten.matcher(resourceUri).find()){
throw new WebdavException("共享目录不可写");
}
HttpServletRequest request = transaction.getRequest();
HttpServletResponse response = transaction.getResponse();
@ -146,7 +136,7 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -146,7 +136,7 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
if (tFile.getType().equals(FileType.file.name())) {
return new String[0];
}
Set<TFile> tFileList = aliYunDriverClientService.getTFiles(tFile.getFile_id(), tFile.getShare_id(), tFile.getShare_password());
Set<TFile> tFileList = aliYunDriverClientService.getTFiles(tFile.getFile_id());
return tFileList.stream().map(TFile::getName).toArray(String[]::new);
}
@ -170,20 +160,13 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @@ -170,20 +160,13 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
@Override
public void removeObject(ITransaction transaction, String uri) {
LOGGER.info("removeObject: {}", uri);
if (inSharePatten.matcher(uri).find()){
// 对于共享文件夹内的删除进行跳过,因为在删除共享目录时候用户使用 rm -r 'Test!Pfb5mXu2TLK' 之类的命令或者使用 GUI 删除都会递归删除子文件,如果这里报错就会导致本身的文件夹也无法删除
LOGGER.info("removeObject skip: {}", uri);
return;
}
aliYunDriverClientService.remove(uri);
}
@Override
public boolean moveObject(ITransaction transaction, String destinationPath, String sourcePath) {
LOGGER.info("moveObject, destinationPath={}, sourcePath={}", destinationPath, sourcePath);
if (inSharePatten.matcher(destinationPath).find() || inSharePatten.matcher(sourcePath).find()){
throw new WebdavException("共享目录不可写");
}
PathInfo destinationPathInfo = aliYunDriverClientService.getPathInfo(destinationPath);
PathInfo sourcePathInfo = aliYunDriverClientService.getPathInfo(sourcePath);
// 名字相同,说明是移动目录

Loading…
Cancel
Save