|
|
@ -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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|