Browse Source

实现分享连接的解析和使用(只读)

develop^2
xausky 3 years ago
parent
commit
68faba24bf
  1. 30
      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. 10
      src/main/java/com/github/zxbu/webdavteambition/model/result/TFile.java
  8. 74
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java
  9. 2
      src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java

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

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

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

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

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

@ -2,7 +2,9 @@ package com.github.zxbu.webdavteambition.model;
public class DownloadRequest { public class DownloadRequest {
private String drive_id; private String drive_id;
private String share_id;
private String file_id; private String file_id;
private String shareToken;
private Integer expire_sec = 14400; private Integer expire_sec = 14400;
public String getDrive_id() { public String getDrive_id() {
@ -28,4 +30,22 @@ public class DownloadRequest {
public void setExpire_sec(Integer expire_sec) { public void setExpire_sec(Integer expire_sec) {
this.expire_sec = 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,6 +2,7 @@ package com.github.zxbu.webdavteambition.model;
public class FileListRequest extends Page{ public class FileListRequest extends Page{
private String drive_id; private String drive_id;
private String share_id;
private Boolean all = false; private Boolean all = false;
private String fields = "*"; private String fields = "*";
private String image_thumbnail_process = "image/resize,w_400/format,jpeg"; private String image_thumbnail_process = "image/resize,w_400/format,jpeg";
@ -9,6 +10,15 @@ public class FileListRequest extends Page{
private String parent_file_id; private String parent_file_id;
private String video_thumbnail_process = "video/snapshot,t_0,f_jpg,ar_auto,w_300"; 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() { public String getDrive_id() {
return drive_id; return drive_id;
} }

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

@ -0,0 +1,24 @@
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

@ -0,0 +1,36 @@
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;
}
}

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

@ -20,6 +20,7 @@ public class TFile {
private String url; private String url;
private Long size; private Long size;
private String download_url; private String download_url;
private String share_id;
public Date getCreated_at() { public Date getCreated_at() {
return created_at; return created_at;
@ -148,4 +149,13 @@ public class TFile {
public void setUpdated_at(Date updated_at) { public void setUpdated_at(Date updated_at) {
this.updated_at = 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;
}
} }

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

@ -24,8 +24,9 @@ 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.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service @Service
public class AliYunDriverClientService { public class AliYunDriverClientService {
@ -34,6 +35,7 @@ public class AliYunDriverClientService {
private static String rootPath = "/"; private static String rootPath = "/";
private static int chunkSize = 10485760; // 10MB private static int chunkSize = 10485760; // 10MB
private TFile rootTFile = null; private TFile rootTFile = null;
private Pattern sharePatten = Pattern.compile("^.*卍([a-zA-Z0-9]{11})$");
private static Cache<String, Set<TFile>> tFilesCache = Caffeine.newBuilder() private static Cache<String, Set<TFile>> tFilesCache = Caffeine.newBuilder()
.initialCapacity(128) .initialCapacity(128)
@ -51,10 +53,10 @@ public class AliYunDriverClientService {
AliYunDriverFileSystemStore.setBean(this); AliYunDriverFileSystemStore.setBean(this);
} }
public Set<TFile> getTFiles(String nodeId) { public Set<TFile> getTFiles(String nodeId, String shareId) {
Set<TFile> tFiles = tFilesCache.get(nodeId, key -> { Set<TFile> tFiles = tFilesCache.get(shareId + ":" + nodeId, key -> {
// 获取真实的文件列表 // 获取真实的文件列表
return getTFiles2(nodeId); return getTFiles2(nodeId, shareId);
}); });
Set<TFile> all = new LinkedHashSet<>(tFiles); Set<TFile> all = new LinkedHashSet<>(tFiles);
// 获取上传中的文件列表 // 获取上传中的文件列表
@ -63,8 +65,8 @@ public class AliYunDriverClientService {
return all; return all;
} }
private Set<TFile> getTFiles2(String nodeId) { private Set<TFile> getTFiles2(String nodeId, String shareId) {
List<TFile> tFileList = fileListFromApi(nodeId, null, new ArrayList<>()); List<TFile> tFileList = fileListFromApi(nodeId, null, new ArrayList<>(), shareId);
tFileList.sort(Comparator.comparing(TFile::getUpdated_at).reversed()); tFileList.sort(Comparator.comparing(TFile::getUpdated_at).reversed());
Set<TFile> tFileSets = new LinkedHashSet<>(); Set<TFile> tFileSets = new LinkedHashSet<>();
for (TFile tFile : tFileList) { for (TFile tFile : tFileList) {
@ -76,22 +78,26 @@ public class AliYunDriverClientService {
return tFileSets; return tFileSets;
} }
private List<TFile> fileListFromApi(String nodeId, String marker, List<TFile> all) { private List<TFile> fileListFromApi(String nodeId, String marker, List<TFile> all, String shareId) {
FileListRequest listQuery = new FileListRequest(); FileListRequest listQuery = new FileListRequest();
listQuery.setMarker(marker); listQuery.setMarker(marker);
listQuery.setLimit(100); listQuery.setLimit(100);
listQuery.setOrder_by("updated_at"); listQuery.setOrder_by("updated_at");
listQuery.setOrder_direction("DESC"); listQuery.setOrder_direction("DESC");
listQuery.setDrive_id(client.getDriveId());
listQuery.setParent_file_id(nodeId); listQuery.setParent_file_id(nodeId);
String json = client.post("/file/list", listQuery); if (shareId != null){
listQuery.setShare_id(shareId);
} else {
listQuery.setDrive_id(client.getDriveId());
}
String json = client.post("/file/list", listQuery, shareId);
TFileListResult<TFile> tFileListResult = JsonUtil.readValue(json, new TypeReference<TFileListResult<TFile>>() { TFileListResult<TFile> tFileListResult = JsonUtil.readValue(json, new TypeReference<TFileListResult<TFile>>() {
}); });
all.addAll(tFileListResult.getItems()); all.addAll(tFileListResult.getItems());
if (!StringUtils.hasLength(tFileListResult.getNext_marker())) { if (!StringUtils.hasLength(tFileListResult.getNext_marker())) {
return all; return all;
} }
return fileListFromApi(nodeId, tFileListResult.getNext_marker(), all); return fileListFromApi(nodeId, tFileListResult.getNext_marker(), all, shareId);
} }
@ -283,13 +289,25 @@ public class AliYunDriverClientService {
public Response download(String path, HttpServletRequest request, long size ) { public Response download(String path, HttpServletRequest request, long size ) {
TFile file = getTFileByPath(path); TFile file = getTFileByPath(path);
DownloadRequest downloadRequest = new DownloadRequest(); if (file.getShare_id() == null){
downloadRequest.setDrive_id(client.getDriveId()); DownloadRequest downloadRequest = new DownloadRequest();
downloadRequest.setFile_id(file.getFile_id()); downloadRequest.setDrive_id(client.getDriveId());
String json = client.post("/file/get_download_url", downloadRequest); downloadRequest.setFile_id(file.getFile_id());
Object url = JsonUtil.getJsonNodeValue(json, "url"); String json = client.post("/file/get_download_url", downloadRequest);
LOGGER.debug("{} url = {}", path, url); Object url = JsonUtil.getJsonNodeValue(json, "url");
return client.download(url.toString(), request, size); 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()));
downloadRequest.setExpire_sec(600);
String json = client.post("/file/get_share_link_download_url", downloadRequest, file.getShare_id());
Object url = JsonUtil.getJsonNodeValue(json, "url");
LOGGER.debug("{} url = {}", path, url);
return client.download(url.toString(), request, size);
}
} }
private TFile getNodeIdByPath2(String path) { private TFile getNodeIdByPath2(String path) {
@ -304,7 +322,11 @@ public class AliYunDriverClientService {
if (tFile == null ) { if (tFile == null ) {
return null; return null;
} }
return getNodeIdByParentId(tFile.getFile_id(), pathInfo.getName()); Matcher matcher = sharePatten.matcher(pathInfo.getName());
if (matcher.find()){
return getShareRootTFile(matcher.group(1), pathInfo.getName());
}
return getNodeIdByParentId(tFile.getFile_id(), tFile.getShare_id(), pathInfo.getName());
} }
@ -343,14 +365,24 @@ public class AliYunDriverClientService {
return rootTFile; return rootTFile;
} }
private TFile getNodeIdByParentId(String parentId, String name) { private TFile getShareRootTFile(String shareId, String name){
Set<TFile> tFiles = getTFiles(parentId); 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);
return rootTFile;
}
private TFile getNodeIdByParentId(String parentId, String shareId, String name) {
Set<TFile> tFiles = getTFiles(parentId, shareId);
for (TFile tFile : tFiles) { for (TFile tFile : tFiles) {
if (tFile.getName().equals(name)) { if (tFile.getName().equals(name)) {
return tFile; return tFile;
} }
} }
return null; return null;
} }

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

@ -136,7 +136,7 @@ public class AliYunDriverFileSystemStore implements IWebdavStore {
if (tFile.getType().equals(FileType.file.name())) { if (tFile.getType().equals(FileType.file.name())) {
return new String[0]; return new String[0];
} }
Set<TFile> tFileList = aliYunDriverClientService.getTFiles(tFile.getFile_id()); Set<TFile> tFileList = aliYunDriverClientService.getTFiles(tFile.getFile_id(), tFile.getShare_id());
return tFileList.stream().map(TFile::getName).toArray(String[]::new); return tFileList.stream().map(TFile::getName).toArray(String[]::new);
} }

Loading…
Cancel
Save