From 31c0876e586c8ca5fa63768ebd0b8b0454510b23 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sat, 29 May 2021 09:20:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=97=E8=A1=A8=E4=B8=AD?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=98=BE=E7=A4=BA=E4=B8=8A=E4=BC=A0=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/AliYunDriverClientService.java | 86 +++++++++---------- .../store/VirtualTFileService.java | 68 +++++++++++++++ 2 files changed, 111 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/github/zxbu/webdavteambition/store/VirtualTFileService.java diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java index c86258a..45177c4 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java +++ b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java @@ -12,6 +12,7 @@ import net.sf.webdav.exceptions.WebdavException; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -27,10 +28,13 @@ public class AliYunDriverClientService { private static String rootPath = "/"; private static int chunkSize = 10485760; // 10MB private TFile rootTFile = null; - private ThreadLocal>> tFilesCache = new ThreadLocal<>(); + private final ThreadLocal>> tFilesCache = new ThreadLocal<>(); private final AliYunDriverClient client; + @Autowired + private VirtualTFileService virtualTFileService; + public AliYunDriverClientService(AliYunDriverClient aliYunDriverClient) { this.client = aliYunDriverClient; AliYunDriverFileSystemStore.setBean(this); @@ -42,29 +46,38 @@ public class AliYunDriverClientService { map = new ConcurrentHashMap<>(); tFilesCache.set(map); } - return map.computeIfAbsent(nodeId, key -> { - FileListRequest listQuery = new FileListRequest(); - listQuery.setOffset(0); - listQuery.setLimit(10000); - listQuery.setOrder_by("updated_at"); - listQuery.setOrder_direction("DESC"); - listQuery.setDrive_id(client.getDriveId()); - listQuery.setParent_file_id(nodeId); - String json = client.post("/file/list", listQuery); - TFileListResult tFileListResult = JsonUtil.readValue(json, new TypeReference>() { - }); - List tFileList = tFileListResult.getItems(); - tFileList.sort(Comparator.comparing(TFile::getUpdated_at).reversed()); - Set tFileSets = new LinkedHashSet<>(); - for (TFile tFile : tFileList) { - if (!tFileSets.add(tFile)) { - LOGGER.info("当前目录下{} 存在同名文件:{},文件大小:{}", nodeId, tFile.getName(), tFile.getSize()); - } - } - // 对文件名进行去重,只保留最新的一个 - return tFileSets; + Set tFiles = map.computeIfAbsent(nodeId, key -> { + // 获取真实的文件列表 + return getTFiles2(nodeId); }); + Set all = new LinkedHashSet<>(tFiles); + // 获取上传中的文件列表 + Collection virtualTFiles = virtualTFileService.list(nodeId); + all.addAll(virtualTFiles); + return all; + } + private Set getTFiles2(String nodeId) { + FileListRequest listQuery = new FileListRequest(); + listQuery.setOffset(0); + listQuery.setLimit(10000); + listQuery.setOrder_by("updated_at"); + listQuery.setOrder_direction("DESC"); + listQuery.setDrive_id(client.getDriveId()); + listQuery.setParent_file_id(nodeId); + String json = client.post("/file/list", listQuery); + TFileListResult tFileListResult = JsonUtil.readValue(json, new TypeReference>() { + }); + List tFileList = tFileListResult.getItems(); + tFileList.sort(Comparator.comparing(TFile::getUpdated_at).reversed()); + Set tFileSets = new LinkedHashSet<>(); + for (TFile tFile : tFileList) { + if (!tFileSets.add(tFile)) { + LOGGER.info("当前目录下{} 存在同名文件:{},文件大小:{}", nodeId, tFile.getName(), tFile.getSize()); + } + } + // 对文件名进行去重,只保留最新的一个 + return tFileSets; } @@ -125,6 +138,9 @@ public class AliYunDriverClientService { UploadPreResult uploadPreResult = JsonUtil.readValue(json, UploadPreResult.class); List partInfoList = uploadPreResult.getPart_info_list(); if (partInfoList != null) { + if (size > 0) { + virtualTFileService.createTFile(parent.getFile_id(), uploadPreResult); + } LOGGER.info("文件预处理成功,开始上传。文件名:{},上传URL数量:{}", path, partInfoList.size()); byte[] buffer = new byte[chunkSize]; @@ -133,12 +149,14 @@ public class AliYunDriverClientService { try { int read = IOUtils.read(inputStream, buffer, 0, buffer.length); if (read == -1) { + LOGGER.info("文件上传结束。文件名:{},当前进度:{}/{}", path, (i + 1), partInfoList.size()); return; } client.upload(partInfo.getUpload_url(), buffer, 0, read); - LOGGER.info("文件正在上传。文件名:{},当前进度:{}/{}", path, (i+1), partInfoList.size()); - + virtualTFileService.updateLength(parent.getFile_id(), uploadPreResult.getFile_id(), buffer.length); + LOGGER.info("文件正在上传。文件名:{},当前进度:{}/{}", path, (i + 1), partInfoList.size()); } catch (IOException e) { + virtualTFileService.remove(parent.getFile_id(), uploadPreResult.getFile_id()); throw new WebdavException(e); } } @@ -152,26 +170,8 @@ public class AliYunDriverClientService { uploadFinalRequest.setUpload_id(uploadPreResult.getUpload_id()); client.post("/file/complete", uploadFinalRequest); + virtualTFileService.remove(parent.getFile_id(), uploadPreResult.getFile_id()); LOGGER.info("文件上传成功。文件名:{}", path); -// if (!uploadPreResult.getName().equals(pathInfo.getName())) { -// LOGGER.info("上传文件名{}与原文件名{}不同,对文件进行重命名", uploadPreResult.getName(), pathInfo.getName()); -// TFile oldFile = getNodeIdByPath2(path); -// // 如果旧文件存在,则先删除 -// if (oldFile != null) { -// LOGGER.info("旧文件{}还存在,大小为{},进行删除操作,可前往网页版的回收站查看", path, oldFile.getSize()); -// remove(path); -// try { -// Thread.sleep(1500); -// } catch (InterruptedException e) { -// // no -// } -// } -// RenameRequest renameRequest = new RenameRequest(); -// renameRequest.setDrive_id(client.getDriveId()); -// renameRequest.setFile_id(oldFile.getFile_id()); -// renameRequest.setName(pathInfo.getName()); -// client.post("/file/update", renameRequest); -// } clearCache(); } diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/VirtualTFileService.java b/src/main/java/com/github/zxbu/webdavteambition/store/VirtualTFileService.java new file mode 100644 index 0000000..acf7a8e --- /dev/null +++ b/src/main/java/com/github/zxbu/webdavteambition/store/VirtualTFileService.java @@ -0,0 +1,68 @@ +package com.github.zxbu.webdavteambition.store; + +import com.github.zxbu.webdavteambition.model.FileType; +import com.github.zxbu.webdavteambition.model.result.TFile; +import com.github.zxbu.webdavteambition.model.result.UploadPreResult; +import org.springframework.stereotype.Service; + +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 虚拟文件(用于上传时,列表展示) + */ +@Service +public class VirtualTFileService { + private final Map> virtualTFileMap = new ConcurrentHashMap<>(); + + /** + * 创建文件 + */ + public void createTFile(String parentId, UploadPreResult uploadPreResult) { + Map tFileMap = virtualTFileMap.computeIfAbsent(parentId, s -> new ConcurrentHashMap<>()); + tFileMap.put(uploadPreResult.getFile_id(), convert(uploadPreResult)); + } + + public void updateLength(String parentId, String fileId, long length) { + Map tFileMap = virtualTFileMap.get(parentId); + if (tFileMap == null) { + return; + } + TFile tFile = tFileMap.get(fileId); + if (tFile == null) { + return; + } + tFile.setSize(tFile.getSize() + length); + tFile.setUpdated_at(new Date()); + } + + public void remove(String parentId, String fileId) { + Map tFileMap = virtualTFileMap.get(parentId); + if (tFileMap == null) { + return; + } + tFileMap.remove(fileId); + } + + public Collection list(String parentId) { + Map tFileMap = virtualTFileMap.get(parentId); + if (tFileMap == null) { + return Collections.emptyList(); + } + return tFileMap.values(); + } + + private TFile convert(UploadPreResult uploadPreResult) { + TFile tFile = new TFile(); + tFile.setCreated_at(new Date()); + tFile.setFile_id(uploadPreResult.getFile_id()); + tFile.setName(uploadPreResult.getFile_name()); + tFile.setType(FileType.file.name()); + tFile.setUpdated_at(new Date()); + tFile.setSize(0L); + return tFile; + } +}